Home > front end >  Helm - How to add a label block through values.yaml
Helm - How to add a label block through values.yaml

Time:12-22

I have simple helm chart. I have a labels: block that I need to refer in a Deployment

Here's my values.yaml

labels:
    app: test-app
    group: test-group
    provider: test-provider

And in the templates/deployment.yaml I need to add the above whole labels block. So I did;

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ include "accountmasterdata.fullname" . }}
  namespace: {{ .Values.namespace }}
  labels:
  {{ .Values.labels | nindent 4 }}

  {{- include "accountmasterdata.labels" . | nindent 4 }} 

But I get the following error

wrong type for value; expected string; got map[string]interface {}

Can someone help me with two things:

  1. How can I solve this issue

  2. And in the line where it says {{- include "accountmasterdata.labels" . | nindent 4 }} , where I can see the accountmasterdata.labels values? And how to override those?

Thank you!

CodePudding user response:

Iterating over a mapping is covered in the "Variables" documentation:

For data structures that have both a key and a value, we can use range to get both. For example, we can loop through .Values.favorite like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
  {{- range $key, $val := .Values.favorite }}
  {{ $key }}: {{ $val | quote }}
  {{- end }}

So in your template, you would handle the value of .Values.labels like this:

  labels:
{{- range $name, $value := .Values.labels }}
    {{ $name | quote }}: {{ $value | quote }}
{{- end -}}

And in the line where it says {{- include "accountmasterdata.labels" . | nindent 4 }} , where I can see the accountmasterdata.labels values? And how to override those?

Is this a template you are writing? If so, where have you defined these values? Presumably in your templates/ directory there exists a file that includes something like:

{{- define "accountmasterdata.labels" -}}
...
{{- end -}}

The contents of that block are what will get inserted at the point of reference.


Lastly, in your template you have:

namespace: {{ .Values.namespace }}

But you probably want to use .Release.Namespace instead:

namespace: {{ .Release.Namespace | quote }}

With the above changes in place, I end up with:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ include "accountmasterdata.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels:
{{- range $name, $value := .Values.labels }}
    {{ $name | quote }}: {{ $value | quote }}
{{- end -}}
{{- include "accountmasterdata.labels" . | nindent 4 }} 
  • Related