From 0ef015d3daad258452b0fbf8ac9f3b2659cdc5fa Mon Sep 17 00:00:00 2001 From: Hang Yan Date: Thu, 8 Feb 2018 15:12:16 +0800 Subject: [PATCH] Fix unset env bug for v3 compose --- pkg/loader/compose/v3.go | 13 ++- script/test/cmd/tests.sh | 8 ++ .../fixtures/v3/docker-compose-unset-env.yaml | 8 ++ .../fixtures/v3/output-unset-env-k8s.json | 86 +++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 script/test/fixtures/v3/docker-compose-unset-env.yaml create mode 100644 script/test/fixtures/v3/output-unset-env-k8s.json diff --git a/pkg/loader/compose/v3.go b/pkg/loader/compose/v3.go index e9068ba5a..9ba0c928c 100644 --- a/pkg/loader/compose/v3.go +++ b/pkg/loader/compose/v3.go @@ -319,8 +319,19 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose // Gather the environment values // DockerCompose uses map[string]*string while we use []string // So let's convert that using this hack + // Note: unset env pick up the env value on host if exist for name, value := range composeServiceConfig.Environment { - env := kobject.EnvVar{Name: name, Value: *value} + var env kobject.EnvVar + if value != nil { + env = kobject.EnvVar{Name: name, Value: *value} + } else { + result, ok := os.LookupEnv(name) + if ok { + env = kobject.EnvVar{Name: name, Value: result} + } else { + continue + } + } serviceConfig.Environment = append(serviceConfig.Environment, env) } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index f6cb3f242..44591008e 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -466,6 +466,14 @@ sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/f convert::expect_success "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-env.yaml" "/tmp/output-k8s.json" + +# Test unset environment variables are passed correctly +export V3_HOST_ENV_TEST_SET_TO_BAR=BAR +cmd="kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-unset-env.yaml" +sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/v3/output-unset-env-k8s.json > /tmp/output-k8s.json +convert::expect_success "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-unset-env.yaml" "/tmp/output-k8s.json" + + # Test environment variables substitution cmd="kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-env-subs.yaml" sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/v3/output-env-subs.json > /tmp/output-k8s.json diff --git a/script/test/fixtures/v3/docker-compose-unset-env.yaml b/script/test/fixtures/v3/docker-compose-unset-env.yaml new file mode 100644 index 000000000..e04348948 --- /dev/null +++ b/script/test/fixtures/v3/docker-compose-unset-env.yaml @@ -0,0 +1,8 @@ +version: '3' +services: + foo: + image: foo/bar:latest + environment: + - BAR + - V3_HOST_ENV_TEST_SET_TO_BAR + - FOO=foo diff --git a/script/test/fixtures/v3/output-unset-env-k8s.json b/script/test/fixtures/v3/output-unset-env-k8s.json new file mode 100644 index 000000000..3a0f20ee3 --- /dev/null +++ b/script/test/fixtures/v3/output-unset-env-k8s.json @@ -0,0 +1,86 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "ports": [ + { + "name": "headless", + "port": 55555, + "targetPort": 0 + } + ], + "selector": { + "io.kompose.service": "foo" + }, + "clusterIP": "None" + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Deployment", + "apiVersion": "extensions/v1beta1", + "metadata": { + "name": "foo", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "foo" + } + }, + "spec": { + "containers": [ + { + "name": "foo", + "image": "foo/bar:latest", + "env": [ + { + "name": "V3_HOST_ENV_TEST_SET_TO_BAR", + "value": "BAR" + }, + { + "name": "FOO", + "value": "foo" + } + ], + "resources": {} + } + ], + "restartPolicy": "Always" + } + }, + "strategy": {} + }, + "status": {} + } + ] +}