I am trying to use the {{ template }} function on my helm chart, but inside a loop the value does error.
First here is my _helpers.tpl:
{{- define "traefik.deployNamespace" -}}
{{ default "kube-system" .Values.deployNamespace }}
{{- end -}}
I can use that variable with {{ template "treafik.deployNamespace }} on all the templates, expect when inside a loop:
# Iterates on allowed namespaces and creates the roles
{{- if .Values.allowedNamespaces }}
{{- range .Values.allowedNamespaces }}
# Rules for namespace: {{ . }}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{ . }}-traefik-ingress-r
namespace: {{ . }}
rules:
- apiGroups:
- ""
resources:
- services
- endpoints
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses
verbs:
- get
- list
- watch
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{ . }}-traefik-ingress-rb
namespace: {{ . }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ . }}-traefik-ingress-r
subjects:
- kind: ServiceAccount
name: traefik-ingress-sa
namespace: {{ template "traefik.deployNamespace" . }}
{{- end }}
{{- end }}
I am probably doing the $ctx wrong, but I don't know what I should use in there.
> % helm template .
Error: template: traefik/templates/_helpers.tpl:62:36: executing "traefik.deployNamespace" at <.Values.deployNamespace>: can't evaluate field Values in type strin
At the bottom of the file you have a couple of uses of the . current-context variable. Inside the context of a {{ range }}...{{ end }} loop, . gets set to the item from the list you're iterating over.
# `.` in all three of these is the same thing (a string)
name: {{ . }}-traefik-ingress-rb
namespace: {{ . }}
namespace: {{ template "traefik.deployNamespace" . }}
Templates generally expect to get the top-level Helm environment object as their parameter (a dictionary-type object with "Charts", "Values", and so on). You need to save it away outside your loop in a variable so you can get access to it.
{{- $top := . }}
{{- range .Values.allowedNamespaces }}
...
namespace: {{ template "traefik.deployNamespace" $top }}
...
{{- end }}
Related
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}}
The helm values look like in the following example. Here appdata can scale to any number but will contain the same set of keys.
data:
appdata:
config0:
url: 'https://example1.com'
port: '80'
config1:
url: 'https://example2.com'
port: '8673'
someotherconfig:
url: 'https://example3.com'
port: '9887'
...
...
This is what I have so far. This keeps updating the last config's data from someotherconfig key and also I want to have the config map name to contain the config name for each iteration, like {{ template "app" $ }}-config0, {{ template "app" $ }}-config1 and so on based on iteration.
{{- range $Mydata := $.Values.data.appdata }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "app" $ }}
labels:
data:
url: {{ $Mydata.url }}
port: {{ $Mydata.port }}
{{- end }}
You're almost there. You need to use the key/value notation to get the key name. Try the following.
{{- range $configuration, $Mydata := $.Values.data.appdata }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "app" $ }}-{{ $configuration }}
data:
url: {{ $Mydata.url }}
port: {{ $Mydata.port }}
{{- end }}
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:
...
Can someone explain to me what the role of the keyword "template" is in this code :
apiVersion: v1
kind: Secret
metadata:
name: {{ template "identity-openidconnect" . }}
namespace: {{ .Release.Namespace }}
labels:
app: {{ template "microService.name" . }}
release: "{{ .Release.Name }}"
xxxx
xxxxxxxxxxxx
The keyword "template" means, that Helm will find the previously created template and complete the yaml file according to the template in the template. It has to be created in advance. This type of construction allows you to refer to the same scheme many times.
For example, we can define a template to encapsulate a Kubernetes block of labels:
{{- define "mychart.labels" }}
labels:
generator: helm
date: {{ now | htmlDate }}
{{- end }}
Now we can embed this template inside of our existing ConfigMap, and then include it with the template action:
{{- define "mychart.labels" }}
labels:
generator: helm
date: {{ now | htmlDate }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
{{- template "mychart.labels" }}
data:
myvalue: "Hello World"
{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end }}
When the template engine reads this file, it will store away the reference to mychart.labels until template "mychart.labels" is called. Then it will render that template inline. So the result will look like this:
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: running-panda-configmap
labels:
generator: helm
date: 2016-11-02
data:
myvalue: "Hello World"
drink: "coffee"
food: "pizza"
Note: a define does not produce output unless it is called with a template, as in this example.
For more info about templates you can read this page.
I am creating a Helm chart. When doing a dry run I get a error:
Error: YAML parse error on vstsagent/templates/vsts-buildrelease-agent.yaml: error converting YAML to JSON: yaml: line 28: found character that cannot start any token
The dry run also outputs the secret and deployment YAML file which I created. The part where it goes wrong in the deployment shows:
- name: ACCOUNT
valueFrom:
secretKeyRef:
name: %!s(<nil>)-%!s(<nil>)
key: ACCOUNT
- name: TOKEN
valueFrom:
secretKeyRef:
name: %!s(<nil>)-%!s(<nil>)
key: TOKEN
The output from the dry run for the secret looks fine.
The templates I created:
apiVersion: v1
kind: Secret
metadata:
name: {{ template "chart.fullname" . }}
type: Opaque
data:
ACCOUNT: {{ .Values.chart.secret.account }}
TOKEN: {{ .Values.chart.secret.token }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "chart.fullname" . }}
labels:
app: {{ template "chart.name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
updateStrategy:
type: RollingUpdate
template:
metadata:
labels:
release: {{ .Release.Name }}
app: {{ template "chart.name" . }}
annotations:
agentVersion: {{ .Values.chart.image.tag }}
spec:
containers:
- name: {{ template "chart.name" . }}
image: {{ .Values.chart.image.name }}
imagePullPolicy: {{ .Values.chart.image.pullPolicy }}
env:
- name: ACCOUNT
valueFrom:
secretKeyRef:
name: {{ template "chart.fullname" }}
key: ACCOUNT
- name: TOKEN
valueFrom:
secretKeyRef:
name: {{ template "chart.fullname" }}
key: TOKEN
The _helper.tpl looks like this:
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "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).
*/}}
{{- define "chart.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Where am I going wrong in this?
I missed 2 dots....
- name: ACCOUNT
valueFrom:
secretKeyRef:
name: {{ template "chart.fullname" . }}
key: ACCOUNT
- name: TOKEN
valueFrom:
secretKeyRef:
name: {{ template "chart.fullname" . }}
key: TOKEN