openshift commands to catch POD name programmatically/scripting
Asked Answered
S

3

2

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?

Sufferance answered 26/4, 2020 at 14:21 Comment(0)
S
5

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
Shropshire answered 26/4, 2020 at 18:46 Comment(9)
Hi Will, Thanks a lot for your explanation. oc get pods --field-selector status.phase=Running command is more enough to catch the POD name. Thanks a lot for the brief explanationSufferance
Hi @Will Gordon, I used to run the command you posted and it is working manually but unfortunately it is not giving exact value from my script call. NAME READY STATUS RESTARTS AGE jenkins-7fb689fc66-fs2xb 1/1 Running 0 5d jenkins-disk-check-1587920400-2z9vw 0/1 Completed 0 14h Error: unknown flag: --field-selectorSufferance
I ran this command : POD=oc get pods --field-selector status.phase=Running | awk '{print $1}' | grep jenkins-* Do i need to add anything here?Sufferance
"Unknown flag" is usually a version issue. What are you seeing from oc version?Shropshire
I got this info : (sh-4.2$ oc version oc v3.11.0-alpha.0+1ff2229-9 kubernetes v1.10.0+b81c8f8 features: Basic-Auth GSSAPI Kerberos SPNEG). I see this commad will work after 3.9 version as per knowledge . This command works perfectly in OC Application Terminal but somehow calling from script is failingSufferance
Hi could you update your question with the output from 2 different commands (it formats better if you put it in the question). 1) oc get pods --field-selector status.phase=Running and 2) oc get pods --field-selector status.phase=Running -o custom-columns=NAME:.metadata.name | grep jenkins-*.Shropshire
sh-4.2$ oc get pods --field-selector status.phase=Running -o custom-columns=NAME:.metadata.name | grep jenkins-* jenkins-7fb689fc66-fs2xb sh-4.2$ oc get pods --field-selector status.phase=Running NAME READY STATUS RESTARTS AGE jenkins-7fb689fc66-fs2xb 1/1 Running 0 5dSufferance
2) command gives exact output but problem is getting Error: unknown flag: --field-selector while i trigger this script from Jenkins jobs.Sufferance
So the second command is working as expected, but you're still seeing an error message?! So weird, you could just ignore error output by running oc get pods --field-selector status.phase=Running -o custom-columns=NAME:.metadata.name 2>/dev/null | grep jenkins-*Shropshire
I
1

Assuming that you need to print jenkins id in first field, could you please try following.

awk 'match($0,/jenkins[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file

Explanation: Adding explanation for above code.

awk '                                ##Starting awk program from here.
match($0,/jenkins[^ ]*/){            ##Using match function in which mentioning regex jenkins till spacein current line.
  print substr($0,RSTART,RLENGTH)    ##Printing sub-string in current line where starting point is RSTART till RLENGTH value.
}
' Input_file                         ##Mentioning Input_file name here.
Internationalist answered 26/4, 2020 at 14:31 Comment(5)
But i need to print the jenkins-7fb689fc66-fs2xb value and to ignore the disk check value of the podsSufferance
@KandikuppaVinod, I didn't get it, could you please explain more on this one, what is not working?Internationalist
I need to get the only value like jenkins-7fb689fc66-fs2xb and need to grab thae value and keep it in ${POD} variable.and should not print jenkins-disk-check-1587920400-2z9vw value.Sufferance
@KandikuppaVinod, I am still not sure if I get it, how about this awk 'match($0,/jenkins-7fb689fc66[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file try this once?Internationalist
Thanks, But one thing which i want to fetch that jenkins-7fb689fc66 value dynamically without passing 7fb689fc66 I mean the value "jenkins-<starts with number> " always want to find and get that string. (Ex : awk '{print $1}' | grep jenkins-* ) - O/p: jenkins-7fb689fc66Sufferance
A
0

Adding this answer for others reference. You could use in this way.

export POD=$(oc get pods | awk '{print $1}' | grep jenkins* | grep -v jenkins-disk-check)
Algarroba answered 16/3, 2022 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.