Minor cleanup

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-10-09 11:48:06 +02:00
parent cfc490fd92
commit 68d0413e36
3 changed files with 15 additions and 12 deletions

View File

@ -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{

View File

@ -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{
{

View File

@ -21,4 +21,4 @@ services:
memory: 1G
reservations:
cpus: '0.5'
memory: 0.5G
memory: 0.5G