Helm 范围内的嵌套列表

问题描述 投票:0回答:1

我在

values.yaml
deployment.yaml
中有以下内容:

values.yaml

apps:
  test-app1:
    deploy: true
    containerports:
      - 8000
      - 8081
  test-app2:
    deploy: true
    containerports: 6000

deployment.yaml

{{- if .Values.apps }}
  {{- range $key, $value := .Values.apps }}
  {{- if  $value.deploy  }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $key }}
spec:
  progressDeadlineSeconds: 600
  selector:
    matchLabels:
      app: {{ $key }}
  replicas: {{ $.Values.replicas | default 1 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: {{ $key }}
    spec:
      imagePullSecrets:
        - name: secret1
      containers:
        - image: {{ $.Values.image.repository }}/{{ $key }}:latest
          name: {{ $key }}
          imagePullPolicy: IfNotPresent
         ports:
            {{- range .Values.containerports }}
            - containerPort: {{ . | title | quote }}
              {{- end }}
  {{- end }}
  {{- end }}
{{- end }}

如果范围内有多个集装箱港口,我该如何迭代?使用上面的模板,我收到的错误为:

<.Values.containerports>: nil pointer evaluating interface {}.containerports

非常感谢任何帮助。

kubernetes kubernetes-helm
1个回答
1
投票

更改 values.yaml 使

containerports
都是数组

apps:
  test-app1:
    deploy: true
    containerports:
    - 8000
    - 8081
  test-app2:
    deploy: true
    containerports:
    - 6000

否则Helm会抛出错误

range can't iterate over 6000

然后将

deployment.yaml
中的 ports 块更改为

        ports: 
        {{- range $key, $value := $value.containerports }}
        - containerPort: {{ . }}
        {{- end }}

您可能还想在资源之间添加分隔符,否则只会创建一个部署(在您的情况下为test-app2)。

整个deployment.yaml应该像这样

{{- if .Values.apps }}
  {{- range $key, $value := .Values.apps }}
  {{- if  $value.deploy  }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $key }}
spec:
  progressDeadlineSeconds: 600
  selector:
    matchLabels:
      app: {{ $key }}
  replicas: {{ $.Values.replicas | default 1 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: {{ $key }}
    spec:
      containers:
      - image: {{ $.Values.image.repository }}/{{ $key }}:latest
        name: {{ $key }}
        imagePullPolicy: IfNotPresent
        ports: 
        {{- range $key, $value := $value.containerports }}
        - containerPort: {{ . }}
        {{- end }}
  {{- end }}
---
  {{- end }}
{{- end }}
© www.soinside.com 2019 - 2024. All rights reserved.