Helm for循环列表

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

Helm for loop list

问题

我想使用一个部署文件和值文件来创建多个服务的图表。

我的值文件包含所有服务的值,必须在一个部署文件中使用。

以下是我的部署文件内容

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.PA.name }}-deployment
  labels:
    app: {{ .Values.PA.name }}
spec:
  replicas: {{ .Values.PA.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.PA.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.PA.name }}
    spec:
      containers:
      - name: {{ .Values.PA.name }}
        image: {{ .Values.PA.image }}:{{ .Values.PA.tag }}
        ports:
        - containerPort: {{ .Values.PA.port }}

以下是我的值文件

PA:
    name: povisioning_adapter
    replicas: 1
    env: dev
    image: provisioning_adapter
    tag: master
    port: 8001

    service:
        protocol: TCP  
        port: 8001
        targetPort: 8001
        nodePort: 30100

SA:
    name: service_adapter
    replicas: 1
    env: dev
    image: service_adapter
    tag: master
    port: 8002

    service:
        protocol: TCP  
        port: 8002
        targetPort: 8002
        nodePort: 30200

现在我想在部署文件中迭代PA、SA等值。如何在部署文件中声明列表[PA,SA,..]并进行循环遍历呢?

英文:

I wanted to use one deployment file and value file to create charts for multiple services.

My value file has the values of all the service, that has to be used one deployment file.

below is my deployment file content

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.PA.name }}-deployment
  labels:
    app: {{ .Values.PA.name }}
spec:
  replicas: {{ .Values.PA.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.PA.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.PA.name }}
    spec:
      containers:
      - name: {{ .Values.PA.name }}
        image: {{ .Values.PA.image }}:{{ .Values.PA.tag }}
        ports:
        - containerPort: {{ .Values.PA.port }}

Below is my values file

PA:
    name: povisioning_adapter
    replicas: 1
    env: dev
    image: provisioning_adapter
    tag: master
    port: 8001

    service:
        protocol: TCP  
        port: 8001
        targetPort: 8001
        nodePort: 30100
  
SA:
    name: service_adapter
    replicas: 1
    env: dev
    image: service_adapter
    tag: master
    port: 8002

    service:
        protocol: TCP  
        port: 8002
        targetPort: 8002
        nodePort: 30200

Now I want to iterate through PA, SA values, etc. inside my deployment file.
How to declare list [PA,SA,..] and for loop through it inside deployment file?

答案1

得分: 0

你可以将这个包裹在一个 `range` 循环中:

{{- range list .Values.PA .Values.SA -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .name }}-deployment
...
{{ end -}}

如果你需要引用顶层的 `.Values`,在这个设置中,你需要通过明确引用顶层值 `$` 来“逃脱” `range` 循环的作用域。你还可能需要在模板参数中使用这个。

metadata:
  labels:
    name: {{ .name }}
{{ include "myapp.labels" $ | indent 4 }}
{{/*                      ^          */}}

你可以做类似的事情,将其分解成一个帮助模板,生成 Kubernetes 对象之一。你可以重构它以使用组件的名称而不是其特定设置;在你当前有 `.Values.PA.name` 的地方,如果你有顶层的 `$.Values` 对象并且知道名称,那么 `index $.Values "PA" "name"` 是等效的,而其中的任何部分都可以替换为变量。
英文:

You can wrap this in a range loop:

{{- range list .Values.PA .Values.SA -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .name }}-deployment
...
{{ end -}}

If you need to refer to the top-level .Values, in this setup you'd need to "escape" the range loop's scoping by explicitly referring to the top-level value $. You also might need this in a template parameter.

metadata:
  labels:
    name: {{ .name }}
{{ include "myapp.labels" $ | indent 4 }}
{{/*                      ^          */}}

You could do something similar breaking this out into a helper template that produced one of the Kubernetes objects. You may be able to restructure this to use the name of the component rather than its specific settings; where you currently have .Values.PA.name, if you have the top-level $.Values object and you know the name, then index $.Values "PA" "name" is equivalent, and any of those parts can be replaced by variables.

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

发表评论

匿名网友

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

确定