Skip to content

Commit 63e9905

Browse files
feat: add support node backed emptyDirs (#106)
1 parent 2244a55 commit 63e9905

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

charts/k8s-service/templates/_deployment_spec.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ We need this because certain sections are omitted if there are no volumes or env
7171
{{- if gt (len .Values.scratchPaths) 0 -}}
7272
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
7373
{{- end -}}
74+
{{- if gt (len .Values.emptyDirs) 0 -}}
75+
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
76+
{{- end -}}
7477
apiVersion: apps/v1
7578
kind: Deployment
7679
metadata:
@@ -307,6 +310,10 @@ spec:
307310
- name: {{ $name }}
308311
mountPath: {{ quote $value }}
309312
{{- end }}
313+
{{- range $name, $value := .Values.emptyDirs }}
314+
- name: {{ $name }}
315+
mountPath: {{ quote $value }}
316+
{{- end }}
310317
{{- /* END VOLUME MOUNT LOGIC */ -}}
311318
312319
{{- range $key, $value := .Values.sideCarContainers }}
@@ -380,6 +387,10 @@ spec:
380387
emptyDir:
381388
medium: "Memory"
382389
{{- end }}
390+
{{- range $name, $value := .Values.emptyDirs }}
391+
- name: {{ $name }}
392+
emptyDir: {}
393+
{{- end }}
383394
{{- /* END VOLUME LOGIC */ -}}
384395
385396
{{- with .Values.nodeSelector }}

charts/k8s-service/values.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ persistentVolumes: {}
465465
# example: /mnt/scratch
466466
scratchPaths: {}
467467

468+
# emptyDirs is a map of key value pairs that specifies which paths in the container should be setup as an emptyDir volume.
469+
# Under the hood each entry in the map is converted to a volume stored on whatever medium that backs the node
470+
# (disk, SSD, network storage) and mounted into the container on the path provided as the value.
471+
#
472+
# EXAMPLE:
473+
# emptyDirs:
474+
# example: /mnt/example
475+
emptyDirs: {}
476+
468477
# secrets is a map that specifies the Secret resources that should be exposed to the main application container. Each entry in
469478
# the map represents a Secret resource. The key refers to the name of the Secret that should be exposed, with the value
470479
# specifying how to expose the Secret. The value is also a map and has the following attributes:

test/k8s_service_volume_template_test.go

Lines changed: 34 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
@@ -81,3 +82,36 @@ func TestK8SServiceDeploymentAddingPersistentVolumes(t *testing.T) {
8182
assert.Equal(t, volName, volume.Name)
8283
assert.Equal(t, volClaim, volume.PersistentVolumeClaim.ClaimName)
8384
}
85+
86+
func TestK8SServiceDeploymentAddingEmptyDirs(t *testing.T) {
87+
t.Parallel()
88+
89+
volName := "empty-dir"
90+
volMountPath := "/mnt/empty"
91+
92+
deployment := renderK8SServiceDeploymentWithSetValues(
93+
t,
94+
map[string]string{
95+
fmt.Sprintf("emptyDirs.%s", volName): volMountPath,
96+
},
97+
)
98+
99+
// Verify that there is only one container
100+
renderedPodContainers := deployment.Spec.Template.Spec.Containers
101+
require.Equal(t, len(renderedPodContainers), 1)
102+
podContainer := renderedPodContainers[0]
103+
104+
// Verify that a mount has been created for the emptyDir
105+
mounts := podContainer.VolumeMounts
106+
assert.Equal(t, len(mounts), 1)
107+
mount := mounts[0]
108+
assert.Equal(t, volName, mount.Name)
109+
assert.Equal(t, volMountPath, mount.MountPath)
110+
111+
// Verify that a volume has been declared for the emptyDir
112+
volumes := deployment.Spec.Template.Spec.Volumes
113+
assert.Equal(t, len(volumes), 1)
114+
volume := volumes[0]
115+
assert.Equal(t, volName, volume.Name)
116+
assert.Empty(t, volume.EmptyDir)
117+
}

0 commit comments

Comments
 (0)