Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 0623688

Browse files
authored
Merge pull request #1528 from docker/limits
configure container limits and resources
2 parents 8b245d9 + 076f15c commit 0623688

File tree

3 files changed

+94
-21
lines changed

3 files changed

+94
-21
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/awslabs/goformation/v4 v4.15.6
1818
github.com/buger/goterm v1.0.0
1919
github.com/cnabio/cnab-to-oci v0.3.1-beta1
20-
github.com/compose-spec/compose-go v0.0.0-20210322090015-6166d06f9ce2
20+
github.com/compose-spec/compose-go v0.0.0-20210412113016-e35895260882
2121
github.com/containerd/console v1.0.1
2222
github.com/containerd/containerd v1.4.3
2323
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
308308
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
309309
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
310310
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
311-
github.com/compose-spec/compose-go v0.0.0-20210322090015-6166d06f9ce2 h1:8t1noQjw1GkcYno4V93tBTR5w3RHDZre3JJpmaNWp0I=
312-
github.com/compose-spec/compose-go v0.0.0-20210322090015-6166d06f9ce2/go.mod h1:6eIT9U2OgdHmkRD6szmqatCrWWEEUSwl/j2iJYH4jLo=
311+
github.com/compose-spec/compose-go v0.0.0-20210412113016-e35895260882 h1:vLKOiHY9QPS1eiBvGfsEI7wR23B1bGNvwOF5aVl25Fs=
312+
github.com/compose-spec/compose-go v0.0.0-20210412113016-e35895260882/go.mod h1:6eIT9U2OgdHmkRD6szmqatCrWWEEUSwl/j2iJYH4jLo=
313313
github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko=
314314
github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59/go.mod h1:pA0z1pT8KYB3TCXK/ocprsh7MAkoW8bZVzPdih9snmM=
315315
github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 h1:9atoWyI9RtXFwf7UDbme/6M8Ud0rFrx+Q3ZWgSnsxtw=

local/compose/create.go

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/compose-spec/compose-go/types"
2727
moby "github.com/docker/docker/api/types"
28+
"github.com/docker/docker/api/types/blkiodev"
2829
"github.com/docker/docker/api/types/container"
2930
"github.com/docker/docker/api/types/mount"
3031
"github.com/docker/docker/api/types/network"
@@ -377,24 +378,32 @@ func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
377378
}
378379

379380
func getDeployResources(s types.ServiceConfig) container.Resources {
380-
resources := container.Resources{}
381-
if s.Deploy == nil {
382-
return resources
383-
}
384-
385-
reservations := s.Deploy.Resources.Reservations
386-
387-
if reservations == nil || len(reservations.Devices) == 0 {
388-
return resources
389-
}
390-
391-
for _, device := range reservations.Devices {
392-
resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
393-
Capabilities: [][]string{device.Capabilities},
394-
Count: int(device.Count),
395-
DeviceIDs: device.IDs,
396-
Driver: device.Driver,
397-
})
381+
var swappiness *int64
382+
if s.MemSwappiness != 0 {
383+
val := int64(s.MemSwappiness)
384+
swappiness = &val
385+
}
386+
resources := container.Resources{
387+
CgroupParent: s.CgroupParent,
388+
Memory: int64(s.MemLimit),
389+
MemorySwap: int64(s.MemSwapLimit),
390+
MemorySwappiness: swappiness,
391+
MemoryReservation: int64(s.MemReservation),
392+
CPUCount: s.CPUCount,
393+
CPUPeriod: s.CPUPeriod,
394+
CPUQuota: s.CPUQuota,
395+
CPURealtimePeriod: s.CPURTPeriod,
396+
CPURealtimeRuntime: s.CPURTRuntime,
397+
CPUShares: s.CPUShares,
398+
CPUPercent: int64(s.CPUS * 100),
399+
CpusetCpus: s.CPUSet,
400+
}
401+
402+
setBlkio(s.BlkioConfig, &resources)
403+
404+
if s.Deploy != nil {
405+
setLimits(s.Deploy.Resources.Limits, &resources)
406+
setReservations(s.Deploy.Resources.Reservations, &resources)
398407
}
399408

400409
for _, device := range s.Devices {
@@ -430,6 +439,70 @@ func getDeployResources(s types.ServiceConfig) container.Resources {
430439
return resources
431440
}
432441

442+
func setReservations(reservations *types.Resource, resources *container.Resources) {
443+
if reservations == nil {
444+
return
445+
}
446+
for _, device := range reservations.Devices {
447+
resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
448+
Capabilities: [][]string{device.Capabilities},
449+
Count: int(device.Count),
450+
DeviceIDs: device.IDs,
451+
Driver: device.Driver,
452+
})
453+
}
454+
}
455+
456+
func setLimits(limits *types.Resource, resources *container.Resources) {
457+
if limits == nil {
458+
return
459+
}
460+
if limits.MemoryBytes != 0 {
461+
resources.Memory = int64(limits.MemoryBytes)
462+
}
463+
if limits.NanoCPUs != "" {
464+
i, _ := strconv.ParseInt(limits.NanoCPUs, 10, 64)
465+
resources.NanoCPUs = i
466+
}
467+
}
468+
469+
func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
470+
if blkio == nil {
471+
return
472+
}
473+
resources.BlkioWeight = blkio.Weight
474+
for _, b := range blkio.WeightDevice {
475+
resources.BlkioWeightDevice = append(resources.BlkioWeightDevice, &blkiodev.WeightDevice{
476+
Path: b.Path,
477+
Weight: b.Weight,
478+
})
479+
}
480+
for _, b := range blkio.DeviceReadBps {
481+
resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, &blkiodev.ThrottleDevice{
482+
Path: b.Path,
483+
Rate: b.Rate,
484+
})
485+
}
486+
for _, b := range blkio.DeviceReadIOps {
487+
resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, &blkiodev.ThrottleDevice{
488+
Path: b.Path,
489+
Rate: b.Rate,
490+
})
491+
}
492+
for _, b := range blkio.DeviceWriteBps {
493+
resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, &blkiodev.ThrottleDevice{
494+
Path: b.Path,
495+
Rate: b.Rate,
496+
})
497+
}
498+
for _, b := range blkio.DeviceWriteIOps {
499+
resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, &blkiodev.ThrottleDevice{
500+
Path: b.Path,
501+
Rate: b.Rate,
502+
})
503+
}
504+
}
505+
433506
func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
434507
ports := nat.PortSet{}
435508
for _, p := range s.Ports {

0 commit comments

Comments
 (0)