Helm 使用范围嵌套列表

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

我在values.yaml和virtualservice.yaml中有以下内容: 我想使用上述值创建模板文件,但在 helm rendenring 模板时出现错误。

values.yaml

istio:
  enabled: true
  virtualService:
    enabled: true
    virtualServices:
      "0": 
        name: hello-app
        gateways: 
           - gateway-new
        hosts:
          - prod.abc.com
        apps:
          name: primary
          path: "/api"
          routes:
            "0":
              weight: 100
              port: 8080
              name: hello-app
            "1":
              weight: 0
              port: 8080
              name: hello-app-canary 
        "1": 
            name: hello-app-internal
            gateways: 
              - mesh
            hosts:
              - hello-app.test.prod.svc.cluster.local
            apps:
              name: internal
              path: "/api"
              routes:
                "0":
                  weight: 100
                  port: 9081
                  name: hello-app
                "1":
                  weight: 0
                  port: 9081
                  name: hello-app-canary  

virtualservice.yaml

{{- if ((.Values.istio).enabled) }}
{{- if ((.Values.istio.virtualService).enabled) }}
{{- range $key, $value := .Values.istio.virtualService.virtualServices }}
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: {{ .name }}
  namespace: {{ $.Release.Namespace }}
  labels:
{{ include "common.labels" $ | indent 4 }}
spec:
  gateways:
  {{- range .gateways }}
  - {{.}}
  {{- end }}
  hosts:
  {{- range .hosts }}
  - {{.}}
  {{- end }}
  http:
  {{- range $app := $value.apps }}
  - match:
      - uri:
          prefix: "/{{ $app.path }}"
    name: {{ $app.name }}
    route:
    {{- range $route := $app.routes }}
    - destination:
          host: {{ $route.name }}
          port:
            number: {{ $route.port  }}
      weight: {{ $route.weight  }}
    {{- end }}
    {{- end }}
{{- end }}
{{- end }}
{{- end }}

如果我有多个使用范围的列表,如何迭代?使用上面的模板,我收到错误:

 at <$app.path>: can't evaluate field path in type interface {}

非常感谢任何帮助。

kubernetes kubernetes-helm istio
1个回答
1
投票

首先,看起来虚拟服务“1”有一个额外的缩进(现在它位于虚拟服务“0”部分下,但它应该位于“virtualServices”下)。

此外,如果你想迭代每个虚拟服务的应用程序,

apps
字段应该是一个列表,但现在它是一个字典。因此,当您使用
range $app := $value.apps
进行迭代时,您实际上是在迭代
apps
的字段,这会导致您收到错误。

这是

values.yam
的固定版本:

istio:
  enabled: true
  virtualService:
    enabled: true
    virtualServices:
      "0": 
        name: hello-app
        gateways: 
           - gateway-new
        hosts:
          - prod.abc.com
        apps:
        - name: primary
          path: "/api"
          routes:
            "0":
              weight: 100
              port: 8080
              name: hello-app
            "1":
              weight: 0
              port: 8080
              name: hello-app-canary 
      "1": 
          name: hello-app-internal
          gateways: 
            - mesh
          hosts:
            - hello-app.test.prod.svc.cluster.local
          apps:
          - name: internal
            path: "/api"
            routes:
              "0":
                weight: 100
                port: 9081
                name: hello-app
              "1":
                weight: 0
                port: 9081
                name: hello-app-canary  

© www.soinside.com 2019 - 2024. All rights reserved.