Search code examples
kuberneteskubernetes-helmhelm3

Nested templating helm


I am working on a problem to implement a helm chart with customize configMap and trying to populate the configMap based on the environment mode.

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 .}}"

The helm template output is a plain string and not a yaml content. I have tried toYaml as well but it did not help either. Could someone please help to find a way to do nested rendering and to be able to use the final output in the 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] "

Solution

  • in this situation, you need to use _halpers.tpl file.

    first, add this to the _halpers.tpl file:

    {{- 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 }}
    

    the values.yaml file will look like this:

    externalIPService:
     ip: 1.1.1.1
     port:  80 
    
    

    and the configMap.yaml file, will need to look like this:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: simple-demo
    data:
      message: |-
      {{ include "appname.emsConfig" . | nindent 4}}