Search code examples
gokubernetes-helmgo-templates

How can evaluate field in range?


I trying to create a default template for many similar applications, I need share same PVC with two or more pod and need modify the chart for create ot not PVC, if already exists.

This is my portion of values.yml about volumes:

persistence:
  enabled: true

volumeMounts:
  - name: vol1
    mountPath: /opt/vol1
  - name: vol2
    mountPath: /opt/vol2

volumes:
  - name: vol1
    create: true
    claimName: claim-vol1
    storageClassName: gp2
    accessModes: ReadWriteOnce
    storage: 1Gi
  - name: vol2
    create: false
    claimName: claim-vol2
    storageClassName: gp2
    accessModes: ReadWriteOnce
    storage: 1Gi

And this is my pvclaim.yml:

{{- if .Values.persistence.enabled }}
{{- if .Values.volumes.create }}
{{- range .Values.volumes }}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ .claimName }}
spec:
  storageClassName: {{ .storageClassName }}
  accessModes:
    - {{ .accessModes }}
  resources:
    requests:
      storage: {{ .storage }}
{{- end }}
{{- end }}
{{- end }}

I thought I'd add the field create into range of volumes to manage creations of PVC (assuming in this example that PVC vol2 already exists from other helm chart).

If it were possible I would like helm to read the create field inside the range, this way I get an error: evaluate field create in type interface {}

If you have any other ideas they are welcome, thanks!


Solution

  • volumes is an array, it does not have a create field.

    Elements of volumes have that field. So .Values.volumes.create does not make any sense. Inside the range you may check the create field of the element using .create, e.g.

    {{- range .Values.volumes }}
        {{if .create}}do something here{{end}}
    {{- end}}