diff --git a/changelog/v0.34.9/conditional-env.yaml b/changelog/v0.34.9/conditional-env.yaml new file mode 100644 index 000000000..8214ab694 --- /dev/null +++ b/changelog/v0.34.9/conditional-env.yaml @@ -0,0 +1,7 @@ +changelog: + - type: NEW_FEATURE + issueLink: https://github.com/solo-io/gloo-mesh-enterprise/issues/12370 + resolvesIssue: false + description: | + Add support for conditional template environment variables. + skipCI: false diff --git a/codegen/cmd_test.go b/codegen/cmd_test.go index e69084369..6b98a6f2f 100644 --- a/codegen/cmd_test.go +++ b/codegen/cmd_test.go @@ -1836,7 +1836,7 @@ roleRef: []v1.EnvVar{{Name: "FOO", ValueFrom: &v1.EnvVarSource{SecretKeyRef: &v1.SecretKeySelector{LocalObjectReference: v1.LocalObjectReference{Name: "bar"}, Key: "baz"}}}}), ) - Describe("rendering feature gate env vars", func() { + Describe("rendering template env vars", func() { var tmpDir string BeforeEach(func() { @@ -1968,6 +1968,43 @@ roleRef: {Name: "FOO", Value: "bar"}, {Name: "FEATURE_ENABLE_FOO", Value: "false"}, }), + Entry("when Env and Conditional TemplateEnvVar are specified, and condition is true", + map[string]string{"Foo": "false"}, + []v1.EnvVar{ + { + Name: "FOO", + Value: "bar", + }, + }, + []TemplateEnvVar{ + { + Condition: "$.Values.featureGates.Foo", + Name: "FEATURE_ENABLE_FOO", + Value: "{{ $.Values.featureGates.Foo | quote }}", + }, + }, + []v1.EnvVar{ + {Name: "FOO", Value: "bar"}, + {Name: "FEATURE_ENABLE_FOO", Value: "false"}, + }), + Entry("when Env and Conditional TemplateEnvVar are specified, and condition is false", + map[string]string{"Foo": "false"}, + []v1.EnvVar{ + { + Name: "FOO", + Value: "bar", + }, + }, + []TemplateEnvVar{ + { + Condition: "$.Values.featureGates.InvalidCondition", + Name: "FEATURE_ENABLE_FOO", + Value: "{{ $.Values.featureGates.Foo | quote }}", + }, + }, + []v1.EnvVar{ + {Name: "FOO", Value: "bar"}, + }), ) }) diff --git a/codegen/model/chart.go b/codegen/model/chart.go index 5d4e6ee49..66ac873d9 100644 --- a/codegen/model/chart.go +++ b/codegen/model/chart.go @@ -153,6 +153,10 @@ type ConditionalVolumeMount struct { // TemplateEnvVar corresponds to an environment variable that can use templated Helm values type TemplateEnvVar struct { + // Condition for this environment variable to be rendered + // E.g. `and (.Values.operator.customValueA) (.Values.operator.customValueB)` + Condition string + // Name of the environment variable // E.g. FOO_BAR Name string diff --git a/codegen/templates/chart/operator-deployment.yamltmpl b/codegen/templates/chart/operator-deployment.yamltmpl index 50c538937..d412df39c 100644 --- a/codegen/templates/chart/operator-deployment.yamltmpl +++ b/codegen/templates/chart/operator-deployment.yamltmpl @@ -120,8 +120,14 @@ spec: env: {{ toYaml [[ $containerVar ]].env | indent 10 }} [[- range $f := $container.TemplateEnvVars ]] +[[- if $f.Condition ]] +{{- if [[ $f.Condition ]] }} +[[- end]] - name: [[ $f.Name ]] value: [[ $f.Value ]] +[[- if $f.Condition ]] +{{- end }} +[[- end]] [[- end ]] {{- else if [[ $containerVar ]].extraEnvs }} env: