Home > Enterprise >  Is there a way to exec into an open shift pod and capture the output of a command using the python s
Is there a way to exec into an open shift pod and capture the output of a command using the python s

Time:01-02

The project I am currently working on has multiple pods running in Redhat Openshift. I would like to be able to run a python script to view the version of each pod currently running in the project instead of manually going into each pod and checking for the version.

Heres the basic pseudocode for what I am trying to achieve:

  • Obtain openshift projects
  • Use the project which I would like to view running pods
  • Get all current pods in the project
  • Loop through each pod, exec into them, and get the version

I have been able to get all the way up to the last step by using the subprocess module in python. What I am trying to figure out is if there is a way to exec into the pod, and then run a command, capture the output, then exit. Here is the code I have so far

import subprocess
from subprocess import PIPE


if __name__ == '__main__':

    projects = subprocess.run(['oc', 'projects'], capture_output=True)
    if projects.returncode != 0:
        print('Error obtaining open shift projects, make sure you are logged in and have the correct permissions')
    project = subprocess.run(['oc', 'project', 'my-project'])

    pods = subprocess.run(['oc', 'get', 'pods'], stdout=PIPE).stdout.splitlines()
    for pod in pods:
        curr_pod = pod.split()[0].decode()
        exec = subprocess.run(['oc', 'exec',  '-it', curr_pod, '--', 'bash'], capture_output=True, shell=True)
        print(curr_pod)
        if exec.returncode == 0:
            version = subprocess.run(['ls'])
            print(curr_pod, version.stdout)
            subprocess.run(['exit'], shell=True)


Exec seems to do the trick for getting into the pod, but the commands prior don't give me the output expected. If I was able to just 'ls' each pod once I got into them and do that for each pod that would be more than enough to get the job done. Thanks in advance, this is my first Stack Overflow question so I'm happy to elaborate more.

CodePudding user response:

import subprocess
from subprocess import PIPE


if __name__ == '__main__':

    projects = subprocess.run(['oc', 'projects'], capture_output=True)
    if projects.returncode != 0:
        print('Error obtaining open shift projects, make sure you are logged in and have the correct permissions')
    project = subprocess.run(['oc', 'project', 'my-project'])

    pods = subprocess.run(['oc', 'get', 'pods', '--no-headers'], stdout=PIPE).stdout.splitlines()
    for pod in pods:
        curr_pod = pod.split()[0].decode()
        exec = subprocess.run(['oc', 'exec', curr_pod, '--', 'ls'])
        print(curr_pod, exec.stdout)

I have modified the oc exec command slightly to be able to print the output of ls to the terminal. Also added --no-headers to avoid printing the header filed while running oc get pods. Hope this is what you are looking for.

CodePudding user response:

It looks like you would like to only run one command. The oc cli allows you to execute a single command without the need to shell into the pod with an interactive pod

$ oc execute <pod> "ls"

Would do what I think you are looking for.

So to re-write your code a little I would not switch projects like you do but use the -n to specify the namespace/project. This is un tested so it may have syntax errors

import subprocess
from subprocess import PIPE


if __name__ == '__main__':

    projects = subprocess.run(['oc', 'projects'], capture_output=True)
    if projects.returncode != 0:
        print('Error obtaining open shift projects, make sure you are logged in and have the correct permissions')

    pods = subprocess.run(['oc', 'get', 'pods', '-n', 'my-project'], stdout=PIPE).stdout.splitlines()
    for pod in pods:
        curr_pod = pod.split()[0].decode()
        version_result = subprocess.run(['oc', '-n', 'my-project', 'exec', curr_pod, 'ls'], stdout=PIPE).stdout.splitlines()
        print(curr_pod)
        print(version_result)
  • Related