How can I use the Kubernetes Pytnon API to execute a command?

4/22/2020

Lets say I want my Python3 program to query slurm for its version. The equivalent kubectl commands that I currently run are

# get the pod name
kubectl get pod -n mynamespace -l app=slurmctld -o jsonpath=‘{.items[0].metadata.name}’

# execute
kubectl exec -n mynamespace -c slurmctld slurmctld-429c7462ee-4hgv7 -- sinfo --version

Where slurmctld-429c7462ee-4hgv7 is the name of the slurm pod I determined in the first command.

I want to use the Kubernetes Python client API. I currently use it to perform the first step no problem using the following code in my Python3 program.

podname = kubernetes.client.CoreV1Api().list_namespaced_pod('mynamespace', label_selector='app=slurmctld')
podname = podname.items[0].metadata.name

How could I use the API to do perform the second step?

-- DeepDeadpool
kubernetes
python-3.x

0 Answers