Browse code

Move verify container resources to a separate function

Make the code easy to view.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>

Qiang Huang authored on 2015/12/11 11:59:29
Showing 1 changed files
... ...
@@ -180,131 +180,151 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
180 180
 	return nil
181 181
 }
182 182
 
183
-// verifyPlatformContainerSettings performs platform-specific validation of the
184
-// hostconfig and config structures.
185
-func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *runconfig.HostConfig, config *runconfig.Config) ([]string, error) {
183
+func verifyContainerResources(resources *runconfig.Resources) ([]string, error) {
186 184
 	warnings := []string{}
187 185
 	sysInfo := sysinfo.New(true)
188 186
 
189
-	warnings, err := daemon.verifyExperimentalContainerSettings(hostConfig, config)
190
-	if err != nil {
191
-		return warnings, err
192
-	}
193
-
194
-	if hostConfig.ShmSize != nil && *hostConfig.ShmSize <= 0 {
195
-		return warnings, fmt.Errorf("SHM size must be greater then 0")
196
-	}
197
-
198 187
 	// memory subsystem checks and adjustments
199
-	if hostConfig.Memory != 0 && hostConfig.Memory < linuxMinMemory {
188
+	if resources.Memory != 0 && resources.Memory < linuxMinMemory {
200 189
 		return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
201 190
 	}
202
-	if hostConfig.Memory > 0 && !sysInfo.MemoryLimit {
191
+	if resources.Memory > 0 && !sysInfo.MemoryLimit {
203 192
 		warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
204 193
 		logrus.Warnf("Your kernel does not support memory limit capabilities. Limitation discarded.")
205
-		hostConfig.Memory = 0
206
-		hostConfig.MemorySwap = -1
194
+		resources.Memory = 0
195
+		resources.MemorySwap = -1
207 196
 	}
208
-	if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !sysInfo.SwapLimit {
197
+	if resources.Memory > 0 && resources.MemorySwap != -1 && !sysInfo.SwapLimit {
209 198
 		warnings = append(warnings, "Your kernel does not support swap limit capabilities, memory limited without swap.")
210 199
 		logrus.Warnf("Your kernel does not support swap limit capabilities, memory limited without swap.")
211
-		hostConfig.MemorySwap = -1
200
+		resources.MemorySwap = -1
212 201
 	}
213
-	if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
202
+	if resources.Memory > 0 && resources.MemorySwap > 0 && resources.MemorySwap < resources.Memory {
214 203
 		return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.")
215 204
 	}
216
-	if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
205
+	if resources.Memory == 0 && resources.MemorySwap > 0 {
217 206
 		return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.")
218 207
 	}
219
-	if hostConfig.MemorySwappiness != nil && *hostConfig.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
208
+	if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
220 209
 		warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
221 210
 		logrus.Warnf("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
222
-		hostConfig.MemorySwappiness = nil
211
+		resources.MemorySwappiness = nil
223 212
 	}
224
-	if hostConfig.MemorySwappiness != nil {
225
-		swappiness := *hostConfig.MemorySwappiness
213
+	if resources.MemorySwappiness != nil {
214
+		swappiness := *resources.MemorySwappiness
226 215
 		if swappiness < -1 || swappiness > 100 {
227 216
 			return warnings, fmt.Errorf("Invalid value: %v, valid memory swappiness range is 0-100.", swappiness)
228 217
 		}
229 218
 	}
230
-	if hostConfig.MemoryReservation > 0 && !sysInfo.MemoryReservation {
219
+	if resources.MemoryReservation > 0 && !sysInfo.MemoryReservation {
231 220
 		warnings = append(warnings, "Your kernel does not support memory soft limit capabilities. Limitation discarded.")
232 221
 		logrus.Warnf("Your kernel does not support memory soft limit capabilities. Limitation discarded.")
233
-		hostConfig.MemoryReservation = 0
222
+		resources.MemoryReservation = 0
234 223
 	}
235
-	if hostConfig.Memory > 0 && hostConfig.MemoryReservation > 0 && hostConfig.Memory < hostConfig.MemoryReservation {
224
+	if resources.Memory > 0 && resources.MemoryReservation > 0 && resources.Memory < resources.MemoryReservation {
236 225
 		return warnings, fmt.Errorf("Minimum memory limit should be larger than memory reservation limit, see usage.")
237 226
 	}
238
-	if hostConfig.KernelMemory > 0 && !sysInfo.KernelMemory {
227
+	if resources.KernelMemory > 0 && !sysInfo.KernelMemory {
239 228
 		warnings = append(warnings, "Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
240 229
 		logrus.Warnf("Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
241
-		hostConfig.KernelMemory = 0
230
+		resources.KernelMemory = 0
242 231
 	}
243
-	if hostConfig.KernelMemory > 0 && hostConfig.KernelMemory < linuxMinMemory {
232
+	if resources.KernelMemory > 0 && resources.KernelMemory < linuxMinMemory {
244 233
 		return warnings, fmt.Errorf("Minimum kernel memory limit allowed is 4MB")
245 234
 	}
246
-	if hostConfig.KernelMemory > 0 && !checkKernelVersion(4, 0, 0) {
235
+	if resources.KernelMemory > 0 && !checkKernelVersion(4, 0, 0) {
247 236
 		warnings = append(warnings, "You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
248 237
 		logrus.Warnf("You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
249 238
 	}
250
-	if hostConfig.CPUShares > 0 && !sysInfo.CPUShares {
239
+
240
+	// cpu subsystem checks and adjustments
241
+	if resources.CPUShares > 0 && !sysInfo.CPUShares {
251 242
 		warnings = append(warnings, "Your kernel does not support CPU shares. Shares discarded.")
252 243
 		logrus.Warnf("Your kernel does not support CPU shares. Shares discarded.")
253
-		hostConfig.CPUShares = 0
244
+		resources.CPUShares = 0
254 245
 	}
255
-	if hostConfig.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
246
+	if resources.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
256 247
 		warnings = append(warnings, "Your kernel does not support CPU cfs period. Period discarded.")
257 248
 		logrus.Warnf("Your kernel does not support CPU cfs period. Period discarded.")
258
-		hostConfig.CPUPeriod = 0
249
+		resources.CPUPeriod = 0
259 250
 	}
260
-	if hostConfig.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
251
+	if resources.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
261 252
 		warnings = append(warnings, "Your kernel does not support CPU cfs quota. Quota discarded.")
262 253
 		logrus.Warnf("Your kernel does not support CPU cfs quota. Quota discarded.")
263
-		hostConfig.CPUQuota = 0
254
+		resources.CPUQuota = 0
264 255
 	}
265
-	if (hostConfig.CpusetCpus != "" || hostConfig.CpusetMems != "") && !sysInfo.Cpuset {
256
+
257
+	// cpuset subsystem checks and adjustments
258
+	if (resources.CpusetCpus != "" || resources.CpusetMems != "") && !sysInfo.Cpuset {
266 259
 		warnings = append(warnings, "Your kernel does not support cpuset. Cpuset discarded.")
267 260
 		logrus.Warnf("Your kernel does not support cpuset. Cpuset discarded.")
268
-		hostConfig.CpusetCpus = ""
269
-		hostConfig.CpusetMems = ""
261
+		resources.CpusetCpus = ""
262
+		resources.CpusetMems = ""
270 263
 	}
271
-	cpusAvailable, err := sysInfo.IsCpusetCpusAvailable(hostConfig.CpusetCpus)
264
+	cpusAvailable, err := sysInfo.IsCpusetCpusAvailable(resources.CpusetCpus)
272 265
 	if err != nil {
273
-		return warnings, derr.ErrorCodeInvalidCpusetCpus.WithArgs(hostConfig.CpusetCpus)
266
+		return warnings, derr.ErrorCodeInvalidCpusetCpus.WithArgs(resources.CpusetCpus)
274 267
 	}
275 268
 	if !cpusAvailable {
276
-		return warnings, derr.ErrorCodeNotAvailableCpusetCpus.WithArgs(hostConfig.CpusetCpus, sysInfo.Cpus)
269
+		return warnings, derr.ErrorCodeNotAvailableCpusetCpus.WithArgs(resources.CpusetCpus, sysInfo.Cpus)
277 270
 	}
278
-	memsAvailable, err := sysInfo.IsCpusetMemsAvailable(hostConfig.CpusetMems)
271
+	memsAvailable, err := sysInfo.IsCpusetMemsAvailable(resources.CpusetMems)
279 272
 	if err != nil {
280
-		return warnings, derr.ErrorCodeInvalidCpusetMems.WithArgs(hostConfig.CpusetMems)
273
+		return warnings, derr.ErrorCodeInvalidCpusetMems.WithArgs(resources.CpusetMems)
281 274
 	}
282 275
 	if !memsAvailable {
283
-		return warnings, derr.ErrorCodeNotAvailableCpusetMems.WithArgs(hostConfig.CpusetMems, sysInfo.Mems)
276
+		return warnings, derr.ErrorCodeNotAvailableCpusetMems.WithArgs(resources.CpusetMems, sysInfo.Mems)
284 277
 	}
285
-	if hostConfig.BlkioWeight > 0 && !sysInfo.BlkioWeight {
278
+
279
+	// blkio subsystem checks and adjustments
280
+	if resources.BlkioWeight > 0 && !sysInfo.BlkioWeight {
286 281
 		warnings = append(warnings, "Your kernel does not support Block I/O weight. Weight discarded.")
287 282
 		logrus.Warnf("Your kernel does not support Block I/O weight. Weight discarded.")
288
-		hostConfig.BlkioWeight = 0
283
+		resources.BlkioWeight = 0
289 284
 	}
290
-	if hostConfig.BlkioWeight > 0 && (hostConfig.BlkioWeight < 10 || hostConfig.BlkioWeight > 1000) {
285
+	if resources.BlkioWeight > 0 && (resources.BlkioWeight < 10 || resources.BlkioWeight > 1000) {
291 286
 		return warnings, fmt.Errorf("Range of blkio weight is from 10 to 1000.")
292 287
 	}
293
-	if len(hostConfig.BlkioWeightDevice) > 0 && !sysInfo.BlkioWeightDevice {
288
+	if len(resources.BlkioWeightDevice) > 0 && !sysInfo.BlkioWeightDevice {
294 289
 		warnings = append(warnings, "Your kernel does not support Block I/O weight_device.")
295 290
 		logrus.Warnf("Your kernel does not support Block I/O weight_device. Weight-device discarded.")
296
-		hostConfig.BlkioWeightDevice = []*pblkiodev.WeightDevice{}
291
+		resources.BlkioWeightDevice = []*pblkiodev.WeightDevice{}
297 292
 	}
298
-	if len(hostConfig.BlkioDeviceReadBps) > 0 && !sysInfo.BlkioReadBpsDevice {
293
+	if len(resources.BlkioDeviceReadBps) > 0 && !sysInfo.BlkioReadBpsDevice {
299 294
 		warnings = append(warnings, "Your kernel does not support Block read limit in bytes per second.")
300 295
 		logrus.Warnf("Your kernel does not support Block I/O read limit in bytes per second. --device-read-bps discarded.")
301
-		hostConfig.BlkioDeviceReadBps = []*pblkiodev.ThrottleDevice{}
296
+		resources.BlkioDeviceReadBps = []*pblkiodev.ThrottleDevice{}
302 297
 	}
303
-	if len(hostConfig.BlkioDeviceWriteBps) > 0 && !sysInfo.BlkioWriteBpsDevice {
298
+	if len(resources.BlkioDeviceWriteBps) > 0 && !sysInfo.BlkioWriteBpsDevice {
304 299
 		warnings = append(warnings, "Your kernel does not support Block write limit in bytes per second.")
305 300
 		logrus.Warnf("Your kernel does not support Block I/O write limit in bytes per second. --device-write-bps discarded.")
306
-		hostConfig.BlkioDeviceWriteBps = []*pblkiodev.ThrottleDevice{}
301
+		resources.BlkioDeviceWriteBps = []*pblkiodev.ThrottleDevice{}
302
+	}
303
+
304
+	return warnings, nil
305
+}
306
+
307
+// verifyPlatformContainerSettings performs platform-specific validation of the
308
+// hostconfig and config structures.
309
+func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *runconfig.HostConfig, config *runconfig.Config) ([]string, error) {
310
+	warnings := []string{}
311
+	sysInfo := sysinfo.New(true)
312
+
313
+	warnings, err := daemon.verifyExperimentalContainerSettings(hostConfig, config)
314
+	if err != nil {
315
+		return warnings, err
307 316
 	}
317
+
318
+	w, err := verifyContainerResources(&hostConfig.Resources)
319
+	if err != nil {
320
+		return warnings, err
321
+	}
322
+	warnings = append(warnings, w...)
323
+
324
+	if hostConfig.ShmSize != nil && *hostConfig.ShmSize <= 0 {
325
+		return warnings, fmt.Errorf("SHM size must be greater then 0")
326
+	}
327
+
308 328
 	if hostConfig.OomKillDisable && !sysInfo.OomKillDisable {
309 329
 		hostConfig.OomKillDisable = false
310 330
 		return warnings, fmt.Errorf("Your kernel does not support oom kill disable.")