From a2673466f362a5a627554249b9e9ebe13d488588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Paes=20de=20Barros=20do=20Carmo?= Date: Wed, 6 Nov 2024 15:21:03 +0000 Subject: [PATCH 1/2] feat(no-interpolate): add no-interpolate feature option to the cli --- cmd/convert.go | 5 +++ pkg/app/app.go | 2 +- pkg/kobject/kobject.go | 1 + pkg/loader/compose/compose.go | 4 +- pkg/loader/loader.go | 2 +- script/test/cmd/tests.sh | 6 +++ .../compose-env-no-interpolation/compose.yaml | 10 +++++ .../output-k8s.yaml | 43 +++++++++++++++++++ 8 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 script/test/fixtures/compose-env-no-interpolation/compose.yaml create mode 100644 script/test/fixtures/compose-env-no-interpolation/output-k8s.yaml diff --git a/cmd/convert.go b/cmd/convert.go index 17993b2e9..50fd2b137 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -62,6 +62,9 @@ var ( // default is true. WithKomposeAnnotation bool + // NoInterpolation decides if we will interpolate environment variables in the compose file. + NoInterpolate bool + // MultipleContainerMode which enables creating multi containers in a single pod is a developing function. // default is false MultipleContainerMode bool @@ -121,6 +124,7 @@ var convertCmd = &cobra.Command{ YAMLIndent: ConvertYAMLIndent, Profiles: ConvertProfiles, WithKomposeAnnotation: WithKomposeAnnotation, + NoInterpolate: NoInterpolate, MultipleContainerMode: MultipleContainerMode, ServiceGroupMode: ServiceGroupMode, ServiceGroupName: ServiceGroupName, @@ -202,6 +206,7 @@ func init() { convertCmd.Flags().BoolVar(&GenerateNetworkPolicies, "generate-network-policies", false, "Specify whether to generate network policies or not") convertCmd.Flags().BoolVar(&WithKomposeAnnotation, "with-kompose-annotation", true, "Add kompose annotations to generated resource") + convertCmd.Flags().BoolVar(&NoInterpolate, "no-interpolate", false, "Disable environment variable interpolation in the compose file") // Deprecated commands convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs") diff --git a/pkg/app/app.go b/pkg/app/app.go index ce0c99fc1..64f1ba13a 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -218,7 +218,7 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) { komposeObject := kobject.KomposeObject{ ServiceConfigs: make(map[string]kobject.ServiceConfig), } - komposeObject, err = l.LoadFile(opt.InputFiles, opt.Profiles) + komposeObject, err = l.LoadFile(opt.InputFiles, opt.Profiles, opt.NoInterpolate) if err != nil { log.Fatalf(err.Error()) } diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 7d43ad018..de57d3630 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -92,6 +92,7 @@ type ConvertOptions struct { ServiceGroupName string SecretsAsFiles bool GenerateNetworkPolicies bool + NoInterpolate bool } // IsPodController indicate if the user want to use a controller diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index 1eeccc48a..796999c64 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -151,7 +151,7 @@ func checkUnsupportedKey(composeProject *types.Project) []string { } // LoadFile loads a compose file into KomposeObject -func (c *Compose) LoadFile(files []string, profiles []string) (kobject.KomposeObject, error) { +func (c *Compose) LoadFile(files []string, profiles []string, noInterpolate bool) (kobject.KomposeObject, error) { // Gather the working directory workingDir, err := transformer.GetComposeFileDir(files) if err != nil { @@ -161,7 +161,7 @@ func (c *Compose) LoadFile(files []string, profiles []string) (kobject.KomposeOb projectOptions, err := cli.NewProjectOptions( files, cli.WithOsEnv, cli.WithWorkingDirectory(workingDir), - cli.WithInterpolation(true), + cli.WithInterpolation(!noInterpolate), cli.WithProfiles(profiles), ) if err != nil { diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index def6f3783..a1458cef0 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -25,7 +25,7 @@ import ( // Loader interface defines loader that loads files and converts it to kobject representation type Loader interface { - LoadFile(files []string, profiles []string) (kobject.KomposeObject, error) + LoadFile(files []string, profiles []string, noInterpolate bool) (kobject.KomposeObject, error) ///Name() string } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index 9220fbc10..4b509db08 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -276,6 +276,12 @@ k8s_output="$KOMPOSE_ROOT/script/test/fixtures/compose-env-interpolation/output- convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 convert::expect_success "$os_cmd" "$os_output" || exit 1 +# Test support for compose env without interpolation +k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/compose-env-no-interpolation/compose.yaml convert --stdout --no-interpolate --with-kompose-annotation=false" +k8s_output="$KOMPOSE_ROOT/script/test/fixtures/compose-env-no-interpolation/output-k8s.yaml" +convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 +convert::expect_success "$os_cmd" "$os_output" || exit 1 + # Test support for subpath volume k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/vols-subpath/compose.yaml convert --stdout --with-kompose-annotation=false" k8s_output="$KOMPOSE_ROOT/script/test/fixtures/vols-subpath/output-k8s.yaml" diff --git a/script/test/fixtures/compose-env-no-interpolation/compose.yaml b/script/test/fixtures/compose-env-no-interpolation/compose.yaml new file mode 100644 index 000000000..7bc9b0f16 --- /dev/null +++ b/script/test/fixtures/compose-env-no-interpolation/compose.yaml @@ -0,0 +1,10 @@ +version: '3.8' + +services: + foo: + image: ${DOCKER_REGISTRY-}foo:${IMAGE_TAG:-latest} + build: . + environment: + - VERSION=${IMAGE_TAG:-latest} + ports: + - 80:80 diff --git a/script/test/fixtures/compose-env-no-interpolation/output-k8s.yaml b/script/test/fixtures/compose-env-no-interpolation/output-k8s.yaml new file mode 100644 index 000000000..757b39cf9 --- /dev/null +++ b/script/test/fixtures/compose-env-no-interpolation/output-k8s.yaml @@ -0,0 +1,43 @@ +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: foo + name: foo +spec: + ports: + - name: "80" + port: 80 + targetPort: 80 + selector: + io.kompose.service: foo + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: foo + name: foo +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: foo + template: + metadata: + labels: + io.kompose.service: foo + spec: + containers: + - env: + - name: VERSION + value: ${IMAGE_TAG:-latest} + image: ${DOCKER_REGISTRY-}foo:${IMAGE_TAG:-latest} + name: foo + ports: + - containerPort: 80 + protocol: TCP + restartPolicy: Always + From b354989559016a6f0cc618738f313b9ab547bb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Paes=20de=20Barros=20do=20Carmo?= Date: Wed, 13 Nov 2024 02:00:15 +0000 Subject: [PATCH 2/2] chore: improving the description for the no-interpolate argument --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index 50fd2b137..e54893c65 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -206,7 +206,7 @@ func init() { convertCmd.Flags().BoolVar(&GenerateNetworkPolicies, "generate-network-policies", false, "Specify whether to generate network policies or not") convertCmd.Flags().BoolVar(&WithKomposeAnnotation, "with-kompose-annotation", true, "Add kompose annotations to generated resource") - convertCmd.Flags().BoolVar(&NoInterpolate, "no-interpolate", false, "Disable environment variable interpolation in the compose file") + convertCmd.Flags().BoolVar(&NoInterpolate, "no-interpolate", false, "Keep environment variable names in the Compose file") // Deprecated commands convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs")