From 71c31649073cd87fcdf340c37499bce94af0bf0a Mon Sep 17 00:00:00 2001 From: AhmedGrati <48932084+AhmedGrati@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:29:26 +0100 Subject: [PATCH] fix: enable compose environment variables interpolation (#1642) Signed-off-by: AhmedGrati --- pkg/loader/compose/compose.go | 2 +- script/test/cmd/tests_new.sh | 9 +++- .../docker-compose.yaml | 7 +++ .../compose-env-interpolation/output-k8s.yaml | 49 +++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 script/test/fixtures/compose-env-interpolation/docker-compose.yaml create mode 100644 script/test/fixtures/compose-env-interpolation/output-k8s.yaml diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index a406c0e4..1cbf7b04 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -156,7 +156,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) { return kobject.KomposeObject{}, err } - projectOptions, err := cli.NewProjectOptions(files, cli.WithOsEnv, cli.WithWorkingDirectory(workingDir), cli.WithInterpolation(false)) + projectOptions, err := cli.NewProjectOptions(files, cli.WithOsEnv, cli.WithWorkingDirectory(workingDir), cli.WithInterpolation(true)) if err != nil { return kobject.KomposeObject{}, errors.Wrap(err, "Unable to create compose options") } diff --git a/script/test/cmd/tests_new.sh b/script/test/cmd/tests_new.sh index 5b0a857c..037126aa 100755 --- a/script/test/cmd/tests_new.sh +++ b/script/test/cmd/tests_new.sh @@ -249,6 +249,11 @@ os_output="$KOMPOSE_ROOT/script/test/fixtures/fsgroup/output-os.yaml" convert::expect_success_and_warning "$k8s_cmd" "$k8s_output" # Test support for compose.yaml file -k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/compose-file-support convert --stdout --with-kompose-annotation=false" +k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/compose-file-support/docker-compose.yaml convert --stdout --with-kompose-annotation=false" k8s_output="$KOMPOSE_ROOT/script/test/fixtures/compose-file-support/output-k8s.yaml" -convert::expect_success "$os_cmd" "$os_output" +convert::expect_success "$k8s_cmd" "$k8s_output" + +# Test support for compose env interpolation +k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/compose-env-interpolation/docker-compose.yaml convert --stdout --with-kompose-annotation=false" +k8s_output="$KOMPOSE_ROOT/script/test/fixtures/compose-env-interpolation/output-k8s.yaml" +convert::expect_success "$k8s_cmd" "$k8s_output" diff --git a/script/test/fixtures/compose-env-interpolation/docker-compose.yaml b/script/test/fixtures/compose-env-interpolation/docker-compose.yaml new file mode 100644 index 00000000..b098afdb --- /dev/null +++ b/script/test/fixtures/compose-env-interpolation/docker-compose.yaml @@ -0,0 +1,7 @@ +version: '3.8' + +services: + foo: + labels: + kompose.image-pull-policy: "${IMAGE_PULL_POLICY:-IfNotPresent}" + build: . diff --git a/script/test/fixtures/compose-env-interpolation/output-k8s.yaml b/script/test/fixtures/compose-env-interpolation/output-k8s.yaml new file mode 100644 index 00000000..109ea022 --- /dev/null +++ b/script/test/fixtures/compose-env-interpolation/output-k8s.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + kompose.image-pull-policy: IfNotPresent + creationTimestamp: null + labels: + io.kompose.service: foo + name: foo +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: foo + strategy: {} + template: + metadata: + annotations: + kompose.image-pull-policy: IfNotPresent + creationTimestamp: null + labels: + io.kompose.network/compose-env-interpolation-default: "true" + io.kompose.service: foo + spec: + containers: + - image: foo + imagePullPolicy: IfNotPresent + name: foo + resources: {} + restartPolicy: Always +status: {} + +--- +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + creationTimestamp: null + name: compose-env-interpolation-default +spec: + ingress: + - from: + - podSelector: + matchLabels: + io.kompose.network/compose-env-interpolation-default: "true" + podSelector: + matchLabels: + io.kompose.network/compose-env-interpolation-default: "true" +