Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

Shaping the future of IT, one connection at a time.

Create account Log in
Balkrishna Pandey
Balkrishna Pandey

Posted on • Updated on

oc get all pods handy cheat sheet

  • Pod which are not running Across all namespaces
oc get po -A \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($3!=$4) { found=1; print $0}} END { if (!found) print "Not Found"}'
Enter fullscreen mode Exit fullscreen mode
  • Delete all pods which are not running across all namespaces
oc get po -A \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($3!=$4) { found=1; print $0}} END { if (!found) print "Not Found"}' \
| awk  '{ printf "oc delete po %s -n %s \n", $2 , $1}'
Enter fullscreen mode Exit fullscreen mode

Next Copy and paste output from above command or run above command with |bash at the end.

  • Delete all failed pods in a single namespace where context is set
oc get po -o wide \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($2!=$3) { found=1; print $0}} END { if (!found) print "Not Found"}' \
| awk  '{ printf "oc delete po %s  \n", $1 }'
Enter fullscreen mode Exit fullscreen mode

Next Copy and paste output from above command or run above command with |bash at the end.

  • Pods which are not running on openshift-ingress namespace.
oc get po -n openshift-ingress \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($2!=$3) { found=1; print $0}} END { if (!found) print "Not Found"}'
Enter fullscreen mode Exit fullscreen mode

Here I am comparing column 2 with 3, notice header changes in oc get pods -A vs oc get pods

[root@bastion ~]# oc get pods | head -n 1
NAME          READY   STATUS    RESTARTS   AGE
[root@bastion ~]# oc get pods -A | head -n 1
NAMESPACE                                          NAME                                                              READY   STATUS                  RESTARTS   AGE
Enter fullscreen mode Exit fullscreen mode
  • Count all Pods per Nodes
oc get nodes -o custom-columns=NODE:.metadata.name --no-headers \
| while read node; do 
echo "$node $(oc get pods --all-namespaces=true --field-selector spec.nodeName=$node --no-headers | wc -l)"; 
done
Enter fullscreen mode Exit fullscreen mode
  • Count Pods per Namespace
oc get --all-namespaces=true pods \
| awk '{a[$1]++} END {for (i in a) {print i, a[i]}}'
Enter fullscreen mode Exit fullscreen mode
  • Pods which are running since 24h
oc get pods --field-selector=status.phase=Running -o json \
| jq '.items[] | select(.metadata.creationTimestamp | fromdateiso8601 > (now - 86400)) | .metadata.name'
Enter fullscreen mode Exit fullscreen mode
  • Pods which are running since 24h in a specific node
oc get pods  --field-selector=status.phase=Running,spec.nodeName=worker-11 -o json \
| jq '.items[] | select(.metadata.creationTimestamp | fromdateiso8601 > (now - 86400)) | .metadata.name'
Enter fullscreen mode Exit fullscreen mode
  • List all operators installed in OCP
oc get csv --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{end}' | sort | uniq
Enter fullscreen mode Exit fullscreen mode
  • Cleanup quay operator. Command is applicable for other operators as well, make sure to adjust the command.
JOB_NAME=$(oc get job -n openshift-marketplace -o json | jq -r '.items[] | select(.spec.template.spec.containers[].env[].value|contains ("quay")) | .metadata.name')
echo $JOB_NAME
oc delete job $JOB_NAME -n openshift-marketplace
oc delete configmap $JOB_NAME -n openshift-marketplace
for i in `oc get ip -n openshift-operators | grep quay | awk '{ print $1}'`; do echo $i; oc delete ip $i -n openshift-operators; done;
CURRENT_CSV=$(oc get subscription quay-operator -n openshift-operators -o=jsonpath='{.status.currentCSV}')
echo $CURRENT_CSV
oc delete subscription quay-operator -n openshift-operators
oc delete csv  $CURRENT_CSV -n openshift-operators
Enter fullscreen mode Exit fullscreen mode

Discussion (0)