Skip to content

Commit cb199b7

Browse files
author
Richard Lucas
authored
adding envFrom for deployments (#101)
1 parent 80ce781 commit cb199b7

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

charts/k8s-service/templates/_deployment_spec.tpl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ We need this because certain sections are omitted if there are no volumes or env
4343
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
4444
{{- else if eq (index . "as") "environment" -}}
4545
{{- $_ := set $hasInjectionTypes "hasEnvVars" true -}}
46+
{{- else if eq (index . "as") "envFrom" }}
47+
{{- $_ := set $hasInjectionTypes "hasEnvFrom" true -}}
4648
{{- else if eq (index . "as") "none" -}}
4749
{{- /* noop */ -}}
4850
{{- else -}}
@@ -55,6 +57,8 @@ We need this because certain sections are omitted if there are no volumes or env
5557
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
5658
{{- else if eq (index . "as") "environment" -}}
5759
{{- $_ := set $hasInjectionTypes "hasEnvVars" true -}}
60+
{{- else if eq (index . "as") "envFrom" }}
61+
{{- $_ := set $hasInjectionTypes "hasEnvFrom" true -}}
5862
{{- else if eq (index . "as") "none" -}}
5963
{{- /* noop */ -}}
6064
{{- else -}}
@@ -261,8 +265,24 @@ spec:
261265
{{- end }}
262266
{{- end }}
263267
{{- end }}
268+
{{- if index $hasInjectionTypes "hasEnvFrom" }}
269+
envFrom:
270+
{{- range $name, $value := .Values.configMaps }}
271+
{{- if eq $value.as "envFrom" }}
272+
- configMapRef:
273+
name: {{ $name }}
274+
{{- end }}
275+
{{- end }}
276+
{{- range $name, $value := .Values.secrets }}
277+
{{- if eq $value.as "envFrom" }}
278+
- secretRef:
279+
name: {{ $name }}
280+
{{- end }}
281+
{{- end }}
282+
{{- end }}
264283
{{- /* END ENV VAR LOGIC */ -}}
265284
285+
266286
{{- /* START VOLUME MOUNT LOGIC */ -}}
267287
{{- if index $hasInjectionTypes "hasVolume" }}
268288
volumeMounts:

charts/k8s-service/values.yaml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ additionalContainerEnv: {}
371371
# configMaps is a map that specifies the ConfigMap resources that should be exposed to the main application container. Each
372372
# entry in the map represents a ConfigMap resource. The key refers to the name of the ConfigMap that should be exposed,
373373
# with the value specifying how to expose the ConfigMap. The value is also a map and has the following attributes:
374-
# - as (enum[volume,environment,none]) (required)
374+
# - as (enum[volume,environment,envFrom,none]) (required)
375375
# : ConfigMaps can be exposed to Pods as a volume mount, or as environment variables. This attribute is a string
376-
# enum that is expected to be either "volume" or "environment", specifying that the ConfigMap should be exposed as a
377-
# mounted volume or via environment variables respectively. This attribute can also be set to "none", which
378-
# disables the `ConfigMap` on the container.
376+
# enum that is expected to be either "volume", "environment", or "envFrom", specifying that the ConfigMap should
377+
# be exposed as a mounted volume, via environment variables, or loaded as environment variables respectively.
378+
# This attribute can also be set to "none", which disables the `ConfigMap` on the container.
379379
# - mountPath (string)
380380
# : For ConfigMaps mounted as a volume, specify the mount path on the container file system where the config values
381381
# will be available. Required when the ConfigMap is exposed as a volume. Ignored when the ConfigMap is exposed as
@@ -400,7 +400,8 @@ additionalContainerEnv: {}
400400
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
401401
#
402402
# The following example exposes the ConfigMap `myconfig` as a volume mounted to `/etc/myconfig`, while it exposes the
403-
# ConfigMap `myotherconfig` as an environment variable.
403+
# ConfigMap `myotherconfig` as an environment variable. Additionally, it automatically mounts all of the keys
404+
# `anotherconfig` as environment variables using the `envFrom` keyword.
404405
#
405406
# EXAMPLE:
406407
#
@@ -413,9 +414,11 @@ additionalContainerEnv: {}
413414
# items:
414415
# foo:
415416
# envVarName: CONFIG_FOO
417+
# anotherconfig:
418+
# as: envFrom
416419
configMaps: {}
417420

418-
# persistentVolumes is a map that specifies PeristantVolumes that should be mounted on the pod. Each entry represents a
421+
# persistentVolumes is a map that specifies PersistentVolumes that should be mounted on the pod. Each entry represents a
419422
# persistent volume which should already exist within your cluster. They Key is the name of the persistent volume.
420423
# The value is also a map and has the following attributes:
421424
# - mountPath (string) (required)
@@ -447,11 +450,11 @@ scratchPaths: {}
447450
# secrets is a map that specifies the Secret resources that should be exposed to the main application container. Each entry in
448451
# the map represents a Secret resource. The key refers to the name of the Secret that should be exposed, with the value
449452
# specifying how to expose the Secret. The value is also a map and has the following attributes:
450-
# - as (enum[volume,environment,none]) (required)
453+
# - as (enum[volume,environment,envFrom,none]) (required)
451454
# : Secrets can be exposed to Pods as a volume mount, or as environment variables. This attribute is a string enum
452-
# that is expected to be either "volume" or "environment", specifying that the Secret should be exposed as a mounted
453-
# volume or via environment variables respectively. This attribute can also be set to "none", which disables the
454-
# `Secret` on the container.
455+
# that is expected to be either "volume", "environment", or "envFrom", specifying that the Secret should be
456+
# exposed as a mounted volume, via environment variables, or loaded in its entirety as environment variables
457+
# respectively. This attribute can also be set to "none", which disables the `Secret` on the container.
455458
# - mountPath (string)
456459
# : For Secrets mounted as a volume, specify the mount path on the container file system where the secrets will be
457460
# available. Required when the Secret is exposed as a volume. Ignored when the Secret is exposed as environment
@@ -476,7 +479,8 @@ scratchPaths: {}
476479
# https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets
477480
#
478481
# The following example exposes the Secret `mysecret` as a volume mounted to `/etc/mysecret`, while it exposes the
479-
# Secret `myothersecret` as an environment variable.
482+
# Secret `myothersecret` as an environment variable. Additionally, it automatically mounts all of the keys
483+
# `anothersecret` as environment variables using the `envFrom` keyword.
480484
#
481485
# EXAMPLE:
482486
#
@@ -489,9 +493,10 @@ scratchPaths: {}
489493
# items:
490494
# foo:
491495
# envVarName: SECRET_FOO
496+
# anothersecret:
497+
# as: envFrom
492498
secrets: {}
493499

494-
495500
# containerResources specifies the amount of resources the application container will require. Only specify if you have
496501
# specific resource needs.
497502
# NOTE: This variable is injected directly into the pod spec. See the official documentation for what this might look

test/k8s_service_template_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,46 @@ func TestK8SServiceFullnameOverride(t *testing.T) {
794794

795795
assert.Equal(t, deployment.Name, overiddenName)
796796
}
797+
798+
func TestK8SServiceEnvFrom(t *testing.T) {
799+
t.Parallel()
800+
801+
t.Run("BothConfigMapsAndSecretsEnvFrom", func(t *testing.T) {
802+
deployment := renderK8SServiceDeploymentWithSetValues(t,
803+
map[string]string{
804+
"configMaps.test-configmap.as": "envFrom",
805+
"secrets.test-secret.as": "envFrom",
806+
},
807+
)
808+
809+
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
810+
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 2)
811+
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name, "test-configmap")
812+
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[1].SecretRef.Name, "test-secret")
813+
})
814+
815+
t.Run("OnlyConfigMapsEnvFrom", func(t *testing.T) {
816+
deployment := renderK8SServiceDeploymentWithSetValues(t,
817+
map[string]string{
818+
"configMaps.test-configmap.as": "envFrom",
819+
},
820+
)
821+
822+
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
823+
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 1)
824+
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].ConfigMapRef.Name, "test-configmap")
825+
})
826+
827+
t.Run("OnlySecretsEnvFrom", func(t *testing.T) {
828+
deployment := renderK8SServiceDeploymentWithSetValues(t,
829+
map[string]string{
830+
"secrets.test-secret.as": "envFrom",
831+
},
832+
)
833+
834+
assert.NotNil(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom)
835+
assert.Equal(t, len(deployment.Spec.Template.Spec.Containers[0].EnvFrom), 1)
836+
assert.Equal(t, deployment.Spec.Template.Spec.Containers[0].EnvFrom[0].SecretRef.Name, "test-secret")
837+
})
838+
839+
}

0 commit comments

Comments
 (0)