Improve readability for command in an init container kubernetes

3/5/2020

I have an init container and am running a command in it which takes a ton of parameters so I have something like

command: ['sh', '-c', 'tool input1 input2 output
-action param1 -action param2 -action param3 -action param4 -action param5 -action param6 -action param7 -action param7 -action param8 -action param9 -action param10 ']

This highly decreases the readability of the command. Is there someway I can improve this like passsing this a a separate array ?

-- The_Lost_Avatar
kubernetes

2 Answers

3/5/2020

You can make command's args as a YAML list:

  initContainers:
  - name: init-container
    image: busybox:1.28
    command: ["/bin/sh","-c"]
    args:
      - -action
      - param1
      - -action
      - param2
    .
    .
    .
-- Sithroo
Source: StackOverflow

3/6/2020

Another tip is to do it via command, so save your time that you needn't manually do that.

kubectl run nginx --image=nginx --generator=run-pod/v1 --dry-run -o yaml  -- tool input1 input2 output -action param1 -action param2 -action param3 -action param4 -action param5 -action param6 -action param7 -action param7 -action param8 -action param9 -action param10
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - args:
    - tool
    - input1
    - input2
    - output
    - -action
    - param1
    - -action
    - param2
    - -action
    - param3
    - -action
    - param4
    - -action
    - param5
    - -action
    - param6
    - -action
    - param7
    - -action
    - param7
    - -action
    - param8
    - -action
    - param9
    - -action
    - param10
    image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Above dry-run command will generate the yaml for option --args, you copy & paste it into the part of original init container, make it so simple and you can do it less in one munite

You still need keep the part of command, don't forget

    command: ["/bin/sh","-c"]
-- BMW
Source: StackOverflow