forked from LaconicNetwork/kompose
Merge pull request #926 from hangyan/fix-v3-unset-env
Fix unset env bug for v3 compose
This commit is contained in:
commit
acf09d8d3a
@ -319,8 +319,19 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
|
|||||||
// Gather the environment values
|
// Gather the environment values
|
||||||
// DockerCompose uses map[string]*string while we use []string
|
// DockerCompose uses map[string]*string while we use []string
|
||||||
// So let's convert that using this hack
|
// 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 {
|
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)
|
serviceConfig.Environment = append(serviceConfig.Environment, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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"
|
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
|
# Test environment variables substitution
|
||||||
cmd="kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-env-subs.yaml"
|
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
|
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
|
||||||
|
|||||||
8
script/test/fixtures/v3/docker-compose-unset-env.yaml
vendored
Normal file
8
script/test/fixtures/v3/docker-compose-unset-env.yaml
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
foo:
|
||||||
|
image: foo/bar:latest
|
||||||
|
environment:
|
||||||
|
- BAR
|
||||||
|
- V3_HOST_ENV_TEST_SET_TO_BAR
|
||||||
|
- FOO=foo
|
||||||
86
script/test/fixtures/v3/output-unset-env-k8s.json
vendored
Normal file
86
script/test/fixtures/v3/output-unset-env-k8s.json
vendored
Normal file
@ -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": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user