使用值文件创建具有不同名称的ConfigMap的Helm命令

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

Helm create configmap with different names from values file

问题

请注意,你的模板文件似乎有一些语法错误,特别是在循环和条件语句中。以下是你期望的输出的修正模板:

  1. {{- range $topic, $values := $.Values.topicsToRead }}
  2. {{- if $values.multiPod }}
  3. {{- range $values.region }}
  4. ---
  5. apiVersion: v1
  6. kind: ConfigMap
  7. metadata:
  8. name: cm-{{ . }}
  9. data:
  10. features.toml: |
  11. [features]
  12. x_enabled = false
  13. {{- end }}
  14. {{- end }}
  15. {{- end }}

你可以使用修正后的模板文件运行Helm命令来获得期望的输出。如果multiPodtrue,则它将创建基于region的多个ConfigMap;如果为false,则只会创建一个名为cm-storage的ConfigMap。

英文:

I'm trying to create configmaps with different names based on the multiPod variable in the values file. If the multiPod variable is set to True, then helm should create 3 CM based on the region and 1 CM based on the key like cm-storage or cm-compute. Please help me in achieving this use case. TIA

configMap.yaml

  1. {{- range $keys, $values := $.Values.topicsToRead }}
  2. {{- if $values.multiPod }}
  3. {{- range $values.region }}
  4. ---
  5. apiVersion: v1
  6. kind: ConfigMap
  7. metadata:
  8. name: cm-{{ . }}
  9. data:
  10. features.toml: |
  11. [features]
  12. x_enabled = false
  13. {{- end -}}
  14. {{- end }}
  15. {{- end }}

Values.yaml

  1. topicsToRead:
  2. compute:
  3. multiPod: true
  4. region:
  5. - region815
  6. - region816
  7. - region817
  8. storage:
  9. multiPod: false
  10. region:
  11. - region715
  12. - region716
  13. - region717

Output:

  1. helm template -s templates/configmap.yaml .
  2. ---
  3. # Source: helm-chart/templates/configmap.yaml
  4. apiVersion: v1
  5. kind: ConfigMap
  6. metadata:
  7. name: cm-region815
  8. data:
  9. features.toml: |
  10. [features]
  11. x_enabled = false
  12. ---
  13. # Source: helm-chart/templates/configmap.yaml
  14. apiVersion: v1
  15. kind: ConfigMap
  16. metadata:
  17. name: cm-region816
  18. data:
  19. features.toml: |
  20. [features]
  21. x_enabled = false
  22. ---
  23. # Source: helm-chart/templates/configmap.yaml
  24. apiVersion: v1
  25. kind: ConfigMap
  26. metadata:
  27. name: cm-region817
  28. data:
  29. features.toml: |
  30. [features]
  31. x_enabled = false

Output I want:

  1. helm template -s templates/configmap.yaml .
  2. ---
  3. # Source: helm-chart/templates/configmap.yaml
  4. apiVersion: v1
  5. kind: ConfigMap
  6. metadata:
  7. name: cm-region815
  8. data:
  9. features.toml: |
  10. [features]
  11. x_enabled = false
  12. ---
  13. # Source: helm-chart/templates/configmap.yaml
  14. apiVersion: v1
  15. kind: ConfigMap
  16. metadata:
  17. name: cm-region816
  18. data:
  19. features.toml: |
  20. [features]
  21. x_enabled = false
  22. ---
  23. # Source: helm-chart/templates/configmap.yaml
  24. apiVersion: v1
  25. kind: ConfigMap
  26. metadata:
  27. name: cm-region817
  28. data:
  29. features.toml: |
  30. [features]
  31. x_enabled = false
  32. ---
  33. # Source: helm-chart/templates/configmap.yaml
  34. apiVersion: v1
  35. kind: ConfigMap
  36. metadata:
  37. name: cm-storage
  38. data:
  39. features.toml: |
  40. [features]
  41. x_enabled = false

答案1

得分: 2

我认为你需要将这个内容分解成多个模板。你可以编写一个生成ConfigMap内容的模板:

  1. {{- /* 生成一个ConfigMap。传入一个带有“name”键的字典作为参数。 */ -}}
  2. {{- define "configmap" -}}
  3. ---
  4. apiVersion: v1
  5. kind: ConfigMap
  6. metadata:
  7. name: {{ .name }}
  8. data:
  9. features.toml: |
  10. [features]
  11. x_enabled = false
  12. {{ end -}}

有了这个模板之后,你可以从外部模板中调用它。由于它是一个独立的辅助模板,即使multiPod为false,你也可以调用它而无需复制模板内容。

  1. {{- range $k, $v := $.Values.topicsToRead }}
  2. {{- if $v.multiPod }}
  3. {{- range $v.region }}
  4. {{- include "configmap" (dict "name" (printf "cm-%s" .)) -}}
  5. {{- end -}}
  6. {{- else -))
  7. {{- include "configmap" (dict "name" (printf "cm-%s" $k)) -}}
  8. {{- end -}}
  9. {{- end -}}

(我已经为了可读性缩进了这个内容,但也确保了每个模板元素都有-空白控制标记。我主要使用了$v作为循环变量名称,以便与标准Helm.Values区分开来。)

