Skip to content

Commit

Permalink
fix env substitution for docker compose v3
Browse files Browse the repository at this point in the history
  • Loading branch information
kadel committed Jun 20, 2017
1 parent ee79612 commit 0d06f9f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/loader/compose/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,25 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
"github.com/pkg/errors"
"os"
)

// converts os.Envrion ([]string) to map[string]string
// based on https://github.com/docker/cli/blob/5dd30732a23bbf14db1c64d084ae4a375f592cfa/cli/command/stack/deploy_composefile.go#L143
func buildEnvironment() (map[string]string, error) {
env := os.Environ()
result := make(map[string]string, len(env))
for _, s := range env {
// if value is empty, s is like "K=", not "K".
if !strings.Contains(s, "=") {
return result, errors.Errorf("unexpected environment %q", s)
}
kv := strings.SplitN(s, "=", 2)
result[kv[0]] = kv[1]
}
return result, nil
}

// The purpose of this is not to deploy, but to be able to parse
// v3 of Docker Compose into a suitable format. In this case, whatever is returned
// by docker/cli's ServiceConfig
Expand Down Expand Up @@ -63,12 +80,17 @@ func parseV3(files []string) (kobject.KomposeObject, error) {
Config: parsedComposeFile,
}

// get environment variables
env, err := buildEnvironment()
if err != nil {
return kobject.KomposeObject{}, errors.Wrap(err, "can't build environment")
}

// Config details
// Environment is nil as docker/cli loads the appropriate environmental values itself
configDetails := types.ConfigDetails{
WorkingDir: workingDir,
ConfigFiles: []types.ConfigFile{configFile},
Environment: nil,
Environment: env,
}

// Actual config
Expand Down
3 changes: 3 additions & 0 deletions script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ convert::expect_success "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/tes
# Test environment variables are passed correctly
convert::expect_success "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-env.yaml" "$KOMPOSE_ROOT/script/test/fixtures/v3/output-env-k8s.json"

# Test environment variables substitution
convert::expect_success "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose-env-subs.yaml" "$KOMPOSE_ROOT/script/test/fixtures/v3/output-env-subs.json"

# Test that two files that are different versions fail
convert::expect_failure "kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/v3/docker-compose.yaml -f $KOMPOSE_ROOT/script/test/fixtures/etherpad/docker-compose.yml"

Expand Down
6 changes: 6 additions & 0 deletions script/test/fixtures/v3/docker-compose-env-subs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
foo:
image: foo/bar:latest
environment:
FOO: $foo
74 changes: 74 additions & 0 deletions script/test/fixtures/v3/output-env-subs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "foo",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "foo"
}
},
"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"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "foo"
}
},
"spec": {
"containers": [
{
"name": "foo",
"image": "foo/bar:latest",
"env": [
{
"name": "FOO",
"value": "bar"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
}
]
}

0 comments on commit 0d06f9f

Please sign in to comment.