diff --git a/aci/convert/convert.go b/aci/convert/convert.go index 324e72c5f..754c0d61d 100644 --- a/aci/convert/convert.go +++ b/aci/convert/convert.go @@ -444,6 +444,10 @@ func bytesToGb(b types.UnitBytes) float64 { return math.Round(f*100) / 100 } +func gbToBytes(memInBytes float64) uint64 { + return uint64(memInBytes * 1024 * 1024 * 1024) +} + // ContainerGroupToServiceStatus convert from an ACI container definition to service status func ContainerGroupToServiceStatus(containerID string, group containerinstance.ContainerGroup, container containerinstance.Container, region string) compose.ServiceStatus { var replicas = 1 @@ -469,14 +473,14 @@ func fqdn(group containerinstance.ContainerGroup, region string) string { // ContainerGroupToContainer composes a Container from an ACI container definition func ContainerGroupToContainer(containerID string, cg containerinstance.ContainerGroup, cc containerinstance.Container, region string) containers.Container { - memLimits := 0. - memRequest := 0. + memLimits := uint64(0) + memRequest := uint64(0) cpuLimit := 0. cpuReservation := 0. if cc.Resources != nil { if cc.Resources.Limits != nil { if cc.Resources.Limits.MemoryInGB != nil { - memLimits = *cc.Resources.Limits.MemoryInGB * 1024 * 1024 * 1024 + memLimits = gbToBytes(*cc.Resources.Limits.MemoryInGB) } if cc.Resources.Limits.CPU != nil { cpuLimit = *cc.Resources.Limits.CPU @@ -484,7 +488,7 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe } if cc.Resources.Requests != nil { if cc.Resources.Requests.MemoryInGB != nil { - memRequest = *cc.Resources.Requests.MemoryInGB * 1024 * 1024 * 1024 + memRequest = gbToBytes(*cc.Resources.Requests.MemoryInGB) } if cc.Resources.Requests.CPU != nil { cpuReservation = *cc.Resources.Requests.CPU @@ -515,8 +519,8 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe hostConfig := &containers.HostConfig{ CPULimit: cpuLimit, CPUReservation: cpuReservation, - MemoryLimit: uint64(memLimits), - MemoryReservation: uint64(memRequest), + MemoryLimit: memLimits, + MemoryReservation: memRequest, RestartPolicy: toContainerRestartPolicy(cg.RestartPolicy), } c := containers.Container{ diff --git a/aci/convert/convert_test.go b/aci/convert/convert_test.go index f45fe6a7e..c2beecb5b 100644 --- a/aci/convert/convert_test.go +++ b/aci/convert/convert_test.go @@ -111,8 +111,8 @@ func TestContainerGroupToContainer(t *testing.T) { HostConfig: &containers.HostConfig{ CPULimit: 3, CPUReservation: 2, - MemoryLimit: 214748364, - MemoryReservation: 107374182, + MemoryLimit: gbToBytes(0.2), + MemoryReservation: gbToBytes(0.1), RestartPolicy: "any", }, } @@ -544,8 +544,9 @@ func TestComposeContainerGroupToContainerIgnoreDomainNameWithoutPorts(t *testing assert.Assert(t, group.IPAddress == nil) } +var _0_1Gb = gbToBytes(0.1) + func TestComposeContainerGroupToContainerResourceRequests(t *testing.T) { - _0_1Gb := 0.1 * 1024 * 1024 * 1024 project := types.Project{ Services: []types.ServiceConfig{ { @@ -575,7 +576,6 @@ func TestComposeContainerGroupToContainerResourceRequests(t *testing.T) { } func TestComposeContainerGroupToContainerResourceRequestsAndLimits(t *testing.T) { - _0_1Gb := 0.1 * 1024 * 1024 * 1024 project := types.Project{ Services: []types.ServiceConfig{ { @@ -609,7 +609,6 @@ func TestComposeContainerGroupToContainerResourceRequestsAndLimits(t *testing.T) } func TestComposeContainerGroupToContainerResourceLimitsOnly(t *testing.T) { - _0_1Gb := 0.1 * 1024 * 1024 * 1024 project := types.Project{ Services: []types.ServiceConfig{ { diff --git a/tests/composefiles/aci-demo/aci_demo_port_resources.yaml b/tests/composefiles/aci-demo/aci_demo_port_resources.yaml index 8735121d4..7f76be39e 100644 --- a/tests/composefiles/aci-demo/aci_demo_port_resources.yaml +++ b/tests/composefiles/aci-demo/aci_demo_port_resources.yaml @@ -21,4 +21,4 @@ services: memory: 1G reservations: cpus: '0.5' - memory: 0.5G \ No newline at end of file + memory: 0.5G