Helm template - combine yamls with .Files.Get - yaml

I am using the tp function, https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function
to include a config file of yaml inside a template.
templates/omer.yaml:
{{ $omer := tpl (.Files.Get "files/app.conf") . }}
{{- $omer-}}
The file files/app.conf contains:
pod1:
custom:
deployment:
annotations:
sidecar.istio.io/proxyCPU: 10m
sidecar.istio.io/proxyMemory: "16Mi"
sidecar.istio.io/proxyCPULimit: 50m
sidecar.istio.io/proxyMemoryLimit: "64Mi"
When I helm template, it works fine. What I'm trying to accomplish, is to merge the app.conf yaml to another yaml, also defined in the templates folder. Trying the merge command for other yamls is working, but when using the tpl Files.Get it is recognised as a string and not as template, trying templates/omer.yaml:
{{ $omer := tpl (.Files.Get "files/app.conf") . }}
{{- $output := mergeOverwrite $omer }}
{{- $output -}}
gives me an error:
Error: template: chart/templates/omer.yaml:2:30: executing "chart/templates/omer.yaml" at <$omer>: wrong type for value; expected map[string]interface {}; got string
Also when trying to convert:
{{- $output := fromYaml ($omer ) }}
Getting an error:
Error: YAML parse error on chart/templates/omer.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type releaseutil.SimpleHead

Related

alertmanager gotemplate function contains is not found

I'm trying to exclude certain labels from the list to be added in the URL.
However alertmanager complains about the following template with the error:
ts=2023-02-08T23:25:31.016Z caller=coordinator.go:132 component=configuration msg="one or more config change subscribers failed to apply new config" file=/etc/alertmanager/alertmanager.yml err="failed to parse templates: template: notifications.tmpl:9: function \"contains\" not defined"
{{ define "alert_silencer_url" -}}
https://xxx.yyy/#/silences/new?filter=%7B
{{- range .CommonLabels.SortedPairs -}}
{{- if not contains .Name "alertname helm_sh_chart" -}}
{{- .Name }}%3D"{{- .Value -}}"%2C%20
{{- end -}}
{{- end -}}
alertname%3D"{{- .CommonLabels.alertname -}}"%7D
{{- end }}
what could be done. I have a list of keywords I need to drop from the URL.

how to use $val to get value from map in helm for kubernetes?

I got a map in values.yaml:
Schedule:
app1: node01
app2: node07
app3: node13
and I want to use it in template/app.yaml:
{{- $tuplei := untilStep 1 4 1 -}}
{{- range $keyi, $vali := $tuplei }}
---
spec:
template:
spec:
nodeName: {{ $.Values.Schedule.node$vali }}
It can't work:
Error: parse error at (xxx/templates/app.yaml:51): bad character U+0024 '$'
helm.go:94: [debug] parse error at (xxx/templates/app.yaml:51): bad character U+0024 '$'
I have tried some ways, but still can't make it.
#{{- $ScheduleName := printf "app%d" $vali }}
#nodeName: get $.Values.Schedule "$ScheduleName"
#can't work, too.
The Go text/template language includes an index function, which does an arbitrary lookup by key or index. So your last form is almost correct: you need to construct the key in a string, and then use index to retrieve it.
{{- $scheduleName := printf "app%d" $vali -}}
nodeName: {{ index $.Values.Schedule $scheduleName }}
Make sure to not quote the $scheduleName variable reference, lest the template language interpret it as a string literal.

Range and string in Go template

I am writing a Go template to create a yaml config:
my yaml config
logs:
- type: file
path: /opt/nomad/data/alloc/*/alloc/logs/*App1.stderr.*
service: App1
source: nomad
- type: file
path: /opt/nomad/data/alloc/*/alloc/logs/*App2.stderr.*
service: App2
source: nomad
My go template
logs:
{{- range $value := . -}}
- type: file
path: /opt/nomad/data/alloc/*/alloc/logs/*{{$value.Task}}.stderr.*
exclude_paths: /opt/nomad/data/alloc/*/alloc/logs/*{{$value.Task}}.stderr.fifo
service: {{$value.Task}}
source: nomad
{{end}}
How i need to change the template to get my yaml config
The Go yaml package enables programs to comfortably encode and decode YAML values.
You can use that: https://pkg.go.dev/gopkg.in/yaml.v2

How escape chars in Go Template?

I have this YAML structure:
metadata:
annotations:
creationTimestamp: "2021-04-20T15:18:50Z"
labels:
billing: mybilling
beta.kubernetes.io/os: linux
I'm trying to access it over my script but I could not access the "beta.kubernetes.io/os" because the special chars.
{{$os_version := .metadata.labels.beta.kubernetes.io/os }} --> failed
{{$os_version := .metadata.labels."beta.kubernetes.io/os" }} --> failed
{{$os_version := .metadata.labels.beta\.kubernetes\.io\/os }} --> failed
{{$os_version := .metadata.labels.billing }} --> success
How to access the metadata ".metadata.labels.beta.kubernetes.io/os"? How to scape this special chars?
The standard library already provides the index function
{{ index .metadata.labels "beta.kubernetes.io/os" }}
In my case:
{{$os_version := index .metadata.labels "beta.kubernetes.io/os" -}} --> works!!!

What does this mean in YAML?

I have a YAML file used for helm chart:
my_project_deployment.yaml:
- name: unzipper.storageMounts.persistent
value: {{- range $index, $item := .Values.base.storage.netapp.persistent }}{{- if $index }},{{- end }} {{ print .mountPath "/studies_001" }}{{- end }}
I know that it is being used for defining properties for specific project. However, I don't understand what it means.
I have a file called dev_value.yaml file which this .Values are referencing to.
dev_value.yaml:
base:
storage:
netapp:
transient:
volumeI:
serverIp:
imageStorePath: /cache001
mountPath: /mnt/i
claim:
core: pvc-core-cache001
persistent:
- name: volumeR
serverIp:
imageStorePath: /imagestore001
mountPath: /mnt/r
claim:
core: pvc-core-imagestore001
- name: volumeS
serverIp:
imageStorePath: /imagestore002
mountPath: /mnt/s
claim:
core: pvc-core-imagestore002
what would be the value in my_project_deployment.yaml? And can anyone please explain how to come up with the value?
This has little to do with YAML, it is Go's text/template syntax. The YAML file is processed by the templating engine before it is parsed by the YAML processor, so by the time the input is parsed as YAML, the value has already been set.
Helm does have docs about how templating works and particularly on values files.
The instructions to the templating engine can be formatted so that it is obvious what happens:
{{- range $index, $item := .Values.base.storage.netapp.persistent }}
{{- if $index }},{{- end }} {{ print .mountPath "/studies_001" }}
{{- end }}
It says:
For each sequence item in the values file at path base.storage.netapp.persistent, write a comma if it's not the first one ($index will be interpreted as false if it's 0), then print its mountPath suffixed by /studies_001.
With your values file, you will get
value: /mnt/r/studies_001, /mnt/s/studies_001

Resources