Merging helm templates without overwriting with empty values

1/21/2021

I'm working with helper chart in helm to be able to overwrite common template from lib chart.
To merge templates I'm using the following util:

{{- define "mylibchart.util.merge" -}}
{{- $top := first . -}}
{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
{{- toYaml (merge $overrides $tpl) -}}
{{- end -}}

Problem I faced with is the following.

Common template:

imagePullPolicy: Always
spec:
  replicas: {{ .Values.global.container.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.application.name }}
  template:
    metadata: 
      labels:
        app: {{ .Values.application.name }}

Overwriting template:

spec:
  template:
    metadata: 
      labels:
        app: Custom value
        annotation: custom

Result:

imagePullPolicy: Always
spec:
  template:
    metadata: 
      labels:
        app: Custom value
        annotation: custom

Result I want to achieve:

imagePullPolicy: Always
spec:
  replicas: {{ .Values.global.container.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.application.name }}
  template:
    metadata: 
      labels:
        app: Custom value
        annotation: custom

I've already checked sprig lib but didn't find other function to be handy in this case.

-- DavidGreen55
azure-aks
kubernetes
kubernetes-helm
templates

0 Answers