嵌套模板头盔
Nested templating helm
我正在解决使用自定义 configMap 实现 helm chart 并尝试根据环境模式填充 configMap 的问题。
Values.yaml
externalIPService:
ip: 1.1.1.1
port: 80
emsConfig: "receivers:
otlp:
protocols:
http:
processors:
batch:
exporters:
otlp/ems:
endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
"
configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: "{{ tpl .Values.emsConfig .}}"
helm 模板输出是纯字符串而不是 yaml 内容。我也试过 toYaml 但它也没有帮助。有人可以帮忙找到一种方法来进行嵌套渲染并能够在 confiMap
中使用最终输出吗
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: "receivers: otlp: protocols: http: processors: batch: exporters: otlp/ems: endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }} service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlp/ems] metrics: receivers: [otlp] processors: [batch] exporters: [otlp/ems] logs: receivers: [otlp] processors: [batch] exporters: [otlp/ems] "
在这种情况下,您需要使用 _halpers.tpl
文件。
首先,将其添加到 _halpers.tpl
文件中:
{{- define "appname.emsConfig" -}}
receivers:
otlp:
protocols:
http:
processors:
batch:
exporters:
otlp/ems:
endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
{{- end }}
values.yaml
文件将如下所示:
externalIPService:
ip: 1.1.1.1
port: 80
和 configMap.yaml
文件需要如下所示:
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: |-
{{ include "appname.emsConfig" . | nindent 4}}
我正在解决使用自定义 configMap 实现 helm chart 并尝试根据环境模式填充 configMap 的问题。
Values.yaml
externalIPService:
ip: 1.1.1.1
port: 80
emsConfig: "receivers:
otlp:
protocols:
http:
processors:
batch:
exporters:
otlp/ems:
endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
"
configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: "{{ tpl .Values.emsConfig .}}"
helm 模板输出是纯字符串而不是 yaml 内容。我也试过 toYaml 但它也没有帮助。有人可以帮忙找到一种方法来进行嵌套渲染并能够在 confiMap
中使用最终输出吗apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: "receivers: otlp: protocols: http: processors: batch: exporters: otlp/ems: endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }} service: pipelines: traces: receivers: [otlp] processors: [batch] exporters: [otlp/ems] metrics: receivers: [otlp] processors: [batch] exporters: [otlp/ems] logs: receivers: [otlp] processors: [batch] exporters: [otlp/ems] "
在这种情况下,您需要使用 _halpers.tpl
文件。
首先,将其添加到 _halpers.tpl
文件中:
{{- define "appname.emsConfig" -}}
receivers:
otlp:
protocols:
http:
processors:
batch:
exporters:
otlp/ems:
endpoint: {{ .Values.externalIPService.ip }}:{{ .Values.externalIPService.port }}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp/ems]
{{- end }}
values.yaml
文件将如下所示:
externalIPService:
ip: 1.1.1.1
port: 80
和 configMap.yaml
文件需要如下所示:
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-demo
data:
message: |-
{{ include "appname.emsConfig" . | nindent 4}}