Helm 图表嵌套循环

Helm charts nested loops

正在尝试使用此模板为我的 Helm 图表生成部署

{{- range .Values.services }}
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-{{ . }}
spec:
  replicas: {{ .replicaCount }}
  template:
    metadata:
      labels:
        app: myapp-{{ . }}
        chart: myapp-{{ $.Values.cluster }}-{{ $.Values.environment }}
    spec:
      containers:
      - name: myapp-{{ . }}
        image: {{ $.Values.containerRegistry }}/myapp-{{ . }}:latest
        ports:
        - containerPort: {{ .targetPort }}
        env:
  {{- with .environmentVariables }}
  {{ indent 10 }}
  {{- end }}
      imagePullSecrets:
       - name: myregistry
{{- end }}

我的 2 项服务。在 values.yaml 我得到了

environment: dev

cluster: sandbox

ingress:
  enabled: true

containerRegistry: myapp.io

services:
- backend:
    port: 80
    targetPort: 8080
    replicaCount: 1
    environmentVariables:
      - name: SOME_VAR
        value: "hello"
- web:
    port: 80
    targetPort: 8080
    replicaCount: 1
    environmentVariables:
      - name: SOME_VAR
        value: "hello"

...但输出格式不正确

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-map[backend:map[replicaCount:1 targetPort:8080 environmentVariables:[map[name:SOME_VAR value:hello] port:80]]

而不是

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-web
(...)

和另一个配置

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-backend
(...)

我可以使用哪些函数或一些不同的数据结构? None 个引用(即 .environmentVariables 正常工作)

我认为你应该重新考虑数据的结构方式,这样会更好:

  services:
  - name: backend
    settings:
      port: 80
      targetPort: 8080
      replicaCount: 1
      environmentVariables:
        - name: SOME_VAR
          value: "hello"

  - name: web
    settings:
      port: 80
      targetPort: 8080
      replicaCount: 1
      environmentVariables:
        - name: SOME_VAR
          value: "hello"

你的 Deployment 看起来像这样:

{{- range .Values.services }}
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-{{ .name }}
spec:
  replicas: {{ .settings.replicaCount }}
  template:
    metadata:
      labels:
        app: myapp-{{ .name }}
    spec:
      containers:
      - name: myapp-{{ .name }}
        image: {{ $.Values.containerRegistry }}/myapp-{{ .name }}:latest
        ports:
        - containerPort: {{ .settings.targetPort }}
        env:
  {{- with .settings.environmentVariables }}
  {{ toYaml . | trim | indent 6 }}
  {{- end }}
      imagePullSecrets:
       - name: myregistry
{{- end }}

实际上会创建两个部署,通过添加 --- 分隔符。