Skip to content

Commit

Permalink
Add support for subPath in configMaps volumes (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobalong79 authored Dec 6, 2021
1 parent 87a5cb7 commit 650ef1e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions charts/k8s-service/templates/_deployment_spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ spec:
{{- if eq $value.as "volume" }}
- name: {{ $name }}-volume
mountPath: {{ quote $value.mountPath }}
{{- if $value.subPath }}
subPath: {{ quote $value.subPath }}
{{- end }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.secrets }}
Expand Down
4 changes: 4 additions & 0 deletions charts/k8s-service/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ additionalContainerEnv: {}
# : For ConfigMaps mounted as a volume, specify the mount path on the container file system where the config values
# will be available. Required when the ConfigMap is exposed as a volume. Ignored when the ConfigMap is exposed as
# environment variables.
# - subPath (string)
# : For ConfigMaps mounted as a volume, specify the sub path on the volume system where the config values will be
# available. Optional when the ConfigMap is exposed as a volume. Ignored when the ConfigMap is exposed as
# environment variables.
# - items (map[ConfigMapItem])
# : Specify how each ConfigMap value should be made available. The keys are the key of the ConfigMap that you wish
# to configure, while the value is another map that controls how that key should be exposed. Required when the
Expand Down
43 changes: 42 additions & 1 deletion test/k8s_service_config_injection_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestK8SServiceEnvironmentConfigMapAddsEnvVarsToPod(t *testing.T) {
// dbsettings:
// as: volume
// mountPath: /etc/db
func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountToPod(t *testing.T) {
func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountWithoutSubPathToPod(t *testing.T) {
t.Parallel()

deployment := renderK8SServiceDeploymentWithSetValues(
Expand Down Expand Up @@ -191,6 +191,47 @@ func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountToPod(t *testing.T) {
volumeMount := appContainer.VolumeMounts[0]
assert.Equal(t, volumeMount.Name, "dbsettings-volume")
assert.Equal(t, volumeMount.MountPath, "/etc/db")
assert.Empty(t, volumeMount.SubPath)
}

// Test that setting the `configMaps` input value with volume include the volume mount and subpath for the config map
// We test by injecting to configMaps:
// configMaps:
// dbsettings:
// as: volume
// mountPath: /etc/db/host.txt
// subPath: host.xt
func TestK8SServiceVolumeConfigMapAddsVolumeAndVolumeMountWithSubPathToPod(t *testing.T) {
t.Parallel()

deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"configMaps.dbsettings.as": "volume",
"configMaps.dbsettings.mountPath": "/etc/db/host.txt",
"configMaps.dbsettings.subPath": "host.txt",
},
)

// Verify that there is only one container and only one volume
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
appContainer := renderedPodContainers[0]
renderedPodVolumes := deployment.Spec.Template.Spec.Volumes
require.Equal(t, len(renderedPodVolumes), 1)
podVolume := renderedPodVolumes[0]

// Check that the pod volume is a configmap volume
assert.Equal(t, podVolume.Name, "dbsettings-volume")
require.NotNil(t, podVolume.ConfigMap)
assert.Equal(t, podVolume.ConfigMap.Name, "dbsettings")

// Check that the pod volume will be mounted
require.Equal(t, len(appContainer.VolumeMounts), 1)
volumeMount := appContainer.VolumeMounts[0]
assert.Equal(t, volumeMount.Name, "dbsettings-volume")
assert.Equal(t, volumeMount.MountPath, "/etc/db/host.txt")
assert.Equal(t, volumeMount.SubPath, "host.txt")
}

// Test that setting the `configMaps` input value with volume and individual file mount paths will set the appropriate
Expand Down

0 comments on commit 650ef1e

Please sign in to comment.