0

Using popen to run K8s commands. The SERVICE_ACCOUNT does not produce any output. I get <none>. I have escaped the \" and \\

Original command:

oc get pods -n <namespace> -o custom-columns=POD_NAME:.metadata.name,SERVICE_ACCOUNT:.spec.serviceAccount,SCC_PROFILE:".metadata.annotations.openshift\.io/scc"

Code:

output = subprocess.Popen(["oc","get","pods","-n",smf_namespace,"-o","custom-columns=POD_NAME:.metadata.name,SERVICE_ACCOUNT:.spec.serviceAccount,SCC_PROFILE:\".metadata.annotations.openshift\\.io/scc\""],stdout=subprocess.PIPE,encoding='utf8')
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • You appear to be trying to embed literal double quotes in a double-quoted string without escaping them. – chepner Jan 06 '23 at 20:40
  • 1
    (The double quotes in the original command appear to be for the benefit of the shell--and unnecessary at that--, not part of the command itself.) – chepner Jan 06 '23 at 20:42
  • Just get rid of both `\"`. – Barmar Jan 06 '23 at 20:51
  • And use a raw string so you don't need to escape backslashes. – Barmar Jan 06 '23 at 20:52
  • This question is very much a duplicate -- we often have people thinking that quotes added as shell syntax are actually part of the syntax of the command that they're running; but it's just the shell that needs them. If you get rid of the shell, you also get rid of the need for shell syntax. – Charles Duffy Jan 06 '23 at 21:52
  • To see what your command looks like after the shell syntax is taken out, try running the shell command `printf '%s\n' oc get pods -n namespace -o custom-columns=POD_NAME:.metadata.name,SERVICE_ACCOUNT:.spec.serviceAccount,SCC_PROFILE:".metadata.annotations.openshift\.io/scc"` -- each line should be a separate Python list element. If any syntax element -- quotes, backslash, etc -- isn't there in the printf output when you run that command, that syntax element doesn't belong in your Python list either. – Charles Duffy Jan 06 '23 at 21:54
  • Removing the " from the original command fixed it. Can I get a link that explains this a bit better. This is just the start of the script I am making. – Georgios Gontikas Jan 07 '23 at 03:15

0 Answers0