Skip to content

Commit

Permalink
codegen: allow setting conditional environment variables (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankram authored Oct 20, 2023
1 parent 3936107 commit 4c86f64
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions changelog/v0.34.9/conditional-env.yaml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 38 additions & 1 deletion codegen/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"},
}),
)
})

Expand Down
4 changes: 4 additions & 0 deletions codegen/model/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions codegen/templates/chart/operator-deployment.yamltmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4c86f64

Please sign in to comment.