Helm template pass array of string value - kubernetes-helm

how do you pass array of string to helm template? and it should be like comma separated
so i want to set for the template via --set
i already trying this in my template
command: {{ .Values.web.job.commands }}
i set using this
--set web.job.commands={sleep 3}
but the result
command: [sleep 3]
i want to have like the usual command style
command: ["sleep","3"]

It may be the problem here command: {{ .Values.web.job.commands }} ,In this usage, the data structure represented by {{ .Values.web.job.commands }} will be output here in a way similar to toString, so you get unexpected results [sleep 3]
If you want to get command: ["sleep","3"], you may need to
values.yaml
web:
job:
commands: '["wake", "2"]'
templates/xxx.tpl
...
command: {{ tpl .Values.web.job.commands . }}
...
cmd
helm template --debug test .
output
...
command: ["wake", "2"]
...
cmd:
helm template --debug test . --set web.job.commands='[\"sleep\"\, \"3\"]'
output
...
command: ["sleep", "3"]
...
However, the better usage in this scenario is as follows
values.yaml
web:
job:
commands:
- "wake"
- "2"
templates/xxx.tpl
...
command:
{{ toYaml .Values.web.job.commands | nindent x }}
...
(Modify x according to the actual indentation)
cmd
helm template --debug test .
output
...
command:
- wake
- "2"
...
cmd
helm template --debug test . --set web.job.commands={"sleep"\,"3"}
output
...
command:
- sleep
- 3
...

Related

adding multiple ips/domains to filebeat.yml output.elasticsearch via helm

If have multiple elasticsearch/logstash nodes that you want to point them to the output.elasticsearch:hosts in filebeat.yml from the helm chart you can do like that:
values.yaml
note: define hosts as a string, not an array
logstash:
hosts: 192.168.1.2:5444', '192.168.2.100:5544
filebeat-deployment.yml
env:
- name: ELASTICSEARCH_HOSTS
{{- range $key, $val := .Values.logstash }}
value: {{ . | quote }}
{{- end }}
the results will be :
$ helm exec filebeat-pod cat /etc/filebeat/filebeat.yml -n filebeat
setup.template.overwrite: true
setup.ilm.enabled: false
output.elasticsearch:
hosts: ['192.168.1.2:5444', '192.168.2.100:5544']
#username:
#password:
#ssl.verification_mode:
#ssl.certificate_authorities:
#ssl.certificate:
#ssl.key:
filebeat pod logs
$ helm logs filebeat-pod -n filebeat
2022-10-04T09:54:04.539Z INFO eslegclient/connection.go:99 elasticsearch url: http://192.168.1.2:5444
2022-10-04T09:54:04.539Z INFO eslegclient/connection.go:99 elasticsearch url: http://192.168.2.100:5544
NOTE!! - if you have other solutions by adding the multiple ips/domains via helm chart to the ENV container, just reply to this.
Hope you will find this post helpful for you

Helm - go templates : bad character U+002F '/'

I have this job yaml which work well:
apiVersion: batch/v1
kind: Job
metadata:
name: cli-commands
spec:
template:
spec:
containers:
- name: cli-commands
image: ubuntu:22.04
command: [ 'bash', '-c']
args:
- DEBIAN_FRONTEND=noninteractive apt update && apt install -y curl && curl -sL https://aka.ms/InstallAzureCLIDeb | bash &&
echo installation successful &&
az storage directory create --account-name {{ .Values.env.secret.azurestorageaccountname | b64dec}} --name {{ .Release.Namespace }}
--share-name {{ .Values.systemFilesPath | default (.Values.serviceName) }}
--account-key *****
restartPolicy: Never
The issue is:
I need to change this part of code (first version):
--share-name {{ .Values.systemFilesPath | default (.Values.serviceName) }}
into this (second version):
--share-name {{ .Values.systemFilesPath | default coreregciqa/(.Values.serviceName) }}
but the second version doesn't work and throws this error:
bad character U+002F '/'
How can I resolve that?
All what I want to do is to create a default value which looks like that (for example):
coreregciqa/mono ,but I dont know how to deal with the '/' in this case.
My first version works well because I don't have the '/' there, but my second version doesn't work.
I need your advice please.
Thanks a lot.
Default needs a value after it, and it can't automatically complete the splicing, you need to explicitly call the function to connect the string.
Like this:
--share-name {{ .Values.systemFilesPath | default (printf "coreregciqa/%s" .Values.serviceName) }}

how to pass array in helmfile

I have helmfile
releases:
- name: controller
values:
- values/valuedata.yaml
hooks:
{{ toYaml .Values.hooks }}
file with values
hooks:
- events: [ "presync" ]
showlogs: true
command: "bash"
args: [ "args"]
I want to pass the hooks from values how I can do it ?
I tried many ways and I got an error
This is the command
helmfile --file ./myhelmfile.yaml sync
failed to read myhelmfile.yaml: reading document at index 1: yaml: line 26: did not find expected '-' indicator
What you try to do is to inline part of the values.yaml into your template. Therefore you need to take care of the indentation properly.
In your case I think it'll be something like this:
releases:
- name: controller
values:
- values/valuedata.yaml
hooks:
{{ toYaml .Values.hooks | indent 6 }}
You can find a working example of a similar case here.

helm chart init container with multiple commands

we are defining a initContainer for our helm chart. relevant part is the following
initContainers:
- name: "set-volumes-init"
image: "IMAGE AND TAG"
command: ['sh', '-c', 'COMMAND 1 && COMMAND 2 && COMMAND 3']
volumeMounts:
- name: volume-summary
mountPath: /usr/summ
the question is: how do i make the "command" like have the different commands according to if a value is defined or not?
e.g: if i have the value: podx.val2 defined, i want the COMMAND 2 to be included, but if its not, then i dont want it.
same for other COMMANDS
If I were doing this, I'd build a custom image that contained the shell script, and have it controlled by environment variables.
#!/bin/sh
if [ -n "$DO_COMMAND_2" ]; then
command2
fi
The style you've written could work with a combination of YAML block syntax and Helm conditionals. This is probably harder to maintain and test, but something like this should work:
command: >-
command1
{{ if .Values.val2 }}
&& command2
{{ end }}
&& command3
The YAML >- syntax will cause everything indented after it to get folded into a single line, which helps the whitespace-control issues.

How do I access the current user in a helm chart template

I have a helm chart template, and I would like to use the result of whoami as a template variable. How do I do this?
So if my values.yaml file has:
env:
uniqueId: {{ whoami? }}
how might I do this?
note: I am on os x, so whoami I believe assumes a linux environment, however, in the spirit of this being deployment agnostic I presume there is a non-unix way of doing this.
The Helm Chart's "values.yaml" file is typically for default values. Anything that you'd like to override should be done at time of install/upgrade of the chart.
The Helm docs show a lot of different ways in which values can be used: https://github.com/kubernetes/helm/blob/master/docs/charts.md
In this case, one option is to set the value on the command line:
helm install -set env.whoami=$(id -un) ./your-chart.tgz
You could then have a value.yaml file like:
env:
whoami: "default"
Finally, you can use it in a template like:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Chart.Version }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: WHOAMI
value: {{ .Values.env.whoami }}
Obviously your template will vary, the above is just a snippet.