From c09f77135927c410c88a00164192e6868676021b Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 5 Oct 2020 15:50:28 +0200 Subject: [PATCH] Use compose resource request to set ACI retrouve request, and single container resource requests. Limits should be used to allow one container in a container group to use more than the container requested resources, up to the total resources requested for the container group. Signed-off-by: Guillaume Tardif --- aci/convert/container.go | 2 +- aci/convert/convert.go | 26 +++++++++----------------- aci/convert/convert_test.go | 20 ++++++++++---------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/aci/convert/container.go b/aci/convert/container.go index 7982f6469..54135674c 100644 --- a/aci/convert/container.go +++ b/aci/convert/container.go @@ -54,7 +54,7 @@ func ContainerToComposeProject(r containers.ContainerConfig) (types.Project, err Environment: toComposeEnvs(r.Environment), Deploy: &types.DeployConfig{ Resources: types.Resources{ - Limits: &types.Resource{ + Reservations: &types.Resource{ NanoCPUs: fmt.Sprintf("%f", r.CPULimit), MemoryBytes: types.UnitBytes(r.MemLimit.Value()), }, diff --git a/aci/convert/convert.go b/aci/convert/convert.go index 236e56961..c3683d5c5 100644 --- a/aci/convert/convert.go +++ b/aci/convert/convert.go @@ -175,10 +175,6 @@ func getDNSSidecar(containers []containerinstance.Container) containerinstance.C Image: to.StringPtr(dnsSidecarImage), Command: &alpineCmd, Resources: &containerinstance.ResourceRequirements{ - Limits: &containerinstance.ResourceLimits{ - MemoryInGB: to.Float64Ptr(0.1), // "The memory requirement should be in incrememts of 0.1 GB." - CPU: to.Float64Ptr(0.01), // "The CPU requirement should be in incrememts of 0.01." - }, Requests: &containerinstance.ResourceRequests{ MemoryInGB: to.Float64Ptr(0.1), CPU: to.Float64Ptr(0.01), @@ -357,14 +353,14 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c volumes = &allVolumes } - memLimit := 1. // Default 1 Gb - var cpuLimit float64 = 1 - if s.Deploy != nil && s.Deploy.Resources.Limits != nil { - if s.Deploy.Resources.Limits.MemoryBytes != 0 { - memLimit = bytesToGb(s.Deploy.Resources.Limits.MemoryBytes) + memRequest := 1. // Default 1 Gb + var cpuRequest float64 = 1 + if s.Deploy != nil && s.Deploy.Resources.Reservations != nil { + if s.Deploy.Resources.Reservations.MemoryBytes != 0 { + memRequest = bytesToGb(s.Deploy.Resources.Reservations.MemoryBytes) } - if s.Deploy.Resources.Limits.NanoCPUs != "" { - cpuLimit, err = strconv.ParseFloat(s.Deploy.Resources.Limits.NanoCPUs, 0) + if s.Deploy.Resources.Reservations.NanoCPUs != "" { + cpuRequest, err = strconv.ParseFloat(s.Deploy.Resources.Reservations.NanoCPUs, 0) if err != nil { return containerinstance.Container{}, err } @@ -377,13 +373,9 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c Command: to.StringSlicePtr(s.Command), EnvironmentVariables: getEnvVariables(s.Environment), Resources: &containerinstance.ResourceRequirements{ - Limits: &containerinstance.ResourceLimits{ - MemoryInGB: to.Float64Ptr(memLimit), - CPU: to.Float64Ptr(cpuLimit), - }, Requests: &containerinstance.ResourceRequests{ - MemoryInGB: to.Float64Ptr(memLimit), // TODO: use the memory requests here and not limits - CPU: to.Float64Ptr(cpuLimit), // TODO: use the cpu requests here and not limits + MemoryInGB: to.Float64Ptr(memRequest), + CPU: to.Float64Ptr(cpuRequest), }, }, VolumeMounts: volumes, diff --git a/aci/convert/convert_test.go b/aci/convert/convert_test.go index 18f229b3f..8de973a59 100644 --- a/aci/convert/convert_test.go +++ b/aci/convert/convert_test.go @@ -536,7 +536,7 @@ func TestComposeContainerGroupToContainerIgnoreDomainNameWithoutPorts(t *testing assert.Assert(t, group.IPAddress == nil) } -func TestComposeContainerGroupToContainerResourceLimits(t *testing.T) { +func TestComposeContainerGroupToContainerResourceRequests(t *testing.T) { _0_1Gb := 0.1 * 1024 * 1024 * 1024 project := types.Project{ Services: []types.ServiceConfig{ @@ -545,7 +545,7 @@ func TestComposeContainerGroupToContainerResourceLimits(t *testing.T) { Image: "image1", Deploy: &types.DeployConfig{ Resources: types.Resources{ - Limits: &types.Resource{ + Reservations: &types.Resource{ NanoCPUs: "0.1", MemoryBytes: types.UnitBytes(_0_1Gb), }, @@ -558,12 +558,12 @@ func TestComposeContainerGroupToContainerResourceLimits(t *testing.T) { group, err := ToContainerGroup(context.TODO(), convertCtx, project, mockStorageHelper) assert.NilError(t, err) - limits := *((*group.Containers)[0]).Resources.Limits - assert.Equal(t, *limits.CPU, float64(0.1)) - assert.Equal(t, *limits.MemoryInGB, float64(0.1)) + request := *((*group.Containers)[0]).Resources.Requests + assert.Equal(t, *request.CPU, float64(0.1)) + assert.Equal(t, *request.MemoryInGB, float64(0.1)) } -func TestComposeContainerGroupToContainerResourceLimitsDefaults(t *testing.T) { +func TestComposeContainerGroupToContainerResourceRequestsDefaults(t *testing.T) { project := types.Project{ Services: []types.ServiceConfig{ { @@ -571,7 +571,7 @@ func TestComposeContainerGroupToContainerResourceLimitsDefaults(t *testing.T) { Image: "image1", Deploy: &types.DeployConfig{ Resources: types.Resources{ - Limits: &types.Resource{ + Reservations: &types.Resource{ NanoCPUs: "", MemoryBytes: 0, }, @@ -584,9 +584,9 @@ func TestComposeContainerGroupToContainerResourceLimitsDefaults(t *testing.T) { group, err := ToContainerGroup(context.TODO(), convertCtx, project, mockStorageHelper) assert.NilError(t, err) - limits := *((*group.Containers)[0]).Resources.Limits - assert.Equal(t, *limits.CPU, float64(1)) - assert.Equal(t, *limits.MemoryInGB, float64(1)) + request := *((*group.Containers)[0]).Resources.Requests + assert.Equal(t, *request.CPU, float64(1)) + assert.Equal(t, *request.MemoryInGB, float64(1)) } func TestComposeContainerGroupToContainerenvVar(t *testing.T) {