Helm for循环列表

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

Helm for loop list

问题

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

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

以下是我的部署文件内容

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: {{ .Values.PA.name }}-deployment
  5. labels:
  6. app: {{ .Values.PA.name }}
  7. spec:
  8. replicas: {{ .Values.PA.replicas }}
  9. selector:
  10. matchLabels:
  11. app: {{ .Values.PA.name }}
  12. template:
  13. metadata:
  14. labels:
  15. app: {{ .Values.PA.name }}
  16. spec:
  17. containers:
  18. - name: {{ .Values.PA.name }}
  19. image: {{ .Values.PA.image }}:{{ .Values.PA.tag }}
  20. ports:
  21. - containerPort: {{ .Values.PA.port }}

以下是我的值文件

  1. PA:
  2. name: povisioning_adapter
  3. replicas: 1
  4. env: dev
  5. image: provisioning_adapter
  6. tag: master
  7. port: 8001
  8. service:
  9. protocol: TCP
  10. port: 8001
  11. targetPort: 8001
  12. nodePort: 30100
  13. SA:
  14. name: service_adapter
  15. replicas: 1
  16. env: dev
  17. image: service_adapter
  18. tag: master
  19. port: 8002
  20. service:
  21. protocol: TCP
  22. port: 8002
  23. targetPort: 8002
  24. 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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: {{ .Values.PA.name }}-deployment
  5. labels:
  6. app: {{ .Values.PA.name }}
  7. spec:
  8. replicas: {{ .Values.PA.replicas }}
  9. selector:
  10. matchLabels:
  11. app: {{ .Values.PA.name }}
  12. template:
  13. metadata:
  14. labels:
  15. app: {{ .Values.PA.name }}
  16. spec:
  17. containers:
  18. - name: {{ .Values.PA.name }}
  19. image: {{ .Values.PA.image }}:{{ .Values.PA.tag }}
  20. ports:
  21. - containerPort: {{ .Values.PA.port }}

Below is my values file

  1. PA:
  2. name: povisioning_adapter
  3. replicas: 1
  4. env: dev
  5. image: provisioning_adapter
  6. tag: master
  7. port: 8001
  8. service:
  9. protocol: TCP
  10. port: 8001
  11. targetPort: 8001
  12. nodePort: 30100
  13. SA:
  14. name: service_adapter
  15. replicas: 1
  16. env: dev
  17. image: service_adapter
  18. tag: master
  19. port: 8002
  20. service:
  21. protocol: TCP
  22. port: 8002
  23. targetPort: 8002
  24. 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

  1. 你可以将这个包裹在一个 `range` 循环中:
  2. {{- range list .Values.PA .Values.SA -}}
  3. ---
  4. apiVersion: apps/v1
  5. kind: Deployment
  6. metadata:
  7. name: {{ .name }}-deployment
  8. ...
  9. {{ end -}}
  10. 如果你需要引用顶层的 `.Values`,在这个设置中,你需要通过明确引用顶层值 `$` 来“逃脱” `range` 循环的作用域。你还可能需要在模板参数中使用这个。
  11. metadata:
  12. labels:
  13. name: {{ .name }}
  14. {{ include "myapp.labels" $ | indent 4 }}
  15. {{/* ^ */}}
  16. 你可以做类似的事情,将其分解成一个帮助模板,生成 Kubernetes 对象之一。你可以重构它以使用组件的名称而不是其特定设置;在你当前有 `.Values.PA.name` 的地方,如果你有顶层的 `$.Values` 对象并且知道名称,那么 `index $.Values "PA" "name"` 是等效的,而其中的任何部分都可以替换为变量。
英文:

You can wrap this in a range loop:

  1. {{- range list .Values.PA .Values.SA -}}
  2. ---
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. name: {{ .name }}-deployment
  7. ...
  8. {{ 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.

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

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:

确定