@@ -227,22 +227,23 @@ func parsePSOutput(output []byte, pids []int) (*types.ContainerProcessList, erro
227227}
228228
229229// validateConfig validates container config
230- func validateConfig (config * types.ContainerCreateConfig ) ([]string , error ) {
231- amendResource (& config .HostConfig .Resources )
232-
230+ func validateConfig (config * types.ContainerConfig , hostConfig * types.HostConfig , update bool ) ([]string , error ) {
233231 // validates container hostconfig
234232 warnings := make ([]string , 0 )
235- warns , err := validateResource (& config . HostConfig . Resources )
233+ warns , err := validateResource (& hostConfig . Resources , update )
236234 if err != nil {
237235 return nil , err
238236 }
239237 warnings = append (warnings , warns ... )
240238
239+ if hostConfig .OomScoreAdj < - 1000 || hostConfig .OomScoreAdj > 1000 {
240+ return warnings , fmt .Errorf ("oom score should be in range [-1000, 1000]" )
241+ }
241242 // TODO: add more validate here
242243 return warnings , nil
243244}
244245
245- func validateResource (r * types.Resources ) ([]string , error ) {
246+ func validateResource (r * types.Resources , update bool ) ([]string , error ) {
246247 cgroupInfo := system .NewCgroupInfo ()
247248 if cgroupInfo == nil {
248249 return nil , nil
@@ -251,125 +252,124 @@ func validateResource(r *types.Resources) ([]string, error) {
251252
252253 // validates memory cgroup value
253254 if cgroupInfo .Memory != nil {
254- if r .Memory != 0 && ! cgroupInfo .Memory .MemoryLimit {
255- warn := "Current Kernel does not support memory limit, discard --memory"
256- logrus .Warn (warn )
257- warnings = append (warnings , warn )
255+ if r .Memory > 0 && ! cgroupInfo .Memory .MemoryLimit {
256+ logrus .Warn (MemoryWarn )
257+ warnings = append (warnings , MemoryWarn )
258258 r .Memory = 0
259259 r .MemorySwap = 0
260260 }
261- if r .MemorySwap != 0 && ! cgroupInfo .Memory .MemorySwap {
262- warn := "Current Kernel does not support memory swap, discard --memory-swap"
263- logrus .Warn (warn )
264- warnings = append (warnings , warn )
261+ if r .MemorySwap > 0 && ! cgroupInfo .Memory .MemorySwap {
262+ logrus .Warn (MemorySwapWarn )
263+ warnings = append (warnings , MemorySwapWarn )
265264 r .MemorySwap = 0
266265 }
266+ if r .Memory != 0 && r .Memory < MinMemory {
267+ return warnings , fmt .Errorf ("Minimal memory should greater than 4M" )
268+ }
267269 if r .Memory > 0 && r .MemorySwap > 0 && r .MemorySwap < 2 * r .Memory {
268270 warnings = append (warnings , "You should typically size your swap space to approximately 2x main memory for systems with less than 2GB of RAM" )
269271 }
270272 if r .MemorySwappiness != nil && ! cgroupInfo .Memory .MemorySwappiness {
271- warn := "Current Kernel does not support memory swappiness , discard --memory-swappiness"
272- logrus .Warn (warn )
273- warnings = append (warnings , warn )
273+ logrus .Warn (MemorySwappinessWarn )
274+ warnings = append (warnings , MemorySwappinessWarn )
274275 r .MemorySwappiness = nil
275276 }
277+ if r .MemorySwappiness != nil && (* r .MemorySwappiness < 0 || * r .MemorySwappiness > 100 ) {
278+ return warnings , fmt .Errorf ("MemorySwappiness should in range [-1, 100]" )
279+ }
276280 if r .OomKillDisable != nil && ! cgroupInfo .Memory .OOMKillDisable {
277- warn := "Current Kernel does not support disable oom kill, discard --oom-kill-disable"
278- logrus .Warn (warn )
279- warnings = append (warnings , warn )
281+ logrus .Warn (OOMKillWarn )
282+ warnings = append (warnings , OOMKillWarn )
280283 r .OomKillDisable = nil
281284 }
282285 }
283286
284287 // validates cpu cgroup value
285288 if cgroupInfo .CPU != nil {
286289 if r .CpusetCpus != "" && ! cgroupInfo .CPU .CpusetCpus {
287- warn := "Current Kernel does not support cpuset cpus, discard --cpuset-cpus"
288- logrus .Warn (warn )
289- warnings = append (warnings , warn )
290+ logrus .Warn (CpusetCpusWarn )
291+ warnings = append (warnings , CpusetCpusWarn )
290292 r .CpusetCpus = ""
291293 }
292294 if r .CpusetMems != "" && ! cgroupInfo .CPU .CpusetMems {
293- warn := "Current Kernel does not support cpuset cpus, discard --cpuset-mems"
294- logrus .Warn (warn )
295- warnings = append (warnings , warn )
295+ logrus .Warn (CpusetMemsWarn )
296+ warnings = append (warnings , CpusetMemsWarn )
296297 r .CpusetMems = ""
297298 }
298299 if r .CPUShares > 0 && ! cgroupInfo .CPU .CPUShares {
299- warn := "Current Kernel does not support cpu shares, discard --cpu-shares"
300- logrus .Warn (warn )
301- warnings = append (warnings , warn )
300+ logrus .Warn (CPUSharesWarn )
301+ warnings = append (warnings , CPUSharesWarn )
302302 r .CPUShares = 0
303303 }
304304 if r .CPUQuota > 0 && ! cgroupInfo .CPU .CPUQuota {
305- warn := "Current Kernel does not support cpu quota, discard --cpu-quota"
306- logrus .Warn (warn )
307- warnings = append (warnings , warn )
305+ logrus .Warn (CPUQuotaWarn )
306+ warnings = append (warnings , CPUQuotaWarn )
308307 r .CPUQuota = 0
309308 }
309+ // cpu.cfs_quota_us can accept value less than 0, we allow -1 and > 1000
310+ if r .CPUQuota > 0 && r .CPUQuota < 1000 {
311+ return warnings , fmt .Errorf ("CPU cfs quota should be greater than 1ms(1000)" )
312+ }
310313 if r .CPUPeriod > 0 && ! cgroupInfo .CPU .CPUPeriod {
311- warn := "Current Kernel does not support cpu period, discard --cpu-period"
312- logrus .Warn (warn )
313- warnings = append (warnings , warn )
314+ logrus .Warn (CPUPeriodWarn )
315+ warnings = append (warnings , CPUPeriodWarn )
314316 r .CPUPeriod = 0
315317 }
318+ if r .CPUPeriod != 0 && (r .CPUPeriod < 1000 || r .CPUPeriod > 1000000 ) {
319+ return warnings , fmt .Errorf ("CPU cfs period should be in range [1000, 1000000](1ms, 1s)" )
320+ }
316321 }
317322
318323 // validates blkio cgroup value
319324 if cgroupInfo .Blkio != nil {
320325 if r .BlkioWeight > 0 && ! cgroupInfo .Blkio .BlkioWeight {
321- warn := "Current Kernel does not support blkio weight, discard --blkio-weight"
322- logrus .Warn (warn )
323- warnings = append (warnings , warn )
326+ logrus .Warn (BlkioWeightWarn )
327+ warnings = append (warnings , BlkioWeightWarn )
324328 r .BlkioWeight = 0
325329 }
326330 if len (r .BlkioWeightDevice ) > 0 && ! cgroupInfo .Blkio .BlkioWeightDevice {
327- warn := "Current Kernel does not support blkio weight device, discard --blkio-weight-device"
328- logrus .Warn (warn )
329- warnings = append (warnings , warn )
331+ logrus .Warn (BlkioWeightDeviceWarn )
332+ warnings = append (warnings , BlkioWeightDeviceWarn )
330333 r .BlkioWeightDevice = []* types.WeightDevice {}
331334 }
332335 if len (r .BlkioDeviceReadBps ) > 0 && ! cgroupInfo .Blkio .BlkioDeviceReadBps {
333- warn := "Current Kernel does not support blkio device throttle read bps, discard --device-read-bps"
334- logrus .Warn (warn )
335- warnings = append (warnings , warn )
336+ logrus .Warn (BlkioDeviceReadBpsWarn )
337+ warnings = append (warnings , BlkioDeviceReadBpsWarn )
336338 r .BlkioDeviceReadBps = []* types.ThrottleDevice {}
337339 }
338340 if len (r .BlkioDeviceWriteBps ) > 0 && ! cgroupInfo .Blkio .BlkioDeviceWriteBps {
339- warn := "Current Kernel does not support blkio device throttle write bps, discard --device-write-bps"
340- logrus .Warn (warn )
341- warnings = append (warnings , warn )
341+ logrus .Warn (BlkioDeviceWriteBpsWarn )
342+ warnings = append (warnings , BlkioDeviceWriteBpsWarn )
342343 r .BlkioDeviceWriteBps = []* types.ThrottleDevice {}
343344 }
344345 if len (r .BlkioDeviceReadIOps ) > 0 && ! cgroupInfo .Blkio .BlkioDeviceReadIOps {
345- warn := "Current Kernel does not support blkio device throttle read iops, discard --device-read-iops"
346- logrus .Warn (warn )
347- warnings = append (warnings , warn )
346+ logrus .Warn (BlkioDeviceReadIOpsWarn )
347+ warnings = append (warnings , BlkioDeviceReadIOpsWarn )
348348 r .BlkioDeviceReadIOps = []* types.ThrottleDevice {}
349349 }
350350 if len (r .BlkioDeviceWriteIOps ) > 0 && ! cgroupInfo .Blkio .BlkioDeviceWriteIOps {
351- warn := "Current Kernel does not support blkio device throttle, discard --device-write-iops"
352- logrus .Warn (warn )
353- warnings = append (warnings , warn )
351+ logrus .Warn (BlkioDeviceWriteIOpsWarn )
352+ warnings = append (warnings , BlkioDeviceWriteIOpsWarn )
354353 r .BlkioDeviceWriteIOps = []* types.ThrottleDevice {}
355354 }
356355 }
357356
358357 // validates pid cgroup value
359358 if cgroupInfo .Pids != nil {
360359 if r .PidsLimit != 0 && ! cgroupInfo .Pids .Pids {
361- warn := "Current Kernel does not support pids cgroup, discard --pids-limit"
362- logrus .Warn (warn )
363- warnings = append (warnings , warn )
360+ logrus .Warn (PidsLimitWarn )
361+ warnings = append (warnings , PidsLimitWarn )
364362 r .PidsLimit = 0
365363 }
366364 }
367365
368366 return warnings , nil
369367}
370368
371- // amendResource modify resource to correct setting.
372- func amendResource (r * types.Resources ) {
369+ // amendContainerSettings modify config settings to wanted,
370+ // it will be call before container created.
371+ func amendContainerSettings (config * types.ContainerConfig , hostConfig * types.HostConfig ) {
372+ r := & hostConfig .Resources
373373 if r .Memory > 0 && r .MemorySwap == 0 {
374374 r .MemorySwap = 2 * r .Memory
375375 }
0 commit comments