Friday, May 27, 2022

[SOLVED] openshift commands to catch POD name programmatically/scripting

Issue

I have pods in my open shift and want to work on multiple open shift applications. Lets say like below

sh-4.2$ oc get pods

NAME                                  READY     STATUS      RESTARTS   AGE
jenkins-7fb689fc66-fs2xb              1/1       Running     0          4d
jenkins-disk-check-1587834000         0/1       Completed   0          21h

NAME                                 READY     STATUS    RESTARTS   AGE
jenkins-7fb689fc66-gsz9j              0/1       Running   735        9d
jenkins-disk-check-1587834000    

NAME                                READY     STATUS    RESTARTS   AGE
jenkins-9euygc66-gsz9j               0/1       Running   735        9d

I have tried with below command

oc get pods

export POD=$(oc get pods | awk '{print $1}' | grep jenkins*)

I want to find the pods starting with numbers "jenkins-7fb689fc66-fs2xb",jenkins-9euygc66-gsz9j, etc... using scripting and need to ignore disk check pods. If i catch the above pods and need to execute the terminal and run some shell commands via programmatically. Can someone help me on this?


Solution

kubectl get (and by extension oc get) is a very versatile tool. Unfortunately, after looking around online for awhile, you will definitely not be able to do Regex without relying on an external tool like awk or grep. (I know this wasn't exactly what you were asking, but I figured I'd at least try to see if it's possible.

With that said, there are a couple of tricks you can rely on to filter your oc get output before you even have to pull in external tools (bonus points because this filtering occurs on the server before it even hits your local tools).

I first recommend running oc get pods --show-labels, because if the pods you need are appropriately labeled, you can use a label selector to get just the pods you want, e.g.:

oc get pods --selector name=jenkins
oc get pods --selector <label_key>=<label_value>

Second, if you only care about the Running pods (since the disk-check pods look like they're already Completed), you can use a field selector, e.g.:

oc get pods --field-selector status.phase=Running
oc get pods --field-selector <json_path>=<json_value>

Finally, if there's a specific value that you're after, you can pull that value into the CLI by specifying custom columns, and then greping on the value you care about, e.g.:

oc get pods -o custom-columns=NAME:.metadata.name,TYPES:.status.conditions[*].type | grep "Ready"

The best thing is, if you rely on the label selector and/or field selector, the filtering occurs server side to cut down on the data that ends up making it to your final custom columns, making everything that much more efficient.


For your specific use case, it appears that simply using the --field-selector would be enough, since the disk-check pods are already Completed. So, without further information on exactly how the Jenkins pod's JSON is constructed, this should be good enough for you:

oc get pods --field-selector status.phase=Running


Answered By - Will Gordon
Answer Checked By - Marie Seifert (WPSolving Admin)