Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass dynamic arguments to a helm chart that runs a job

I'd like to allow our developers to pass dynamic arguments to a helm template (Kubernetes job). Currently my arguments in the helm template are somewhat static (apart from certain values) and look like this

      Args:
        --arg1
        value1
        --arg2
        value2
        --sql-cmd
        select * from db

If I were run a task using the docker container without Kubernetes, I would pass parameters like so:

docker run my-image --arg1 value1 --arg2 value2 --sql-cmd "select * from db"

Is there any way to templatize arguments in a helm chart in such way that any number of arguments could be passed to a template.

For example.

cat values.yaml
...
arguments: --arg1 value1 --arg2 value2 --sql-cmd "select * from db"
...

or

cat values.yaml
...
arguments: --arg3 value3
...

I've tried a few approaches but was not successful. Here is one example:

     Args:          
      {{  range .Values.arguments }}
        {{ . }}
      {{ end }}
like image 706
Aaron Bandelli Avatar asked Oct 03 '18 22:10

Aaron Bandelli


People also ask

Which files pass static graph values?

yaml file is used to pass values into the Release helm chart. The file contains default parameters that you can override.

What is appVersion in Helm chart?

Chart versions and appVersions Each Helm chart has the ability to define two separate versions: The version of the chart itself ( version field in Chart. yaml ). The version of the application contained in the chart ( appVersion field in Chart.


3 Answers

Yes. In values.yaml you need to give it an array instead of a space delimited string.

cat values.yaml
...
arguments: ['--arg3', 'value3', '--arg2', 'value2']
...

or

cat values.yaml
...
arguments:
- --arg3
- value3
- --arg2
- value2
...

and then you like you mentioned in the template should do it:

     args:          
      {{  range .Values.arguments }}
        - {{ . }}
      {{ end }}

If you want to override the arguments on the command line you can pass an array with --set like this:

--set arguments={--arg1, value1, --arg2, value2, --arg3, value3, ....}
like image 185
Rico Avatar answered Sep 17 '22 07:09

Rico


In your values file define arguments as:

extraArgs:
  argument1: value1
  argument2: value2
  booleanArg1:

In your template do:

    args:
{{- range $key, $value := .Values.extraArgs }}
    {{- if $value }}
    - --{{ $key }}={{ $value }}
    {{- else }}
    - --{{ $key }}
    {{- end }}
{{- end }}
like image 45
Acid R Avatar answered Sep 21 '22 07:09

Acid R


Rico's answer needed to be improved. Using the previous example I've received errors like:

templates/deployment.yaml: error converting YAML to JSON: yaml or

failed to get versionedObject: unable to convert unstructured object to apps/v1beta2, Kind=Deployment: cannot restore slice from string

This is my working setup with coma in elements: ( the vertical format for the list is more readable )

cat values.yaml
...
arguments: [
    "--arg3,", 
    "value3,",
    "--arg2,",
    "value2,",
]
...

in the template should do it:

          args: [
{{  range .Values.arguments }}
{{ . }}
{{ end }}
          ]
like image 22
Mihail Kuzmich Avatar answered Sep 20 '22 07:09

Mihail Kuzmich