Template for persistent volumes on helm charts

Over and over happens to reinvent the wheel and write some specific configuration related to volume management while creating helm charts. This is due to the fact that helm chart template doesn’t create any settings relative to the persistent volumes.

To avoid doing that in the future, I am adding here a small recipe

Values.yaml

 1persistence:
 2  storageClass: ""
 3  existingClaim: ""
 4  enabled: true
 5  accessMode: ReadWriteOnce
 6  size: 800Mi
 7  # if you need any additional volumes, you can define them here
 8  additionalVolumes: []
 9  # if you need any additional volume mounts, you can define them here
10  additionalMounts: []

Deployment.yaml

… volumes: - name: config {{- if .Values.persistence.enabled }} persistentVolumeClaim: claimName: {{ .Values.persistence.existingClaim | default (include “home-assistant.fullname” .) }} {{- else }} emptyDir: { } {{- end }} {{- if .Values.persistence.additionalVolumes }} {{- .Values.persistence.additionalVolumes | toYaml | nindent 8}} {{- end }} …

and in the container spec

1...
2          volumeMounts:
3           - mountPath: /config
4             name: config
5           {{- if .Values.persistence.additionalMounts }}
6             {{- .Values.persistence.additionalMounts | toYaml | nindent 12 }}
7           {{- end }}
8           ...

pvc.yaml

{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }} kind: PersistentVolumeClaim apiVersion: v1 metadata: name: {{ include “home-assistant.fullname” . }} labels: {{- include “home-assistant.labels” . | nindent 4 }} annotations: {{- if .Values.persistence.storageClass }} volume.beta.kubernetes.io/storage-class: {{ .Values.persistence.storageClass | quote }} {{- else }} volume.alpha.kubernetes.io/storage-class: default {{- end }} spec: accessModes: - {{ .Values.persistence.accessMode | quote }} resources: requests: storage: {{ .Values.persistence.size | quote }} {{- end }}

Copyright

Comments