Home > Back-end >  Pass an entire yaml from values.yaml to templates in helm
Pass an entire yaml from values.yaml to templates in helm

Time:12-13

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.

CodePudding user response:

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

CodePudding user response:

values.yaml file contains a string instead of a valid YAML object. You need to convert the string to a YAML object before passing it to the template.

CodePudding user response:

Your string is already syntactically correct YAML (hopefully!) and you can just write it out as-is. toYaml will add additional quoting to turn it into specifically a YAML string and you don't want that.

---
{{ .Values.metallbConfig }}
{{/* without toYaml */}}

(I'd consider it a little unusual to deposit an entire Kubernetes YAML object in Helm values, especially as an opaque string. Consider moving this into its own file in the templates directory and using templating only for the specific fields that need to be customized.)

  • Related