Skip to content

Commit

Permalink
add ability to use secret values in environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Jul 12, 2024
1 parent db9fec2 commit e750de7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
5 changes: 2 additions & 3 deletions charts/logprep/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ spec:
- name: REQUESTS_CA_BUNDLE
value: /home/logprep/certificates/{{ .Values.secrets.certificates.name }}
{{- end }}
{{- range $key, $value := .Values.environment }}
- name: {{ $key }}
value: {{ $value }}
{{- if .Values.environment }}
{{- toYaml .Values.environment | nindent 12 }}
{{- end }}
volumeMounts:
- name: logprep-temp
Expand Down
13 changes: 10 additions & 3 deletions charts/logprep/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ secrets: {}
# Example:
#
# environment:
# MY_ENV_VAR: "my value"
# MY_OTHER_ENV_VAR: "my other value"
# - name: MY_VAR
# value: "my value"
# - name: MY_OTHER_VAR
# value: "my other value"
# - name: SECRET_USERNAME
# valueFrom:
# secretKeyRef:
# name: backend-user
# key: backend-username
#
environment: {}
environment: []

# Boolean to signal to use affinity to avoid deploying multiple instances of the
# pod on the same node
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/charts/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,35 @@ def test_extra_secrets_are_mounted(self):
assert mount

def test_environment_variables_are_populated(self):
logprep_values = {"environment": {"MY_VAR": "my_value", "MY_OTHER_VAR": "my_other_value"}}
logprep_values = {
"environment": [
{"name": "MY_VAR", "value": "my_value"},
{"name": "MY_OTHER_VAR", "value": "my_other_value"},
]
}
self.manifests = self.render_chart("logprep", logprep_values)
env = self.deployment["spec.template.spec.containers.0.env"]
my_var = [variable for variable in env if variable["name"] == "MY_VAR"].pop()
assert my_var["value"] == "my_value"
my_var = [variable for variable in env if variable["name"] == "MY_OTHER_VAR"].pop()
assert my_var["value"] == "my_other_value"

def test_environment_variables_populated_from_secrets(self):
logprep_values = {
"environment": [
{
"name": "MY_VAR",
"value": "my_value",
},
{
"name": "MY_OTHER_VAR",
"valueFrom": {"secretKeyRef": {"name": "my-secret", "key": "my-key"}},
},
]
}
self.manifests = self.render_chart("logprep", logprep_values)
env = self.deployment["spec.template.spec.containers.0.env"]
my_var = [variable for variable in env if variable["name"] == "MY_VAR"].pop()
assert my_var["value"] == "my_value"
my_var = [variable for variable in env if variable["name"] == "MY_OTHER_VAR"].pop()
assert my_var["valueFrom"]["secretKeyRef"]["name"] == "my-secret"

0 comments on commit e750de7

Please sign in to comment.