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

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?)

Related

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

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 }}

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?

How to replace string with variable in Helm template?

I have this template:
{{- $service_port := 1010 }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $.Values.name }}
data:
{{- range $key, $val := .Values.configmap }}
{{- if contains "<ServicePort>" $val }}
{{ $key }}: '{{ $val | replace "<ServicePort>" {{ $service_port }} }}'
I need to replace it with service_port defined in the beginning of a file. How can I achieve this. The code above does not seem to work.
This works for me. It replaces all serviceName tokens by the real service name.
appSettings: |
{{ $name := include "myService.fullname" . }}
<appSettings>
{{- $root := . }}
{{- range $key, $value := .Values.appSettings }}
<add key={{ $key | quote }} value={{ $value | quote | replace "#{serviceName}#" $name }} />
{{- end }}
</appSettings>
Important was, that the replace function was used as the last one. Before it finally worked, I've tried with using replace before the quote function.

how to use variables defined in helm templates inside files to be used in configmap

Helm newbie here. I am trying to loop through files and create a configmap with its contents. The file contents need to have variable defined inside the loop. Below is the configmap I am working with.
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "proj.name" . }}
labels:
app.kubernetes.io/name: {{ include "proj.name" . }}
app.kubernetes.io/version: {{ include "proj.version" . }}
data:
{{- range $path, $_ := .Files.Glob "myconfig/*.txt" -}}
{{ $path | base | nindent 2 }}: |
{{- tpl ($.Files.Get $path) $ | nindent 4 }}
{{- end -}}
contents of myconfig/name.txt
i am able to access this -> {{ .Values.somekey }}
But not this -> {{ $path }}
I get error: undefined variable "$path"
Any help would be greatly appreciated. Thank you.
The $path variable is a local variable. It's not accessible from other template functions or tpl expansions. The special "root context" variable $ doesn't include local variables either.
What you could do in this case is define your own context for the tpl function:
{{- $context := dict "somekey" .Values.somekey "path" $path }}
{{- tpl ($.Files.Get $path) $context | nindent 4 }}
Then in the file you'd refer to the keys provided in that dict (but not other things from outside that explicit context)
i am able to access this -> {{ .somekey }}
and also this -> {{ .path }}
but i wouldn't be able to reference .Values.anything

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 }}