configure container limits and resources

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-04-12 15:46:18 +02:00
parent c11a458666
commit 076f15c254
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 91 additions and 18 deletions

2
go.mod
View File

@ -17,7 +17,7 @@ require (
github.com/awslabs/goformation/v4 v4.15.6
github.com/buger/goterm v1.0.0
github.com/cnabio/cnab-to-oci v0.3.1-beta1
github.com/compose-spec/compose-go v0.0.0-20210322090015-6166d06f9ce2
github.com/compose-spec/compose-go v0.0.0-20210412113016-e35895260882
github.com/containerd/console v1.0.1
github.com/containerd/containerd v1.4.3
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect

4
go.sum
View File

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

View File

@ -25,6 +25,7 @@ import (
"github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/blkiodev"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
@ -377,24 +378,32 @@ func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy {
}
func getDeployResources(s types.ServiceConfig) container.Resources {
resources := container.Resources{}
if s.Deploy == nil {
return resources
var swappiness *int64
if s.MemSwappiness != 0 {
val := int64(s.MemSwappiness)
swappiness = &val
}
resources := container.Resources{
CgroupParent: s.CgroupParent,
Memory: int64(s.MemLimit),
MemorySwap: int64(s.MemSwapLimit),
MemorySwappiness: swappiness,
MemoryReservation: int64(s.MemReservation),
CPUCount: s.CPUCount,
CPUPeriod: s.CPUPeriod,
CPUQuota: s.CPUQuota,
CPURealtimePeriod: s.CPURTPeriod,
CPURealtimeRuntime: s.CPURTRuntime,
CPUShares: s.CPUShares,
CPUPercent: int64(s.CPUS * 100),
CpusetCpus: s.CPUSet,
}
reservations := s.Deploy.Resources.Reservations
setBlkio(s.BlkioConfig, &resources)
if reservations == nil || len(reservations.Devices) == 0 {
return resources
}
for _, device := range reservations.Devices {
resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
Capabilities: [][]string{device.Capabilities},
Count: int(device.Count),
DeviceIDs: device.IDs,
Driver: device.Driver,
})
if s.Deploy != nil {
setLimits(s.Deploy.Resources.Limits, &resources)
setReservations(s.Deploy.Resources.Reservations, &resources)
}
for _, device := range s.Devices {
@ -430,6 +439,70 @@ func getDeployResources(s types.ServiceConfig) container.Resources {
return resources
}
func setReservations(reservations *types.Resource, resources *container.Resources) {
if reservations == nil {
return
}
for _, device := range reservations.Devices {
resources.DeviceRequests = append(resources.DeviceRequests, container.DeviceRequest{
Capabilities: [][]string{device.Capabilities},
Count: int(device.Count),
DeviceIDs: device.IDs,
Driver: device.Driver,
})
}
}
func setLimits(limits *types.Resource, resources *container.Resources) {
if limits == nil {
return
}
if limits.MemoryBytes != 0 {
resources.Memory = int64(limits.MemoryBytes)
}
if limits.NanoCPUs != "" {
i, _ := strconv.ParseInt(limits.NanoCPUs, 10, 64)
resources.NanoCPUs = i
}
}
func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
if blkio == nil {
return
}
resources.BlkioWeight = blkio.Weight
for _, b := range blkio.WeightDevice {
resources.BlkioWeightDevice = append(resources.BlkioWeightDevice, &blkiodev.WeightDevice{
Path: b.Path,
Weight: b.Weight,
})
}
for _, b := range blkio.DeviceReadBps {
resources.BlkioDeviceReadBps = append(resources.BlkioDeviceReadBps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: b.Rate,
})
}
for _, b := range blkio.DeviceReadIOps {
resources.BlkioDeviceReadIOps = append(resources.BlkioDeviceReadIOps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: b.Rate,
})
}
for _, b := range blkio.DeviceWriteBps {
resources.BlkioDeviceWriteBps = append(resources.BlkioDeviceWriteBps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: b.Rate,
})
}
for _, b := range blkio.DeviceWriteIOps {
resources.BlkioDeviceWriteIOps = append(resources.BlkioDeviceWriteIOps, &blkiodev.ThrottleDevice{
Path: b.Path,
Rate: b.Rate,
})
}
}
func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
ports := nat.PortSet{}
for _, p := range s.Ports {