Kubernetes /bin/bash with -c argument returns - : invalid option

11/5/2021

I have this definition in my values.yaml which is supplied to job.yaml

command: ["/bin/bash"]
args: ["-c", "cd /opt/nonrtric/ric-common/ansible/; cat group_vars/all"]

However, after the pod initializes, I get this error: /bin/bash: - : invalid option

If i try this syntax:

command: ["/bin/sh", "-c"]
args: 
   - >
   cd /opt/nonrtric/ric-common/ansible/ &&
   cat group_vars/all

I get this error: Error: failed to start container "ric-register-avro": Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "/bin/sh -c": stat /bin/sh -c: no such file or directory: unknown

Both sh and bash are supplied in the image, which is CentOS 7

job.yaml

---
apiVersion: batch/v1
kind: Job
metadata:
  name: ric-register-avro
spec:
  backoffLimit: 0
  template:
    spec:
      containers:
        - image: "{{ .Values.ric_register_avro_job.image }}"
          name: "{{ .Values.ric_register_avro_job.name }}"
          command: {{ .Values.ric_register_avro_job.containers.command }}
          args: {{ .Values.ric_register_avro_job.containers.args }}
          volumeMounts:
          - name: all-file
            mountPath: "/opt/nonrtric/ric-common/ansible/group_vars/"
            readOnly: true
            subPath: all
      volumes:
        - name: all-file
          configMap:
            name: ric-register-avro--configmap
      restartPolicy: Never

values.yaml

global:
  name: ric-register-avro
  namespace: foo-bar

ric_register_avro_job:
  name: ric-register-avro
  all_file:
    rest_api_url: http://10.230.227.13/foo
    auth_username: foo
    auth_password: bar
  backoffLimit: 0
  completions: 1
  image: 10.0.0.1:5000/5gc/ric-app
  containers:
    name: ric-register-avro
    command: ["/bin/bash"]
    args: ["-c cd /opt/nonrtric/ric-common/ansible/; cat group_vars/all"]
  restartPolicy: Never
-- molitann
docker
kubernetes
kubernetes-helm

2 Answers

11/5/2021

I use this:

command: ["/bin/sh"]
args: ["-c", "my-command"]

Trying this simple job I've no issue:

apiVersion: batch/v1
kind: Job
metadata:
  name: foo
spec:
  template:
    spec:
      containers:
      - name: foo
        image: centos:7
        command: ["/bin/sh"]
        args: ["-c", "echo 'hello world'"]
      restartPolicy: Never
-- Marco Caberletti
Source: StackOverflow

11/5/2021

In your Helm chart, you directly specify command: and args: using template syntax

command: {{ .Values.ric_register_avro_job.containers.command }}
args: {{ .Values.ric_register_avro_job.containers.args }}

However, the output of a {{ ... }} block is always a string. If the value you have inside the template is some other type, like a list, it will be converted to a string using some default Go rules, which aren't especially useful in a Kubernetes context.

Helm includes two lightly-documented conversion functions toJson and toYaml that can help here. Valid JSON is also valid YAML, so one easy approach is just to convert both parts to JSON

command: {{ toJson .Values.ric_register_avro_job.containers.command }}
args: {{ toJson .Values.ric_register_avro_job.containers.args }}

or, if you want it to look a little more like normal YAML,

          command:
{{ .Values.ric_register_avro_job.containers.command | toYaml | indent 12 }}
          args:
{{ .Values.ric_register_avro_job.containers.args | toYaml | indent 12 }}

or, for that matter, if you're passing a complete container description via Helm values, it could be enough to

        containers:
          - name: ric_register_avro_job
{{ .Values.ric_register_avro_job.containers | toYaml | indent 10 }}

In all of these cases, I've put the templating construct starting at the first column, but then used the indent function to correctly indent the YAML block. Double-check the indentation and adjust the indent parameter.

You can also double-check that what's coming out looks correct using helm template, using the same -f option(s) as when you install the chart.

(In practice, I might put many of the options you show directly into the chart template, rather than making them configurable as values. The container name, for example, doesn't need to be configurable, and I'd usually fix the command. For this very specific example you can also set the container's workingDir: rather than running cd inside a shell wrapper.)

-- David Maze
Source: StackOverflow