如何评估范围内的字段?

huangapple go评论79阅读模式
英文:

How can evaluate field in range?

问题

我正在尝试为许多类似的应用程序创建一个默认模板,我需要在两个或多个 pod 之间共享相同的 PVC,并且需要修改图表以创建或不创建 PVC(如果已存在)。

这是我在 values.yml 中关于卷的部分:

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

这是我的 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 }}

我想在 volumes 的范围内添加 create 字段来管理 PVC 的创建(假设在此示例中,PVC vol2 已经存在于其他 Helm Chart 中)。

如果可能的话,我希望 Helm 能够读取范围内的 create 字段,这样我就不会得到错误信息:evaluate field create in type interface {}

如果你有其他想法,欢迎分享,谢谢!

英文:

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!

答案1

得分: 1

volumes是一个数组,它没有create字段。

volumes的元素才有该字段。因此,.Values.volumes.create没有任何意义。在range内,您可以使用.create来检查元素的create字段,例如:

{{- range .Values.volumes }}
    {{if .create}}在这里执行某些操作{{end}}
{{- end}}
英文:

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}}

huangapple
  • 本文由 发表于 2023年2月15日 20:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75459662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定