From 82ff8dcd7d1f8921d69cf6a3d6ee602dab2b91d1 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Tue, 30 Jun 2020 07:44:45 +0200 Subject: [PATCH] Fix resource limit defaults and make aci e2e tests pass --- azure/convert/convert.go | 10 ++++-- azure/convert/convert_test.go | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/azure/convert/convert.go b/azure/convert/convert.go index a2dae725..447e59b0 100644 --- a/azure/convert/convert.go +++ b/azure/convert/convert.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io/ioutil" + "math" "strconv" "strings" @@ -267,7 +268,9 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c memLimit := 1. // Default 1 Gb var cpuLimit float64 = 1 if s.Deploy != nil && s.Deploy.Resources.Limits != nil { - memLimit = float64(bytesToGb(s.Deploy.Resources.Limits.MemoryBytes)) + if s.Deploy.Resources.Limits.MemoryBytes != 0 { + memLimit = bytesToGb(s.Deploy.Resources.Limits.MemoryBytes) + } if s.Deploy.Resources.Limits.NanoCPUs != "" { cpuLimit, err = strconv.ParseFloat(s.Deploy.Resources.Limits.NanoCPUs, 0) if err != nil { @@ -295,8 +298,9 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c } -func bytesToGb(b types.UnitBytes) int64 { - return int64(b) / 1024 / 1024 / 1024 // from bytes to gigabytes +func bytesToGb(b types.UnitBytes) float64 { + f := float64(b) / 1024 / 1024 / 1024 // from bytes to gigabytes + return math.Round(f*100) / 100 } // ContainerGroupToContainer composes a Container from an ACI container definition diff --git a/azure/convert/convert_test.go b/azure/convert/convert_test.go index d9f067ec..4953ce2f 100644 --- a/azure/convert/convert_test.go +++ b/azure/convert/convert_test.go @@ -213,6 +213,67 @@ func (suite *ConvertTestSuite) TestComposeContainerGroupToContainerMultiplePorts Expect(*groupPorts[1].Port).To(Equal(int32(8080))) } +func (suite *ConvertTestSuite) TestComposeContainerGroupToContainerResourceLimits() { + _0_1Gb := 0.1 * 1024 * 1024 * 1024 + project := compose.Project{ + Name: "", + Config: types.Config{ + Services: []types.ServiceConfig{ + { + Name: "service1", + Image: "image1", + Deploy: &types.DeployConfig{ + Resources: types.Resources{ + Limits: &types.Resource{ + NanoCPUs: "0.1", + MemoryBytes: types.UnitBytes(_0_1Gb), + }, + }, + }, + }, + }, + }, + } + + group, err := ToContainerGroup(suite.ctx, project) + Expect(err).To(BeNil()) + + container1 := (*group.Containers)[0] + limits := *container1.Resources.Limits + Expect(*limits.CPU).To(Equal(float64(0.1))) + Expect(*limits.MemoryInGB).To(Equal(float64(0.1))) +} + +func (suite *ConvertTestSuite) TestComposeContainerGroupToContainerResourceLimitsDefaults() { + project := compose.Project{ + Name: "", + Config: types.Config{ + Services: []types.ServiceConfig{ + { + Name: "service1", + Image: "image1", + Deploy: &types.DeployConfig{ + Resources: types.Resources{ + Limits: &types.Resource{ + NanoCPUs: "", + MemoryBytes: 0, + }, + }, + }, + }, + }, + }, + } + + group, err := ToContainerGroup(suite.ctx, project) + Expect(err).To(BeNil()) + + container1 := (*group.Containers)[0] + limits := *container1.Resources.Limits + Expect(*limits.CPU).To(Equal(float64(1))) + Expect(*limits.MemoryInGB).To(Equal(float64(1))) +} + func TestConvertTestSuite(t *testing.T) { RegisterTestingT(t) suite.Run(t, new(ConvertTestSuite))