Skip to content

Commit

Permalink
feat: add support node backed emptyDirs (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnymatts-global authored Sep 7, 2021
1 parent 2244a55 commit 63e9905
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions charts/k8s-service/templates/_deployment_spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ We need this because certain sections are omitted if there are no volumes or env
{{- if gt (len .Values.scratchPaths) 0 -}}
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
{{- end -}}
{{- if gt (len .Values.emptyDirs) 0 -}}
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
{{- end -}}
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down Expand Up @@ -307,6 +310,10 @@ spec:
- name: {{ $name }}
mountPath: {{ quote $value }}
{{- end }}
{{- range $name, $value := .Values.emptyDirs }}
- name: {{ $name }}
mountPath: {{ quote $value }}
{{- end }}
{{- /* END VOLUME MOUNT LOGIC */ -}}
{{- range $key, $value := .Values.sideCarContainers }}
Expand Down Expand Up @@ -380,6 +387,10 @@ spec:
emptyDir:
medium: "Memory"
{{- end }}
{{- range $name, $value := .Values.emptyDirs }}
- name: {{ $name }}
emptyDir: {}
{{- end }}
{{- /* END VOLUME LOGIC */ -}}
{{- with .Values.nodeSelector }}
Expand Down
9 changes: 9 additions & 0 deletions charts/k8s-service/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@ persistentVolumes: {}
# example: /mnt/scratch
scratchPaths: {}

# emptyDirs is a map of key value pairs that specifies which paths in the container should be setup as an emptyDir volume.
# Under the hood each entry in the map is converted to a volume stored on whatever medium that backs the node
# (disk, SSD, network storage) and mounted into the container on the path provided as the value.
#
# EXAMPLE:
# emptyDirs:
# example: /mnt/example
emptyDirs: {}

# secrets is a map that specifies the Secret resources that should be exposed to the main application container. Each entry in
# the map represents a Secret resource. The key refers to the name of the Secret that should be exposed, with the value
# specifying how to expose the Secret. The value is also a map and has the following attributes:
Expand Down
34 changes: 34 additions & 0 deletions test/k8s_service_volume_template_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build all || tpl
// +build all tpl

// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
Expand Down Expand Up @@ -81,3 +82,36 @@ func TestK8SServiceDeploymentAddingPersistentVolumes(t *testing.T) {
assert.Equal(t, volName, volume.Name)
assert.Equal(t, volClaim, volume.PersistentVolumeClaim.ClaimName)
}

func TestK8SServiceDeploymentAddingEmptyDirs(t *testing.T) {
t.Parallel()

volName := "empty-dir"
volMountPath := "/mnt/empty"

deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
fmt.Sprintf("emptyDirs.%s", volName): volMountPath,
},
)

// Verify that there is only one container
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)
podContainer := renderedPodContainers[0]

// Verify that a mount has been created for the emptyDir
mounts := podContainer.VolumeMounts
assert.Equal(t, len(mounts), 1)
mount := mounts[0]
assert.Equal(t, volName, mount.Name)
assert.Equal(t, volMountPath, mount.MountPath)

// Verify that a volume has been declared for the emptyDir
volumes := deployment.Spec.Template.Spec.Volumes
assert.Equal(t, len(volumes), 1)
volume := volumes[0]
assert.Equal(t, volName, volume.Name)
assert.Empty(t, volume.EmptyDir)
}

0 comments on commit 63e9905

Please sign in to comment.