简体   繁体   中英

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.

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.

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

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 Kube.netes 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM