syntax error in kubernetes pod.yaml running shell command

12/31/2019

Team, I have a working command but cannot figure out the syntax to run from pod.yaml. any hint?

apiVersion: v1
kind: Pod
metadata:
  name: count_mounts
  labels:
    purpose: demonstrate_command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["/bin/bash"]
    args: ["-c", echo $HOSTNAME && mount | grep -Ec '/dev/sd.*\<csi' | awk '$0 <= 64  { print "Mounts are less than 64, that is found", $0 ;} $0 > 64  { print "Mounts are more than 64", $0 ;}']
  restartPolicy: OnFailure

output:

> here I enclosed whole command in double quotes
error: error parsing /home/pod.yaml: error converting YAML to JSON: yaml: line 14: found unknown escape character

> here I left whole command as is
error: error parsing /home/pod.yaml: error converting YAML to JSON: yaml: line 16: found unexpected end of stream

expected output:

node123
Mounts are less than 64, that is found 0

on local system without pod

└─ $ ▶ echo $HOSTNAME && mount | grep -Ec '/dev/sd.*\<csi' | awk '$0 <= 64  { print "Mounts are less than 64, that is found", $0 ;} $0 > 64  { print "Mounts are more than 64", $0 ;}'

node123
Mounts are less than 64, that is found 0
-- AhmFM
docker
kubernetes-pod
shell

1 Answer

12/31/2019

I got around with this with below approach but would definitely like to know how to use nested quotes?

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["/bin/bash", "-c"]
    args:
      - |
        echo $HOSTNAME && mount | grep -Ec '/dev/sd.*\<csi' | awk '$0 <= 64  { print "Mounts are less than 64, that is found", $0 ;} $0 > 64  { print "Mounts are more than 64", $0 ;}'
  restartPolicy: OnFailure

resulted output:

kubectl logs pod/command-demo
node123
Mounts are less than 64, that is found 0
-- AhmFM
Source: StackOverflow