How to refer whole structure from values.yaml instead of specifying one by one?

8/20/2019

I am trying to deploy helm chart in local vritual box on minikube using helm command shown below.

I am referring livenessProbe , readinessProbe configuration directly from values.yam in the deployment.yaml as shown below. However following this approach gives me the error specified below , if i change this to refer each attribute value independently i see the chart is getting installed , pod deploys successfully.

      livenessProbe:
        - {{ .Values.monitorConfig.liveness }}
      readinessProbe:
        - {{ .Values.monitorConfig.readiness }}

Can anyone please let me know what can be done to avoid the error and why??

Thank you.

Helm Command

helm install --debug -n pspk ./pkg/helm/my-service/

Error

Error: release pspk failed: Deployment in version "v1beta1" cannot be handled as a Deployment: v1beta1.Deployment.Spec: v1beta1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.LivenessProbe: readObjectStart: expect { or n, but found [, error found in #10 byte of ...|ssProbe":["map[failu|..., bigger context ...|"imagePullPolicy":"IfNotPresent","livenessProbe":["map[failureThreshold:3 httpGet:map[path:/greeting|...

deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" .}}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: "{{ .Release.Name }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "fullname" . }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 50443
              protocol: TCP
            - name: grpc
              containerPort: 50051
              protocol: TCP
          livenessProbe:
            - {{ .Values.monitorConfig.liveness }}
          readinessProbe:
            - {{ .Values.monitorConfig.readiness }}
          resources:
{{ toYaml .Values.resources | indent 12 }}

values.yaml

replicaCount: 2

application:
  track: stable

image:
  repository: test/go-k8s
  tag: 0.1.1
  pullPolicy: IfNotPresent

# SQL migration scripts

service:
  enabled: false
  type: NodePort
  port: 80
  grpc_port: 50051

env:
  # POSTGRES_HOST  
  postgresHost: localhost
  # POSTGRES_PORT
  postgresPort: "5432"
  # POSTGRES_SSL_MODE
  postgresSSLMode: "disable"
  # POSTGRES_DB
  postgresDB: test
  # POSTGRES_USER
  postgresUser: test
  # POSTGRES_PASSWORD
  postgresPassword: "test"


monitorConfig:
  liveness:
    httpGet:
      path: "/greeting"
      port: 50443
    periodSeconds: 2
    timeoutSeconds: 10
    initialDelaySeconds: 5
    failureThreshold: 3
    successThreshold: 1
  readiness:
    httpGet:
      path: "/greeting"
      port: 50443
    periodSeconds: 2
    timeoutSeconds: 10
    initialDelaySeconds: 5
    failureThreshold: 3
    successThreshold: 1

resources: {}

nodeSelector: {}

tolerations: []

affinity: {}
-- Mozhi
kubernetes
kubernetes-helm
yaml

3 Answers

8/20/2019

In your YAML:

livenessProbe:
  - {{ .Values.monitorConfig.liveness }}
readinessProbe:
  - {{ .Values.monitorConfig.readiness }}

You insert your values into sequence items. Sequence items in YAML are started with -. However, the contents of livenessProbe is expected to be a YAML mapping. The error message is poor but tells you what goes wrong:

expect { or n, but found [,

{ starts a YAML mapping (in flow style), [ starts a YAML sequence (in flow style). The message tells you that the start of a YAML mapping is expected, but the start of a YAML sequence is found. Note that since you're using block style, you don't actually use { and [ here.

So to fix it, simply remove the - so that your inserted mapping (as seen in your values.yaml) is the direct value of livenessProbe and not contained in a sequence:

livenessProbe:
  {{ .Values.monitorConfig.liveness }}
readinessProbe:
  {{ .Values.monitorConfig.readiness }}
-- flyx
Source: StackOverflow

8/20/2019

Thanks to the community answers/comments and helm template guide, it can be combined into:

          {{- if .Values.monitorConfig.liveness }}
          livenessProbe: 
{{ toYaml .Values.monitorConfig.liveness | indent 12 }}
          {{- end }} 

This will give more flexibility.

-- Hanx
Source: StackOverflow

8/20/2019

You need to do two things to make this work correctly: explicitly serialize the value as YAML, and make the indentation correct. This tends to look something like

livenessProbe:
  - {{ .Values.monitorConfig.liveness | toYaml | indent 8 | trim }}

The default serialization will be a Go-native dump format, which isn't YAML and leads to the weird map[failureThreshold:1] output; toYaml fixes this. indent 8 puts spaces at the front of every line in the resulting block (you will need to adjust the "8"). trim removes leading and trailing spaces. (toYaml is Helm-specific and isn't documented well; the other two functions come from the Sprig support library.)

You should double-check this output with

helm template -n pspk ./pkg/helm/my-service/

and if it doesn't look like valid YAML, adjust it further.

-- David Maze
Source: StackOverflow