Search code examples
kuberneteskubernetes-helm

Pass an entire yaml from values.yaml to templates in helm


I am trying to pass entire set of yamls from values.yaml in helm to the templates, so that whatever yaml inputs I pass in the values.yaml section goes in the templates yaml as it is :

For example :

values.yaml

...
...
metallbConfig: |-
  apiVersion: metallb.io/v1beta2
  kind: BGPPeer
  metadata:
    creationTimestamp: null
    name: peer1
    namespace: metallb-system
  spec:
    holdTime: 3s
    keepaliveTime: 0s
    myASN: 64026
    passwordSecret: {}
    peerASN: 65227
    peerAddress: 10.252.254.194
  status: {}

templates/resources.yaml :

{{ toYaml .Values.metallbConfig }}

Essentially what I want to achieve is whole BGPPeer section to be present in the resources.yaml when I deploy the chart.

Currently I am getting this error :

# helm template metallbcnf . --output-dir outputs --debug
...
...
Error: YAML parse error on metallb/templates/resources.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type releaseutil.SimpleHead
helm.go:84: [debug] error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type releaseutil.SimpleHead

Kindly help me resolve the same.


Solution

  • If you want to embed the yaml entirely, you don't need the |-

    For example, I have this in values.yaml

    ...
    probes:
      livenessProbe:
        httpGet:
          path: /ping
          port: 80
        initialDelaySeconds: 15
        periodSeconds: 60
        successThreshold: 1
        timeoutSeconds: 5
        failureThreshold: 3
      readinessProbe:
        httpGet:
          path: /ping
          port: 80
        initialDelaySeconds: 15
        periodSeconds: 60
        successThreshold: 1
        timeoutSeconds: 5
        failureThreshold: 3
    ...
    

    Then use this in my helm deployment:

    apiVersion: apps/v1
    kind: Deployment
    ...
    spec:
      ...
      template:
        ...
        spec:
          ...
          containers:
            - name: {{ .Chart.Name }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag}}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
    
              {{- toYaml .Values.probes | nindent 10 }}
               ...
          ...
    

    You'll notice I need to be explicit about the indenting using nindent otherwise helm just pastes in the yaml as in the values.yaml which breaks the parsing