Helm - How to add a label block through values.yaml - kubernetes

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:
How can I solve this issue
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!

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

nil pointer evaluating interface {}.name when deploy helm chart

I am new to helm and kubernetes, so I maybe missed something.
I have created a Helm Chart to deploy MISP. Unfortunately, I am now getting the following error.
Error: INSTALLATION FAILED: template: misp/templates/serviceaccount.yaml:4:11: executing "misp/templates/serviceaccount.yaml" at <include "misp.fullname" .>: error calling include: template: misp/templates/_helpers.tpl:7:40: executing "misp.fullname" at <.Values.misp.name>: nil pointer evaluating interface {}.name
My helpers.tpl looks like this:
{{/*
This is a helper template file that defines the misp.name variable.
*/}}
{{- define "misp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Values.misp.name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "misp.name" -}}
misp
{{- end -}}
{{- define "misp-chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "misp-chart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "misp-chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "misp-chart.labels" -}}
helm.sh/chart: {{ include "misp-chart.chart" . }}
{{ include "misp-chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "misp-chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "misp-chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Create the name of the service account to use
*/}}
{{- define "misp-chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "misp-chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
and my templates/serviceaccount.yaml like this:
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "misp.fullname" . }}
labels:
app: {{ include "misp.name" . }}
chart: {{ include "misp.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
And here my values.yaml
# Default values for misp.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: coolacid/misp
tag: latest
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 8080
ingress:
enabled: false
# Used to create an Ingress record.
hosts:
- misp.example.com
annotations:
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
tls:
# Secrets must be manually created in the namespace.
- secretName: misp-tls
hosts:
- misp.example.com
database:
mariadb:
enabled: true
image:
repository: bitnami/mariadb
tag: 10.3.23-debian-10-r44
pullPolicy: IfNotPresent
rootUser:
password: password
# databaseName: misp
# user: misp
# password: misp
redis:
enabled: true
image:
repository: bitnami/redis
tag: 6.0.6-debian-10-r0
pullPolicy: IfNotPresent
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
Thank you for your help
try to regenerate the files

How to get the output file without yaml validation in Helm charts?

I have Helm charts that try to install a service on k8s. when I am trying to run this command it shows this error:
YAML parse error on myService/templates/configmap.yaml: error converting YAML to JSON: yaml: line 152: did not find expected key
here is my configmap.yaml file:
{{- if .Values.configFiles }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "revad.fullname" . }}-config
labels:
{{- include "revad.labels" . | nindent 4 }}
data:
{{- $host := .Values.HostURL }}
{{- $ocHost := .Values.OCURL }}
{{- $secret := .Values.SharedSecret }}
{{- $certFile := .Values.CertFile }}
{{- $certKey := .Values.CertKey }}
{{- range $filename, $fileContents := .Values.configFiles }}
{{ $filename }}: |-
{{ if eq $filename "oc.revad.toml" }}
{{ $fileContents | indent 4 | replace "OC_URL" $ocHost | replace "HOST_URL" $host | replace "SHARED_SECRET" $secret | replace "CERT_FILE" $certFile | replace "CERT_KEY" $certKey | quote }}
{{ else }}
{{ $fileContents | indent 4 }}
{{ end }}
{{- end }}
{{- end }}
I cannot find the exact location of the error. I was wondering how much it would be helpful if there were an option that shows the output file without any YAML validation to debug my template. does helm has this option? if the answer is no, so how can I debug my helm chart?
Helm Debugging Templates
helm template --debug test .
It's a great way to have the server render your templates, then return the resulting manifest file.
(And may you provide values.yaml to help locate errors?)

Inject vault secret into k8s config map

For ArgoCD configuration I would like to pass vault secrets into ConfigMap.
My config map template:
{{- if .Values.server.configEnabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
labels:
{{- include "argo-cd.labels" (dict "context" . "component" .Values.server.name "name" "cm") | nindent 4 }}
{{- if .Values.server.configAnnotations }}
annotations:
{{- range $key, $value := .Values.server.configAnnotations }}
{{ $key }}: {{ $value | quote }}
{{- end }}
{{- end }}
data: {{- include "argo-cd.config" $ | nindent 4 }}
{{- end }}
and values part:
server:
podAnnotations:
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/auth-path: "auth/gke"
vault.hashicorp.com/role: "default-role"
config:
oidc.config: |
name: Google
issuer: https://accounts.google.com
clientID: |
{{- with secret "secret-gke/data/argocd/argocd-server/client" -}}
{{ .Data.data.clientID }}
{{- end -}}
clientSecret: |
{{- with secret "secret-gke/data/argocd/argocd-server/client" -}}
{{ .Data.data.clientSecret }}
{{- end -}}
podAnnotations allow to inject secrets into Pod and configAnnotations do not solve the issue, so how to inject secrets into ConfigMap?

Helm3 template error calling include: template: no template "microservice.labels" associated with template "gotpl"

I have helm 3 template created using helm create microservice command. it has below files.
/Chart.yaml
./values.yaml
./.helmignore
./templates/ingress.yaml
./templates/deployment.yaml
./templates/service.yaml
./templates/serviceaccount.yaml
./templates/hpa.yaml
./templates/NOTES.txt
./templates/_helpers.tpl
./templates/tests/test-connection.yaml
Updated values file based on my application, when I try to install the helm chat its giving below error message.
Error: UPGRADE FAILED: template: microservice/templates/ingress.yaml:20:8: executing "microservice/templates/ingress.yaml" at <include "microservice.labels" .>: error calling include: template: no template "microservice.labels" associated with template "gotpl"
helm.go:75: [debug] template: microservice/templates/ingress.yaml:20:8: executing "microservice/templates/ingress.yaml" at <include "microservice.labels" .>: error calling include: template: no template "microservice.labels" associated with template "gotpl"
Here is the ingress.yaml file.
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "microservice.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
{{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
{{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "microservice.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
ingressClassName: {{ .Values.ingress.className }}
{{- end }}
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
{{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
pathType: {{ .pathType }}
{{- end }}
backend:
{{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
service:
name: {{ $fullName }}
port:
number: {{ $svcPort }}
{{- else }}
serviceName: {{ $fullName }}
servicePort: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
How to I added microservice.labels template?. Do I need to create microservice.labels.tlp file?
Any tips to fix this error.
Thanks
SR
I copied the ingress.yaml file to, chart created older version helm. this value was missing in _helpers.tpl file. Now I copied new version of hellpers.tpl file. deployment works now.

Create multiple configmaps on kubernetes with using yaml files

I have a few yaml files that contains some values. I want to read that files while helm deploying and create configmaps for each of them.
I've added config file under the helm charts. ( Same level with templates folder )
chart structure
And then I've tried to create 'configmap-creator.yaml' which is located under the 'templates' folder.
I simply run 'helm upgrade --install ealpkar --namespace ealpkar --create-namespace .'
It was complete successfully but there is only one configmap which is called 'config2-configmap'. I missed the first one ( config1-configmap )
Here is the 'configmap-creator.yaml'
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}
Example of yaml file which is under 'config' folder;
config1.yaml
dummy_product:
ip: 10.10.10.10
port: 22
config2.yaml
dummy_product_2:
ip: 10.10.10.20
port: 22
Fix your template, adding a separator between objects.
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}