Просмотр исходного кода

Fix resource limit defaults and make aci e2e tests pass

Guillaume Tardif 5 лет назад
Родитель
Сommit
82ff8dcd7d
2 измененных файлов с 68 добавлено и 3 удалено
  1. 7 3
      azure/convert/convert.go
  2. 61 0
      azure/convert/convert_test.go

+ 7 - 3
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

+ 61 - 0
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))