Skip to content

Commit cd1f8a3

Browse files
authored
Add support for subPath in secrets volumes (#149)
* Add support for subPath in secrets volumes * [skip ci] Fix code comments
1 parent 134eb6b commit cd1f8a3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

charts/k8s-service/templates/_deployment_spec.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ spec:
317317
{{- if eq $value.as "volume" }}
318318
- name: {{ $name }}-volume
319319
mountPath: {{ quote $value.mountPath }}
320+
{{- if $value.subPath }}
321+
subPath: {{ quote $value.subPath }}
322+
{{- end }}
320323
{{- end }}
321324
{{- end }}
322325
{{- range $name, $value := .Values.persistentVolumes }}

charts/k8s-service/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ emptyDirs: {}
559559
# : For Secrets mounted as a volume, specify the mount path on the container file system where the secrets will be
560560
# available. Required when the Secret is exposed as a volume. Ignored when the Secret is exposed as environment
561561
# variables.
562+
# - subPath (string)
563+
# : For Secrets mounted as a volume, specify the sub path on the volume system where the secret values will be
564+
# available. Optional when the Secret is exposed as a volume. Ignored when the Secret is exposed as
565+
# environment variables.
562566
# - items (map[SecretItem])
563567
# : Specify how each Secret value should be made available. The keys are the key of the Secret that you wish to
564568
# configure, while the value is another map that controls how that key should be exposed. Required when the Secret

test/k8s_service_config_injection_template_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build all || tpl
12
// +build all tpl
23

34
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
@@ -194,6 +195,44 @@ func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountWithoutSubPathToPod(t
194195
assert.Empty(t, volumeMount.SubPath)
195196
}
196197

198+
// Test that setting the `secrets` input value with volume include the volume mount for the secret
199+
// We test by injecting to secrets:
200+
// secrets:
201+
// dbsettings:
202+
// as: volume
203+
// mountPath: /etc/db
204+
func TestK8SServiceVolumeSecretAddsVolumeAndVolumeMountWithoutSubPathToPod(t *testing.T) {
205+
t.Parallel()
206+
207+
deployment := renderK8SServiceDeploymentWithSetValues(
208+
t,
209+
map[string]string{
210+
"secrets.dbsettings.as": "volume",
211+
"secrets.dbsettings.mountPath": "/etc/db",
212+
},
213+
)
214+
215+
// Verify that there is only one container and only one volume
216+
renderedPodContainers := deployment.Spec.Template.Spec.Containers
217+
require.Equal(t, len(renderedPodContainers), 1)
218+
appContainer := renderedPodContainers[0]
219+
renderedPodVolumes := deployment.Spec.Template.Spec.Volumes
220+
require.Equal(t, len(renderedPodVolumes), 1)
221+
podVolume := renderedPodVolumes[0]
222+
223+
// Check that the pod volume is a secret volume
224+
assert.Equal(t, podVolume.Name, "dbsettings-volume")
225+
require.NotNil(t, podVolume.Secret)
226+
assert.Equal(t, podVolume.Secret.SecretName, "dbsettings")
227+
228+
// Check that the pod volume will be mounted
229+
require.Equal(t, len(appContainer.VolumeMounts), 1)
230+
volumeMount := appContainer.VolumeMounts[0]
231+
assert.Equal(t, volumeMount.Name, "dbsettings-volume")
232+
assert.Equal(t, volumeMount.MountPath, "/etc/db")
233+
assert.Empty(t, volumeMount.SubPath)
234+
}
235+
197236
// Test that setting the `configMaps` input value with volume include the volume mount and subpath for the config map
198237
// We test by injecting to configMaps:
199238
// configMaps:
@@ -234,6 +273,46 @@ func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountWithSubPathToPod(t *te
234273
assert.Equal(t, volumeMount.SubPath, "host.txt")
235274
}
236275

276+
// Test that setting the `secrets` input value with volume include the volume mount and subpath for the secret
277+
// We test by injecting to secrets:
278+
// secrets:
279+
// dbsettings:
280+
// as: volume
281+
// mountPath: /etc/db/host.txt
282+
// subPath: host.xt
283+
func TestK8SServiceVolumeSecretAddsVolumeAndVolumeMountWithSubPathToPod(t *testing.T) {
284+
t.Parallel()
285+
286+
deployment := renderK8SServiceDeploymentWithSetValues(
287+
t,
288+
map[string]string{
289+
"secrets.dbsettings.as": "volume",
290+
"secrets.dbsettings.mountPath": "/etc/db/host.txt",
291+
"secrets.dbsettings.subPath": "host.txt",
292+
},
293+
)
294+
295+
// Verify that there is only one container and only one volume
296+
renderedPodContainers := deployment.Spec.Template.Spec.Containers
297+
require.Equal(t, len(renderedPodContainers), 1)
298+
appContainer := renderedPodContainers[0]
299+
renderedPodVolumes := deployment.Spec.Template.Spec.Volumes
300+
require.Equal(t, len(renderedPodVolumes), 1)
301+
podVolume := renderedPodVolumes[0]
302+
303+
// Check that the pod volume is a secret volume
304+
assert.Equal(t, podVolume.Name, "dbsettings-volume")
305+
require.NotNil(t, podVolume.Secret)
306+
assert.Equal(t, podVolume.Secret.SecretName, "dbsettings")
307+
308+
// Check that the pod volume will be mounted
309+
require.Equal(t, len(appContainer.VolumeMounts), 1)
310+
volumeMount := appContainer.VolumeMounts[0]
311+
assert.Equal(t, volumeMount.Name, "dbsettings-volume")
312+
assert.Equal(t, volumeMount.MountPath, "/etc/db/host.txt")
313+
assert.Equal(t, volumeMount.SubPath, "host.txt")
314+
}
315+
237316
// Test that setting the `configMaps` input value with volume and individual file mount paths will set the appropriate
238317
// settings
239318
// We test by injecting to configMaps:

0 commit comments

Comments
 (0)