我在这里使用的另一个技巧是dict。Helm模板只接受一个参数。我猜测你不是为每个ConfigMap生成相同的固定内容,所以我在这里做的是传递一个字典作为参数。我在外部模板中计算它的名称,然后在内部模板中可以引用它作为.name(参数变量.上的name字段)。你可以通过将它们包含在dict参数中来扩展此方法,以添加更多的“参数”。

英文:

I think you have to break this up into multiple templates. You can write a template that produces the ConfigMap content:

  1. {{- /* Produce a ConfigMap. Pass a dictionary with a key "name" as the
  2. parameter. */ -}}
  3. {{- define "configmap" -}}
  4. ---
  5. apiVersion: v1
  6. kind: ConfigMap
  7. metadata:
  8. name: {{ .name }}
  9. data:
  10. features.toml: |
  11. [features]
  12. x_enabled = false
  13. {{ end -}}

Once you have this, then you can call it from the outer template. Since you have it as a separate helper template, you can invoke it without copying the template content even if multiPod is false.

  1. {{- range $k, $v := $.Values.topicsToRead }}
  2. {{- if $v.multiPod }}
  3. {{- range $v.region }}
  4. {{- include "configmap" (dict "name" (printf "cm-%s" .)) -}}
  5. {{- end -}}
  6. {{- else -))
  7. {{- include "configmap" (dict "name" (printf "cm-%s" $k)) -}}
  8. {{- end -}}
  9. {{- end -}}

(I've indented this for readability, but also made sure absolutely every template element has - whitespace-control markers. I've used $v as the loop variable name mostly to distinguish from the standard Helm .Values.)

The one other trick I've used here is the dict. A Helm template only takes a single parameter. I'm guessing you're not generating the same fixed content for every single ConfigMap, so what I've done here is to pass a dictionary as the parameter. I compute its name in the outer template, and then can refer to it as .name (the name field on the parameter variable .) in the inner template. You can extend this to add more "parameters" by including them in the dict parameter.

答案2

得分: 0

  1. ---
  2. # 源自: test/templates/configmap.yaml
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: cm-region815
  7. data:
  8. features.toml: |
  9. [features]
  10. x_enabled = false
  11. ---
  12. # 源自: test/templates/configmap.yaml
  13. apiVersion: v1
  14. kind: ConfigMap
  15. metadata:
  16. name: cm-region816
  17. data:
  18. features.toml: |
  19. [features]
  20. x_enabled = false
  21. ---
  22. # 源自: test/templates/configmap.yaml
  23. apiVersion: v1
  24. kind: ConfigMap
  25. metadata:
  26. name: cm-region817
  27. data:
  28. features.toml: |
  29. [features]
  30. x_enabled = false
  31. ---
  32. # 源自: test/templates/configmap.yaml
  33. apiVersion: v1
  34. kind: ConfigMap
  35. metadata:
  36. name: cm-storage
  37. data:
  38. features.toml: |
  39. [features]
  40. x_enabled = false
英文:

templates/configmap.yaml

  1. {{- range $keys, $values := $.Values.topicsToRead }}
  2. {{- if $values.multiPod }}
  3. {{- range $values.region }}
  4. ---
  5. apiVersion: v1
  6. kind: ConfigMap
  7. metadata:
  8. name: cm-{{ . }}
  9. data:
  10. features.toml: |
  11. [features]
  12. x_enabled = false
  13. {{- end }}
  14. {{- else }}
  15. ---
  16. apiVersion: v1
  17. kind: ConfigMap
  18. metadata:
  19. name: cm-{{ $keys }}
  20. data:
  21. features.toml: |
  22. [features]
  23. x_enabled = false
  24. {{- end }}
  25. {{- end }}

values.yaml

  1. topicsToRead:
  2. compute:
  3. multiPod: true
  4. region:
  5. - region815
  6. - region816
  7. - region817
  8. storage:
  9. multiPod: false
  10. region:
  11. - region715
  12. - region716
  13. - region717

output

  1. ---
  2. # Source: test/templates/configmap.yaml
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: cm-region815
  7. data:
  8. features.toml: |
  9. [features]
  10. x_enabled = false
  11. ---
  12. # Source: test/templates/configmap.yaml
  13. apiVersion: v1
  14. kind: ConfigMap
  15. metadata:
  16. name: cm-region816
  17. data:
  18. features.toml: |
  19. [features]
  20. x_enabled = false
  21. ---
  22. # Source: test/templates/configmap.yaml
  23. apiVersion: v1
  24. kind: ConfigMap
  25. metadata:
  26. name: cm-region817
  27. data:
  28. features.toml: |
  29. [features]
  30. x_enabled = false
  31. ---
  32. # Source: test/templates/configmap.yaml
  33. apiVersion: v1
  34. kind: ConfigMap
  35. metadata:
  36. name: cm-storage
  37. data:
  38. features.toml: |
  39. [features]
  40. x_enabled = false

huangapple
  • 本文由 发表于 2023年3月7日 16:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659505.html
匿名

发表评论

匿名网友

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

确定