Skip to content

Commit

Permalink
Fix unset env bug for v3 compose
Browse files Browse the repository at this point in the history
  • Loading branch information
hangyan committed Feb 8, 2018
1 parent ed9246d commit 0ef015d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/loader/compose/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions script/test/fixtures/v3/docker-compose-unset-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'
services:
foo:
image: foo/bar:latest
environment:
- BAR
- V3_HOST_ENV_TEST_SET_TO_BAR
- FOO=foo
86 changes: 86 additions & 0 deletions script/test/fixtures/v3/output-unset-env-k8s.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
]
}

0 comments on commit 0ef015d

Please sign in to comment.