defining url in helm chart values as a variable - kubernetes-helm

I have the following yaml file in values.yaml
ingress:
enabled: false
url: {{ .Release.Name }}.abc.com
but when I lint it with helm it shows the following error
Error: cannot load values.yaml: error converting YAML to JSON: yaml: line 14: did not find expected key
if I put "{{ .Release.Name }}.abc.com" it then the .Release.Name is not working. Im new to helm. thank you

helm doesn't support re-rendering like this, you need to use named template to make it work.
Named Template
e.g.
templates/_helpers.tpl
...
{{- define "ingressConfig" -}}
ingress:
enabled: false
url: {{ .Release.Name }}.abc.com
{{- end -}}
...
Use it in other templates like configmap.yaml
templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
{{- include "ingressConfig" . | nindent 4 }}
cmd
helm template --debug test .
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
data: |-
ingress:
enabled: false
url: test.abc.com

Related

helm chart conditional install based on a list

I'm trying to find a way to optionally install a manifest based on a list or a map (really don't mind which) in the values file.
in the values file I have
provisioners: ["gp","test"]
and in the manifest I have
{{- if has "test" .Values.provisioners }}
I've also tried
provisioners:
- "gp"
- "test"
and put this in the yaml
{{- if hasKey .Values.provisioners "test" }}
but I can't either way to work, the chart never installs anything.
I feel like I'm missing something pretty basic, but I can't figure out what. Can someone point me in the right direction.
I don't think you shared everything in you template and there might be something else. What you already did is correct, as you in my example below:
# templates/configmap.yaml
{{- if has "test" .Values.provisioners }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
namespace: default
data:
config.yaml: |
attr=content
{{- end }}
{{- if has "gp" .Values.provisioners }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: gp-config
namespace: default
data:
config.yaml: |
attr=content
{{- end }}
{{- if has "unknown" .Values.provisioners }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: not-templated-config
namespace: default
data:
config.yaml: |
attr=content
{{- end }}
Output of the helm template . against local chart:
---
# Source: chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
namespace: default
data:
config.yaml: |
attr=content
---
# Source: chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: gp-config
namespace: default
data:
config.yaml: |
attr=content

Can i use the ne and eq in deployment.yaml like below?

I am trying to use ne and eq in deployment.yaml but while template helm getting below error
Error:YAML parse error on cdp/templates/cdp-deployment.yaml: error converting YAML to JSON: yaml: line 50: did not find expected key
{{- if (or (ne .Values.metadata.name "application-A") (eq .Values.metadata.name "application-B") )}}
ports:
- containerPort: {{ .Values.service.port }}
envFrom:
- configMapRef:
name: {{ .Values.metadata.name }}
- secretRef:
name: {{ .Values.metadata.name }}
{{- end }}
Thank you in advance
There is no problem with this if statement. I tried to write a demo to test it. There is no problem with this paragraph.
values.yaml
metadata:
name: application-B
templates/cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
cfg: |-
{{- if (or (ne .Values.metadata.name "application-A") (eq .Values.metadata.name "application-B") )}}
ok
{{- else }}
notok
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test-v32
labels:
helm.sh/chart: test-0.1.0
app.kubernetes.io/name: test
app.kubernetes.io/instance: test
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
data:
cfg: |-
ok
The rendered line labels are not the same as the actual line labels, as the fool said, you should call the helm template --debug test . command to debug to see what the problem is.

How i can read a yaml dict and use it inside helm-template

I am creating a helm libary that i will use in other helm chart and i want to have a common values file in this libary. I have thought to do it using a external file and load it like below but i haven't be able to do it. What i am doing wrong? Thanks
##defaultRoutesHelm.yaml
route:
dev:
path: dev.es
labels:
router: alpha
staging:
path: staging.es
labels:
router: beta
--
{{- $helmRoute := .Files.Get "config/defaultRoutesHelm.yaml" | fromYaml }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
path: {{get $helmRoute 'route.dev.path'}}

Helm create secret from env file

Kubectl provides a nice way to convert environment variable files into secrets using:
$ kubectl create secret generic my-env-list --from-env-file=envfile
Is there any way to achieve this in Helm? I tried the below snippet but the result was quite different:
kind: Secret
metadata:
name: my-env-list
data:
{{ .Files.Get "envfile" | b64enc }}
It appears kubectl just does the simple thing and only splits on a single = character so the Helm way would be to replicate that behavior (helm has regexSplit which will suffice for our purposes):
apiVersion: v1
kind: Secret
data:
{{ range .Files.Lines "envfile" }}
{{ if . }}
{{ $parts := regexSplit "=" . 2 }}
{{ index $parts 0 }}: {{ index $parts 1 | b64enc }}
{{ end }}
{{ end }}
that {{ if . }} is because .Files.Lines returned an empty string which of course doesn't comply with the pattern
Be aware that kubectl's version accepts barewords looked up from the environment which helm has no support for doing, so if your envfile is formatted like that, this specific implementation will fail
I would like to use env files but it seems to be helm doesn't support that yet.
Instead of using an env file you could use a yaml file.
I mean to convert from this env file
#envfile
MYENV1=VALUE1
MYENV2=VALUE2
to this yaml file (verify the yaml format, always it should be an empty space after the colon)
#envfile.yaml
MYENV1: VALUE1
MYENV2: VALUE2
After this, you should move the envfile.yaml generated in the root folder of your helm chart (same level of values yaml files)
You have to set up your secret.yaml in this way:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
annotations:
checksum/config: {{ (tpl (.Files.Glob "envfile.yaml").AsSecrets . ) | sha256sum }}
type: Opaque
data:
{{- $v := $.Files.Get "envfile.yaml" | fromYaml }}
{{- range $key, $val := $v }}
{{ $key | indent 2 }}: {{ $val | b64enc }}
{{- end}}
We are iterating in the data property the envfile.yaml generated and encoding the value to base64. The result secret will be the next:
kubectl get secret my-secret -o yaml
apiVersion: v1
data:
MYENV1: VkFMVUUx
MYENV2: VkFMVUUy
kind: Secret
metadata:
annotations:
checksum/config: 8365925e9f9cf07b2a2b7f2ad8525ff79837d67eb0d41bb64c410a382bc3fcbc
creationTimestamp: "2022-07-09T10:25:16Z"
labels:
app.kubernetes.io/managed-by: Helm
name: my-secret
resourceVersion: "645673"
uid: fc2b3722-e5ef-435e-85e0-57c63725bd8b
type: Opaque
Also, I'm using checksum/config annotation to update the secret object every time a value is updated.

Usage of Variable Chart.Name in inherited Helm Chart

I've created a helm chart which contains some resources, which are reused in several other Helm charts:
base/templates/base.yaml
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: {{ .Chart.Name }}
Then I've created a helm chart which inherits the base chart and contains some special resources:
sub1/templates/sub1.yaml
...
name: {{ .Chart.Name }}
Actual Output
In the actual output the resources of the base chart use always the chart name of the base chart.
---
# Source: sub1/templates/sub1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sub1
---
# Source: sub1/charts/base/templates/base.yaml
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: base
Wanted output
But I want the chart name of the sub chart to be used in the base chart resources.
# Source: sub1/charts/base/templates/base.yaml
...
kind: SecretProviderClass
metadata:
name: sub1
How can I achieve this?
A solution is to reuse the resources via named templates:
base/templates/base.yaml
{{- define "base-lib.secret-provider-class" -}}
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: {{ .Chart.Name }}
{{- end -}}
sub1/templates/sub1.yaml
{{ include "base-lib.secret-provider-class" . }}
---
...