helm upgrade fails with "function "X" not defined" - kubernetes

I'm trying to upgrade a helm chart,
I get the error function "pod" not defined which make sense because I really have no such function.
The "pod" is coming from a json file which I convert into a configmap and helm is reading this value as a function and not as a straight string which is part of the json file.
This is a snippet of my configmap:
# Generated from 'pods' from https://raw.githubusercontent.com/coreos/prometheus-operator/master/contrib/kube-prometheus/manifests/grafana-dashboardDefinitions.yaml
# Do not change in-place! In order to change this file first read following link:
# https://github.com/helm/charts/tree/master/stable/prometheus-operator/hack
{{- if and .Values.grafana.enabled .Values.grafana.defaultDashboardsEnabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ printf "%s-%s" (include "prometheus-operator.fullname" $) "services-health" | trunc 63 | trimSuffix "-" }}
labels:
{{- if $.Values.grafana.sidecar.dashboards.label }}
{{ $.Values.grafana.sidecar.dashboards.label }}: "1"
{{- end }}
app: {{ template "prometheus-operator.name" $ }}-grafana
{{ include "prometheus-operator.labels" $ | indent 4 }}
data:
services-health.json: |-
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"targets": [
{
"expr": "{__name__=~\"kube_pod_container_status_ready\", container=\"aggregation\",kubernetes_namespace=\"default\",chart=\"\"}",
"format": "time_series",
"instant": false,
"intervalFactor": 2,
"legendFormat": "{{pod}}",
"refId": "A"
}
}
{{- end }}
The error I get is coming from this line: "legendFormat": "{{pod}}",
And this is the error I get:
helm upgrade --dry-run prometheus-operator-chart
/home/ubuntu/infra-devops/helm/vector-chart/prometheus-operator-chart/
Error: UPGRADE FAILED: parse error in "prometheus-operator/templates/grafana/dashboards/services-health.yaml":
template:
prometheus-operator/templates/grafana/dashboards/services-health.yaml:1213:
function "pod" not defined
I tried to escape it but nothing worked.
Anyone get idea about how I can work around this issue?

Escaping gotpl placeholders is possible using backticks. For example, in your scenario, instead of using {{ pod }} you could write {{` {{ pod }} `}}.

Move your dashboard json to a separate file, let's say name it dashboard.json.
Then in your configmap file: instead of listing the json down inline, reference the dashboard.json file by typing the following:
data:
services-health.json: |-
{{ .Files.Get "dashboard.json" | indent 4 }}
That would solve the problem!

In the case of my experiments, I replaced
"legendFormat": "{{ pod }}",
with
"legendFormat": "{{ "{{ pod }}" }}",
and it was very happy to return the syntax I needed (Specifically for the grafana-operator GrafanaDashboard CRD).

Keeping json file out of config map and sourcing it within config map works, but make sure to keep the json file out of template directory while using with helm, or else it will try to search for {{ pod }} .

Related

Create kubernetes docker-registry secret from yaml file for each lookup namespaces?

