英文:
Helm create configmap with different names from values file
问题
请注意,你的模板文件似乎有一些语法错误,特别是在循环和条件语句中。以下是你期望的输出的修正模板:
{{- range $topic, $values := $.Values.topicsToRead }}
{{- if $values.multiPod }}
{{- range $values.region }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-{{ . }}
data:
features.toml: |
[features]
x_enabled = false
{{- end }}
{{- end }}
{{- end }}
你可以使用修正后的模板文件运行Helm命令来获得期望的输出。如果multiPod
为true
,则它将创建基于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
{{- range $keys, $values := $.Values.topicsToRead }}
{{- if $values.multiPod }}
{{- range $values.region }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-{{ . }}
data:
features.toml: |
[features]
x_enabled = false
{{- end -}}
{{- end }}
{{- end }}
Values.yaml
topicsToRead:
compute:
multiPod: true
region:
- region815
- region816
- region817
storage:
multiPod: false
region:
- region715
- region716
- region717
Output:
❯ helm template -s templates/configmap.yaml .
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region815
data:
features.toml: |
[features]
x_enabled = false
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region816
data:
features.toml: |
[features]
x_enabled = false
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region817
data:
features.toml: |
[features]
x_enabled = false
Output I want:
❯ helm template -s templates/configmap.yaml .
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region815
data:
features.toml: |
[features]
x_enabled = false
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region816
data:
features.toml: |
[features]
x_enabled = false
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region817
data:
features.toml: |
[features]
x_enabled = false
---
# Source: helm-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-storage
data:
features.toml: |
[features]
x_enabled = false
答案1
得分: 2
我认为你需要将这个内容分解成多个模板。你可以编写一个生成ConfigMap内容的模板:
{{- /* 生成一个ConfigMap。传入一个带有“name”键的字典作为参数。 */ -}}
{{- define "configmap" -}}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .name }}
data:
features.toml: |
[features]
x_enabled = false
{{ end -}}
有了这个模板之后,你可以从外部模板中调用它。由于它是一个独立的辅助模板,即使multiPod
为false,你也可以调用它而无需复制模板内容。
{{- range $k, $v := $.Values.topicsToRead }}
{{- if $v.multiPod }}
{{- range $v.region }}
{{- include "configmap" (dict "name" (printf "cm-%s" .)) -}}
{{- end -}}
{{- else -))
{{- include "configmap" (dict "name" (printf "cm-%s" $k)) -}}
{{- end -}}
{{- 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:
{{- /* Produce a ConfigMap. Pass a dictionary with a key "name" as the
parameter. */ -}}
{{- define "configmap" -}}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .name }}
data:
features.toml: |
[features]
x_enabled = false
{{ 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.
{{- range $k, $v := $.Values.topicsToRead }}
{{- if $v.multiPod }}
{{- range $v.region }}
{{- include "configmap" (dict "name" (printf "cm-%s" .)) -}}
{{- end -}}
{{- else -))
{{- include "configmap" (dict "name" (printf "cm-%s" $k)) -}}
{{- end -}}
{{- 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
---
# 源自: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region815
data:
features.toml: |
[features]
x_enabled = false
---
# 源自: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region816
data:
features.toml: |
[features]
x_enabled = false
---
# 源自: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region817
data:
features.toml: |
[features]
x_enabled = false
---
# 源自: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-storage
data:
features.toml: |
[features]
x_enabled = false
英文:
templates/configmap.yaml
{{- range $keys, $values := $.Values.topicsToRead }}
{{- if $values.multiPod }}
{{- range $values.region }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-{{ . }}
data:
features.toml: |
[features]
x_enabled = false
{{- end }}
{{- else }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-{{ $keys }}
data:
features.toml: |
[features]
x_enabled = false
{{- end }}
{{- end }}
values.yaml
topicsToRead:
compute:
multiPod: true
region:
- region815
- region816
- region817
storage:
multiPod: false
region:
- region715
- region716
- region717
output
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region815
data:
features.toml: |
[features]
x_enabled = false
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region816
data:
features.toml: |
[features]
x_enabled = false
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-region817
data:
features.toml: |
[features]
x_enabled = false
---
# Source: test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-storage
data:
features.toml: |
[features]
x_enabled = false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论