Browse Source

Support environment variables defined in compose file:
* KEY=VALUE
* KEY (no =VALUE) will fetch the process environment variable

envFile list is already parsed by compose-go and resolved to Environment key values no need to do anything for this.

Guillaume Tardif 5 years ago
parent
commit
0c27fd6236
2 changed files with 45 additions and 1 deletions
  1. 19 1
      azure/convert/convert.go
  2. 26 0
      azure/convert/convert_test.go

+ 19 - 1
azure/convert/convert.go

@@ -22,6 +22,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"math"
+	"os"
 	"strconv"
 	"strings"
 
@@ -280,7 +281,8 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c
 	return containerinstance.Container{
 		Name: to.StringPtr(s.Name),
 		ContainerProperties: &containerinstance.ContainerProperties{
-			Image: to.StringPtr(s.Image),
+			Image:                to.StringPtr(s.Image),
+			EnvironmentVariables: getEnvVariables(s.Environment),
 			Resources: &containerinstance.ResourceRequirements{
 				Limits: &containerinstance.ResourceLimits{
 					MemoryInGB: to.Float64Ptr(memLimit),
@@ -294,7 +296,23 @@ func (s serviceConfigAciHelper) getAciContainer(volumesCache map[string]bool) (c
 			VolumeMounts: volumes,
 		},
 	}, nil
+}
 
+func getEnvVariables(composeEnv types.MappingWithEquals) *[]containerinstance.EnvironmentVariable {
+	result := []containerinstance.EnvironmentVariable{}
+	for key, value := range composeEnv {
+		var strValue string
+		if value == nil {
+			strValue = os.Getenv(key)
+		} else {
+			strValue = *value
+		}
+		result = append(result, containerinstance.EnvironmentVariable{
+			Name:  to.StringPtr(key),
+			Value: to.StringPtr(strValue),
+		})
+	}
+	return &result
 }
 
 func bytesToGb(b types.UnitBytes) float64 {

+ 26 - 0
azure/convert/convert_test.go

@@ -17,6 +17,7 @@
 package convert
 
 import (
+	"os"
 	"testing"
 
 	"github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
@@ -260,6 +261,31 @@ func (suite *ConvertTestSuite) TestComposeContainerGroupToContainerResourceLimit
 	Expect(*limits.MemoryInGB).To(Equal(float64(1)))
 }
 
+func (suite *ConvertTestSuite) TestComposeContainerGroupToContainerenvVar() {
+	os.Setenv("key2", "value2")
+	project := types.Project{
+		Services: []types.ServiceConfig{
+			{
+				Name:  "service1",
+				Image: "image1",
+				Environment: types.MappingWithEquals{
+					"key1": to.StringPtr("value1"),
+					"key2": nil,
+				},
+			},
+		},
+	}
+
+	group, err := ToContainerGroup(suite.ctx, project)
+	Expect(err).To(BeNil())
+
+	container1 := (*group.Containers)[0]
+	envVars := *container1.EnvironmentVariables
+	Expect(len(envVars)).To(Equal(2))
+	Expect(envVars).To(ContainElement(containerinstance.EnvironmentVariable{Name: to.StringPtr("key1"), Value: to.StringPtr("value1")}))
+	Expect(envVars).To(ContainElement(containerinstance.EnvironmentVariable{Name: to.StringPtr("key2"), Value: to.StringPtr("value2")}))
+}
+
 func TestConvertTestSuite(t *testing.T) {
 	RegisterTestingT(t)
 	suite.Run(t, new(ConvertTestSuite))