I am trying to dynamic lookup available namespaces and able to create secrets in the namespaces using below helm chart.
templates/secrets.yaml
{{ range $index, $namespace := (lookup "v1" "Namespace" "" "").items }}
apiVersion: v1
kind: Secret
metadata:
name: myregcred
namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: {{ template "imagePullSecret" . }}
{{- end}}
values.yaml
imageCredentials:
registry: quay.io
username: someone
password: sillyness
email: someone#host.com
_helpers.tpl
{{- define "imagePullSecret" }}
{{- with .Values.imageCredentials }}
{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
{{- end }}
{{- end }}
When i run this helm chart, i get below error
Error: INSTALLATION FAILED: template: secrets/templates/_helpers.tpl:2:16: executing "imagePullSecret" at <.Values.imageCredentials>: nil pointer evaluating interface {}.imageCredentials
I dont know what I am doing wrong here.
When you reference the named template "imagePullSecret" inside the range, the context "." you are providing refers to the body of the loop, which does not have the "Values" attribute.
Try providing the root context instead:
{{ range $index, $namespace := (lookup "v1" "Namespace" "" "").items }}
apiVersion: v1
kind: Secret
metadata:
name: myregcred
namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: {{ template "imagePullSecret" $ }}
---
{{- end}}

How to parse a JSON secret to YAML in Vault?

I have a secret in Vault which is really a "complex" structure of JSON, meaning it's not just a key/value but there are several keys at different sublevels.
I need to somehow get this secret and convert it to the YAML representation of that JSON. If it was a simple structure (like several k/v at the same level), I could use something as simple as
{{- with secret "secret/foo" -}}
{{ range $k, $v := .Data.data }}
{{ $k }}: {{ $v }}
{{- end }}
{{- end }}
however, as this is not the case, and the structure of the JSON is complex, trying to come up with a template is rather impossible.
However, I found that Vault uses Consul templates, and Consul has a parseYAML function, so my question is, how can I template this so that I get all the content of .Data.data and translate it into YAML?
I have tried several approaches similar to this one below:
{{- with secret "secret/foo" -}}
{{ .Data.data| parseYAML }}
{{- end }}
but I'm always getting the same error wrong type for value; expected string; got map[string]interface {}"
UPDATE
Sample yaml:
apiVersion: v1
kind: ServiceAccount
metadata:
name: app
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
annotations:
vault.hashicorp.com/agent-inject: 'true'
vault.hashicorp.com/agent-inject-secret-foo: 'secret/foo'
vault.hashicorp.com/agent-inject-template-secret-foo: |
{{- with secret "secret/foo" -}}
{{ .Data.data| parseYAML }}
{{- end }}
vault.hashicorp.com/role: 'app'
spec:
containers:
- name: app
image: 'app:1.0.0'
serviceAccountName: app
where secret/foo is a long JSON with no clear structure. A random example (actual JSON is about 300 lines I think).
{
"a": {
"a": "a",
"b": "b",
"c": {
"a": "a",
"b": {
"c": "c"
},
"d": "a"
},
"e": {
"a": {
"b": {
"c": {
"a": "a",
"b": "b"
}
}
}
}
}
}
Ok, just figure this out.
{{- with secret "secret/foo" -}}
{{ .Data.data| toYAML }}
{{- end }}
this is correct, but instead of parseYAML the right function is toYAML.

lookup configmap value in helm template

Say I have a configmap like this, and I want to get the value(12301) of version 123v1 in map_a, what is the correct way?
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
test.yml: |
map_a:
"123v1":
"Version": 12301
"Type": "abc"
......
Here is my attempts:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
data:
{{- $configmap := (lookup "v1" "ConfigMap" "default" "myconfig") }}
{{- if $configmap }}
{{- $models := get $configmap.data "test.yml" }}
version: {{ $models.map_a.123v1.Version }}
{{- end }}
$ helm template .
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /root/test-lookup
Error: parse error at (test-lookup/templates/config.yaml:9): ".123v"
helm.go:88: [debug] parse error at (test-lookup/templates/config.yaml:9): ".123v"
The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec. This is also mentioned in the helm doc |-
{{ $models }} is a value behind a pipe symbol of myconfig>data>test.yml.
So, the value of the expression {{ $models }} is a string instead of a map. In other words, {{ $models. map_a }} is an illegal operation.
The real value of {{ $models }} is a multi-line string, the string content is:
map_a:
"123v1":
"Version": 12301
"Type": "abc"
This can work here, of course the result is not what you want:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
data:
{{- $configmap := (lookup "v1" "ConfigMap" "default" "myconfig") }}
{{- if $configmap }}
{{- $models := get $configmap.data "test.yml" }}
version: {{ $models | quote }}
{{- end }}
output:
apiVersion: v1
data:
version: |
map_a:
"123v1":
"Version": 12301
"Type": "abc"
kind: ConfigMap
metadata:
annotations:
...
So in order to achieve your purpose, you have to operate {{ $models }} in accordance with the method of operating strings.
config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
data:
{{- $configmap := (lookup "v1" "ConfigMap" "default" "myconfig") }}
{{- if $configmap }}
{{- $models := get $configmap.data "test.yml" }}
{{- range ( split "\n" $models) }}
{{- if ( contains "Version" .) }}
version: {{ (split ":" .)._1 | trim | quote }}
{{- end }}
{{- end }}
{{- end }}
output:
apiVersion: v1
data:
version: "12301"
kind: ConfigMap
metadata:
annotations:
...

Using include inside range in Go templates (helm)

I have a template that get rendered several times with a range iteration and I can access variables external variables such as $.Release.Name without a problem. However, when I include templates I can't get it to work:
{{ range $key, $val := $.Values.resources }}
...
annotations:
checksum/config: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
{{ end }}
And in secrets.yaml:
apiVersion: "v1"
kind: "Secret"
metadata:
name: {{ $.Release.Name }}-secrets
I got this error:
Error: render error in "botfront-project/templates/deployment.yaml": template: [filename] :19:28: executing [filename] at <include (print $.Template.BasePath "/secrets.yaml") .>: error calling include: template: .../secrets.yaml:4:19: executing ".../secrets.yaml" at <$.Release.Name>: nil pointer evaluating interface {}.Name
How do I access variables inside an included template?
TL;DR;
just replace . with $ to use the global scope instead of the local one you created .
Example:
{{- include "my-chart.labels" $ | nindent 4 }}
Explanations
According to the docs, https://helm.sh/docs/chart_template_guide/control_structures/#modifying-scope-using-with:
we can use $ for accessing the object Release.Name from the parent
scope.
$ is mapped to the root scope when template execution begins
and it does not change during template execution
With range we change the scope inside the loop. Indeed, {{- include "my-chart.labels" . | nindent 4 }} would invoke the current scope ..
So if you dig into this "scope" thing in helm doc, you eventually find this part: https://helm.sh/docs/chart_template_guide/variables/
With this example:
{{- range .Values.tlsSecrets }}
apiVersion: v1
kind: Secret
metadata:
name: {{ .name }}
labels:
# Many helm templates would use `.` below, but that will not work,
# however `$` will work here
app.kubernetes.io/name: {{ template "fullname" $ }}
# I cannot reference .Chart.Name, but I can do $.Chart.Name
helm.sh/chart: "{{ $.Chart.Name }}-{{ $.Chart.Version }}"
app.kubernetes.io/instance: "{{ $.Release.Name }}"
# Value from appVersion in Chart.yaml
app.kubernetes.io/version: "{{ $.Chart.AppVersion }}"
app.kubernetes.io/managed-by: "{{ $.Release.Service }}"
type: kubernetes.io/tls
data:
tls.crt: {{ .certificate }}
tls.key: {{ .key }}
---
{{- end }}

Helm - Templating variables in values.yaml

I'm trying to template variables from a map inside the values.yaml into my final Kubernetes ConfigMap YAML.
I've read through https://github.com/helm/helm/issues/2492 and https://helm.sh/docs/chart_template_guide/ but can't seem to find an answer.
For some context, this is roughly what I'm trying to do:
values.yaml
config:
key1: value
key2: value-{{ .Release.Name }}
configmap.yaml
kind: ConfigMap
data:
config-file: |
{{- range $key, $value := .Values.config }}
{{ $key }} = {{ $value }}
{{- end }}
Where the desired output with would be:
helm template --name v1 mychart/
kind: ConfigMap
data:
config-file: |
key1 = value
key2 = value-v1
I've tried a few variations using template functions and pipelining, but to no avail:
{{ $key }} = {{ tpl $value . }}
{{ $key }} = {{ $value | tpl . }}
{{ $key }} = {{ tpl $value $ }}
The above would also have worked in this way
values.yaml
config:
key1: "value"
key2: "value-{{ .Release.Name }}"
configmap.yaml
kind: ConfigMap
data:
config-file: |
{{- range $key, $value := .Values.config }}
{{ $key }} = {{ tpl $value $ }}
{{- end }}
What I changed was : I put value in quotes in value.yaml and used template tpl in the config map.
I'll refer to the question's title regarding templating variables in helm and suggest another option to use on values.yaml which is YAML Anchors.
Docs reference
As written in here:
The YAML spec provides a way to store a reference to a value, and
later refer to that value by reference. YAML refers to this as
"anchoring":
coffee: "yes, please"
favorite: &favoriteCoffee "Cappucino"
coffees:
- Latte
- *favoriteCoffee
- Espresso
In the above, &favoriteCoffee sets a reference to Cappuccino.
Later, that reference is used as *favoriteCoffee.
So coffees becomes Latte, Cappuccino, Espresso.
A more practical example
Referring to a common image setup (Registry and PullPolicy) in all values.yaml.
Notice how the default values are being set at Global.Image next to the reference definition which starts with &:
Global:
Image:
Registry: &global-docker-registry "12345678910.dkr.ecr.us-west-2.amazonaws.com" # <--- Default value
PullPolicy: &global-pull-policy "IfNotPresent" # <--- Default value
Nginx:
Image:
Registry: *global-docker-registry
PullPolicy: *global-pull-policy
Version: 1.21.4
Port: 80
MySql:
Image:
Registry: *global-docker-registry
PullPolicy: *global-pull-policy
Name: mysql
Version: 8.0.27
Port: 3306
Managed to solve this using the following syntax:
configmap.yaml
kind: ConfigMap
data:
config-file: |
{{- range $key, $value := .Values.config }}
{{ $key }} = {{ tpl ($value | toString) $ }}
{{- end }}
there is fight in this PR here about this topic.
I know that it's possible now, but this require maintenance of the chart to be in-house (e.g. answer of Amrut ).
Let's summarize :
To have templating in values.yaml , these are the available options:
helm may support that in future ( watch this thread about this topic.)
use tpl function inside the chart
use another tool on top of helm : terraform or helmfile.