Search code examples
yamlkubernetes-helm

Merge a yaml files with values in Helm


I am pretty new to Helm templates

In order to implement a multi-environment solution (for Infinispan), I would like to reach a decent level of customizability of the infinispan-config.yaml. Helm already allows you to load values from multiple values files (e.g. values.yaml merged with values-dev.yaml).

What I want to do is to merge an existing file committed to the repository with the values contained in a node in the values file. Sounds strange or complex?

Let's say I have a config map like

data:
  infinispan-config.yaml |-
  {{- .Files.Get "infinispan-config.yaml" }}

It loads the committed "general" infinispan-config.yaml into the resulting template.

But what if for the dev environment I want to customize a single node in the infinispan-config.yaml? By using the values-dev file I can do something like

config:
  infinispan:
    my-custom-property: value

And I would like that custom property to appear in the final ConfigMap along with the default properties.

How do I do that? So far, I could only dump the entire infinispan-config as a node in the general values files, and load with toYaml function


Solution

  • Even if this answer is generated with the help of AI, I tested it and claim responsibility.

    The solution is

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ include "ipe-cache.fullname" . }}-cm
    data:
      infinispan-config.yaml: |-
      {{- toYaml (merge (.Files.Get "infinispan-config.yaml" | fromYaml) (.Values.infinispan | default )) | nindent 4}}
    

    Explanation: use the merge function

    • Merge
      • Object 1: .Files.Get "infinispan-config.yaml" parsed as YAML
      • Object 2: value from .Values.infinispan or default empty value
    • Encode to yaml
    • Indent

    The desired result happens: values from values-dev.yaml override defaults in the plain infinispan-config.yaml

    Applies to any sort of configuration