helm chart init container with multiple commands

10/2/2018

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

-- Fernando Castilla Ospina
kubernetes-helm

1 Answer

10/3/2018

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.

-- David Maze
Source: StackOverflow