Skip to content

Instantly share code, notes, and snippets.

@thikade
Forked from tuxfight3r/openshift_cli_tricks.MD
Last active April 3, 2020 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thikade/527a0344d27265287cbbdf9e6ff6455f to your computer and use it in GitHub Desktop.
Save thikade/527a0344d27265287cbbdf9e6ff6455f to your computer and use it in GitHub Desktop.
openshift cli tricks - using go templates

openshift list all pods and their specs (requests/limits)

oc get pod -o jsonpath='{range .items[*]}{"SPEC:  \n  LIMITS  : "}{.spec.containers[*].resources.limits}{"\n  REQUESTS: "}{.spec.containers[*].resources.requests}{"\n"}{end}'

openshift list all pods and their specs with name (requests /limits)

oc get pod -o jsonpath='{range .items[*]}{"NAME:  "}{.metadata.name}{"\nSPEC:  \n  LIMITS  : "}{.spec.containers[*].resources.limits}{"\n  REQUESTS: "}{.spec.containers[*].resources.requests}{"\n\n"}{end}'

openshift list all nodes and their corresponding os/kernel verion

oc get nodes -o jsonpath='{range .items[*]}{"\t"}{.metadata.name}{"\t"}{.status.nodeInfo.osImage}{"\t"}{.status.nodeInfo.kernelVersion}{"\n"}{end}'

openshift patch build config with patch

oc patch bc/kube-ops-view -p '{"spec":{"resources":{"limits":{"cpu":"1","memory":"1024Mi"},"requests":{"cpu":"100m","memory":"256Mi"}}}}'

openshift display the images used by Replication Controller

oc get rc -n openshift-infra -o jsonpath='{range .items[*]}{"RC: "}{.metadata.name}{"\n Image:"}{.spec.template.spec.containers[*].image}{"\n"}{end}'

openshift display the requestor for namespace

oc get namespace ui-test -o template --template '{{ index .metadata.annotations "openshift.io/requester"  }}'

openshift display all projects and its creator sorted by creator

IFS=,; while read data1 data2; do printf "%-60s : %-50s\n" $data1 $data2; 
done < <( oc get projects -o template \
--template '{{range .items}}{{.metadata.name}},{{index .metadata.annotations "openshift.io/requester"}}{{"\n"}}{{end }}' |
sort -t, -k2 )

openshift fetch custom column name from metadata

oc get rolebinding -o=custom-columns=USERS:.userNames,GROUPS:.groupNames


for i in `oc get pv  -o=custom-columns=NAME:.metadata.name | grep pvc` ;
   do oc describe pv $i | grep Path |awk '{print $2}';
done

Getting Info using Go-templates

Source: https://bierkowski.com/openshift-morsels-displaying-more-information-using-go-template/ All mustaches are doubled"

List pod details (simple example using metadata)

oc get pod -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
nodejs-1-13vgw
nodejs-1-build

List a pod's container images separated by space

Using a go-lang range loop

$ oc get pod -o go-template='{{range .items}}{{.metadata.name}}{{"\t"}}{{range .spec.containers}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}'
pod1 image1 image2
pod1 image

Advanced usecase using if-conditionals: List all images of pods that run in the "restriced" SCC

The go-template is full programming language where you can create variables, use if/else statements. There are 2 nested range-loops with an if-clause selecting only those SCCs that are "restricted".

$ oc get pod -o go-template='{{range .items}}{{$scc := index .metadata.annotations "openshift.io/scc"}}{{if eq $scc "restricted"}}{{.metadata.name}}{{"\t"}}{{range .spec.containers}}{{.image}}{{" "}}{{end}}{{"\n"}}{{end}}{{end}}'
constellation-nodejs-1-13vgw 172.30.1.1:5000/constellation/constellation-nodejs@sha256:5507c05d75df3a2a364d78657b39ae3c5fe2442470154564ce8d839246db55cc
constellation-nodejs1-1-3d0rq 172.30.1.1:5000/constellation/constellation-nodejs1@sha256:533f95953f89ebfb4de739fede8e4b6c6f38622d2f6a4dec6dd249baf2eb0d8e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment