使用 helm chart 中的范围迭代复杂的 values.yaml

iterate though complex values.yaml using range in helm chart

我是新来掌舵世界的人。我想从我的价值观和 sample.yaml 生成清单,所以 我试图从我的 sample.yaml 文件创建模板。

Values.yaml

prodapps:
 alpha:
   name: alpha
   image: alpha.azurecr.io/alpha:latest
   imagePullPolicy: IfNotPresent
   resources:
     requests:
       memory: "10000Mi"
       cpu: "150m"
     limits:
       memory: "800Mi"
       cpu: 600m"
   ingress:
     enabled: true
     annotations:
       zap.ingress.kubernetes.io/rule-type: PathPrefixStrip
       nginx.ingress.kubernetes.io/rewrite-target: /
     path: /alpha/api
     hosts:
       - demo.jumboapps.com
 beta:
   name: beta
   disable: true
   image: beta.azurecr.io/beta:latest
   imagePullPolicy: IfNotPresent
   resources:
     requests:
       memory: "10000Mi"
       cpu: "150m"
     limits:
       memory: "800Mi"
       cpu: 600m"
   ingress:
     enabled: true
     annotations:
       zap.ingress.kubernetes.io/rule-type: PathPrefixStrip
       nginx.ingress.kubernetes.io/rewrite-target: /
     path: /beta/api
     hosts:
       - demo.jumboapps.com

Sample.yaml

apiVersion: v1
kind: Service
metadata:
 name: "{{ .Values.prodapps.alpha.name }}-svc"
 namespace: "{{ .Release.Namespace }}"
spec:
 ports:
   - port: 80
     name: "http"
 selector:
   app: "{{ .Values.prodapps.alpha.name }}"
---
apiVersion: v1
kind: Service
metadata:
 name: "{{ .Values.prodapps.beta.name }}-svc"
 namespace: "{{ .Release.Namespace }}"
spec:
 ports:
   - port: 80
     name: "http"
 selector:
   app: "{{ .Values.prodapps.beta.name }}"

我的主要目标是将我的 sample.yaml.by 模板化并生成有效的 Manifet 文件。 遵循 helm 文档。 但是我找不到任何合理的(我可以理解的)语法来遍历复杂的值文件。

请任何人帮我解决这个问题。

range的用法,k8s在申请资源的时候,如何用'---'来划分资源,你需要多了解一下。

你可以模仿以下

Values.yaml

prodapps:
  alpha:
    name: alpha
    image: alpha.azurecr.io/alpha:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "10000Mi"
        cpu: "150m"
      limits:
        memory: "800Mi"
        cpu: 600m"
  beta:
    name: beta
    disable: true
    image: beta.azurecr.io/beta:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        memory: "10000Mi"
        cpu: "150m"
      limits:
        memory: "800Mi"
        cpu: 600m"

service.yaml

{{ if .Values.prodapps }}
{{- range $k, $v := $.Values.prodapps }}
---
apiVersion: v1
kind: Service
metadata:
  name: "{{ $v.name }}-svc"
  namespace: "{{ .Release.Namespace }}"
spec:
  ports:
    - port: 80
      name: "http"
  selector:
    app: "{{ $v.name }}"
{{- end }}
{{- end }}

deployment.yaml

{{ if .Values.prodapps }}
{{- range $k, $v := $.Values.prodapps }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ $v.name }}-dep"
  namespace: "{{ .Release.Namespace }}"
spec:
  template:
    spec:
      containers:
        - name: {{ $v.Name }}
          image: "{{ $v.image }}"
          imagePullPolicy: {{ $v.imagePullPolicy }}
          resources:
            {{- toYaml $v.resources | nindent 12 }}
{{- end }}
{{- end }}