Skip to content

Commit

Permalink
Merge pull request #80 from watchtowerai/master
Browse files Browse the repository at this point in the history
Add support for mounting persistent volumes
  • Loading branch information
yorinasub17 authored Oct 27, 2020
2 parents 2f48105 + dad3646 commit 8286cd3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions charts/k8s-service/templates/canarydeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ We need this because certain sections are omitted if there are no volumes or env
{{- fail printf "configMaps config has unknown type: %s" (index . "as") -}}
{{- end -}}
{{- end -}}
{{- if gt (len .Values.persistentVolumes) 0 -}}
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
{{- end -}}
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down Expand Up @@ -252,6 +255,10 @@ spec:
mountPath: {{ quote $value.mountPath }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.persistentVolumes }}
- name: {{ $name }}
mountPath: {{ quote $value.mountPath }}
{{- end }}
{{- /* END VOLUME MOUNT LOGIC */ -}}

{{- range $key, $value := .Values.sideCarContainers }}
Expand Down Expand Up @@ -316,6 +323,11 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.persistentVolumes }}
- name: {{ $name }}
persistentVolumeClaim:
claimName: {{ $value.claimName }}
{{- end }}
{{- /* END VOLUME LOGIC */ -}}

{{- with .Values.nodeSelector }}
Expand Down
12 changes: 12 additions & 0 deletions charts/k8s-service/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ We need this because certain sections are omitted if there are no volumes or env
{{- fail printf "configMaps config has unknown type: %s" (index . "as") -}}
{{- end -}}
{{- end -}}
{{- if gt (len .Values.persistentVolumes) 0 -}}
{{- $_ := set $hasInjectionTypes "hasVolume" true -}}
{{- end -}}
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down Expand Up @@ -252,6 +255,10 @@ spec:
mountPath: {{ quote $value.mountPath }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.persistentVolumes }}
- name: {{ $name }}
mountPath: {{ quote $value.mountPath }}
{{- end }}
{{- /* END VOLUME MOUNT LOGIC */ -}}

{{- range $key, $value := .Values.sideCarContainers }}
Expand Down Expand Up @@ -316,6 +323,11 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- range $name, $value := .Values.persistentVolumes }}
- name: {{ $name }}
persistentVolumeClaim:
claimName: {{ $value.claimName }}
{{- end }}
{{- /* END VOLUME LOGIC */ -}}

{{- with .Values.nodeSelector }}
Expand Down
20 changes: 20 additions & 0 deletions charts/k8s-service/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ additionalContainerEnv: {}
# envVarName: CONFIG_FOO
configMaps: {}

# persistentVolumes is a map that specifies PeristantVolumes that should be mounted on the pod. Each entry represents a
# persistent volume which should already exist within your cluster. They Key is the name of the persistent volume.
# The value is also a map and has the following attributes:
# - mountPath (string) (required)
# : The path within the container upon which this volume should be mounted.
# - claimName (string) (required)
# : The name of the Persistent Volume Claim on which this Persistent Volume in bound.
#
# EXAMPLE:
# persistentVolumes:
# example-pv:
# mountPath: /mnt/myVol
# claimName: example-pv-claim
# example-pv-2:
# mountPath: /mnt/myOtherVol
# claimName: example-pv2-claim
#
#
persistentVolumes: {}

# 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
33 changes: 33 additions & 0 deletions test/k8s_service_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,36 @@ func TestK8SServicePodAddingAdditionalLabels(t *testing.T) {
assert.Equal(t, deployment.Spec.Template.Labels["second-label"], second_custom_pod_label_value)
}

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

volName := "pv-1"
volClaim := "claim-1"
volMountPath := "/mnt/path/1"

deployment := renderK8SServiceDeploymentWithSetValues(
t,
map[string]string{
"persistentVolumes.pv-1.claimName": volClaim,
"persistentVolumes.pv-1.mountPath": volMountPath,
},
)

// Verify that there is only one container and that the environments section is populated.
renderedPodContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(renderedPodContainers), 1)

// Verify that a mount has been created for the PV
mounts := renderedPodContainers[0].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 PV
volumes := deployment.Spec.Template.Spec.Volumes
assert.Equal(t, len(volumes), 1)
volume := volumes[0]
assert.Equal(t, volName, volume.Name)
assert.Equal(t, volClaim, volume.PersistentVolumeClaim.ClaimName)
}

0 comments on commit 8286cd3

Please sign in to comment.