'including shell script as entrypoint in helm3
I am using helm-3. My use case is, the user can run multiple line shell script/command as the entry point of my pod. So I should be able to run, whatever the commands/lines user entered as input in values.yaml. values.yaml
shell:
evaluate_events:
ev-cron-1:
schedule: "1 1 1 1 1"
event-list: |-
echo "Hello one"
echo "hello two"
echo "hello three"
Deployment.yaml
containers:
- name: events-batch
image: {{ $.Values.image.imageName }}:{{ $.Values.image.tag }}
imagePullPolicy: {{ $.Values.image.pullPolicy }}
command:
- sh
- -c
args:
- {{ toYaml .Values.shell.evaluate_events.ev-cron-1.event-list | indent 6 }}
Expected output is
containers:
- name: events-batch
image: {{ $.Values.image.imageName }}:{{ $.Values.image.tag }}
imagePullPolicy: {{ $.Values.image.pullPolicy }}
command:
- sh
- -c
args:
- echo "Hello one"
echo "hello two"
echo "hello three"
But getting the below error when trying to generate a template
Error: YAML parse error on serverapp/templates/test.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type releaseutil.SimpleHead
what are the changes required for the above code to work as expected?
Solution 1:[1]
To my knowledge, you need to provide a pipe indicating that this element of the array is multiline.
You need to adjust nindent so that it will be one indentation further than the array hyphen. If it's in the standard deployment, 14 should be correct. But you need to look where you are using this, I cannot tell from your question.
containers:
- name: {{ .Chart.Name }}
command:
- "sh"
- "-c"
- | {{- .Values.script | nindent 14 }}
In values, I put the below for demo purposes.
script: |
echo "Hello, World!"
echo "foo bar baz"
It's getting rendered as, which is the correct syntax.
containers:
- name: test
command:
- "sh"
- "-c"
- |
echo "Hello, World!"
echo "foo bar baz"
You can cross-check if it comes out correct for you by using the helm template command.
Also, note that what you are doing is actually not including the script as entrypoint, really. You are trying to pass it as command. Using the above approach should work in both places.
I think it may be more clean to let the user just provide the full command. If they want to run a multi line script, which is bad practice IMO, then they can write it as such.
# values.yaml
command:
- sh
- -c
- |
echo "Hello, World!"
echo "foo bar baz"
# deplyoment template yaml
containers:
- name: {{ .Chart.Name }}
command: {{- .Values.command | toYaml | nindent 12 }}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
