0

I am using a python subprocess module to execute kubectl exec command, the command is returning an error. Still I am getting a returncode of 0 and pod status of CRASHLOOPBACK state which is not valid.

Need help on fetching the correct returncode on execution of command. Executed command:

cmd = f"kubectl -n {cls.NS} exec -i {mainpod} -- sh -c \"/opt/user/data/bin/mdm -waitDb -v && /opt/user/data/bin/mdm -applySchema /etc/data/db/tables/ -v\""
result = subprocess.run(shlex.split(cmd))

Log output:

INFO : Connection Obtained - database ready
ERROR: /etc/data/db/tables/ is not a valid directory
INFO : Closed DB connection.

CompletedProcess(args=['kubectl', '-n', 'test', 'exec', '-i', 'db-pod', '--', 'sh', '-c', '/opt/user/data/bin/mdm -waitDb -v && /opt/user/data/bin/mdm -applySchema /etc/data/db/tables/ -v'], returncode=0)

How can I fetch the correct returncode in the above case?

13
  • Your Python code looks fine. It appears that the problem is that kubectl does not relay back the status code from sh -c correctly. See also github.com/kubernetes/kubernetes/issues/26424 Commented Mar 26, 2021 at 7:03
  • 1
    As an aside, pasting things into a string only so you can then shlex.split() it immediately seems like extra work. Try cmd = ["kubectl", "-n", cls.NS, "exec", "-i", mainpod, "--", "sh", "-c", "/opt/user/data/bin/mdm -waitDb -v && /opt/user/data/bin/mdm -applySchema /etc/data/db/tables/ -v"] (maybe add str() if your variables are not strings). Commented Mar 26, 2021 at 7:04
  • This stackoverflow.com/questions/66220772/… is for proper output receiving. My scenario is that when I receive an error log, i need the exact return code other than zero which is not working. Commented Mar 26, 2021 at 7:04
  • Yeah, I couldn't see what you were asking at first because of formatting problems; sorry for the spurious link. Commented Mar 26, 2021 at 7:05
  • @tripleee what else can i try out to fetch the status, as i need to execute kubectl command using python, any idea ? Commented Mar 26, 2021 at 9:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.