diff --git a/charts/k8s-service/templates/_deployment_spec.tpl b/charts/k8s-service/templates/_deployment_spec.tpl index a0a3d137..33f51df3 100644 --- a/charts/k8s-service/templates/_deployment_spec.tpl +++ b/charts/k8s-service/templates/_deployment_spec.tpl @@ -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 }} diff --git a/charts/k8s-service/values.yaml b/charts/k8s-service/values.yaml index 150f696b..f40486da 100644 --- a/charts/k8s-service/values.yaml +++ b/charts/k8s-service/values.yaml @@ -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 diff --git a/test/k8s_service_config_injection_template_test.go b/test/k8s_service_config_injection_template_test.go index e7c2a05f..79a1e1b3 100644 --- a/test/k8s_service_config_injection_template_test.go +++ b/test/k8s_service_config_injection_template_test.go @@ -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( @@ -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