Skip to content

Instantly share code, notes, and snippets.

@eduardocerqueira
Forked from cvarjao/.gitignore
Created October 31, 2019 19:17
Show Gist options
  • Save eduardocerqueira/6036731d1c202811fd613d98b1adaf91 to your computer and use it in GitHub Desktop.
Save eduardocerqueira/6036731d1c202811fd613d98b1adaf91 to your computer and use it in GitHub Desktop.
OpenShift oc command line cheatsheet

Find available object types

# at namespace level
oc api-resources --namespaced=true

# at cluster level
oc api-resources --namespaced=false

Obect type documentation

oc explain bc
oc explain bc.spec
oc explain bc.spec.completionDeadlineSeconds

oc explain dc
oc explain dc.spec
oc explain dc.spec.template
oc explain dc.spec.template.spec
oc explain dc.spec.template.spec.volumes

Find POD assigned node (node name)

oc get pod/<pod> -o custom-columns=namespace:metadata.namespace,name:metadata.name,nodeName:spec.nodeName

Find routes that have not been admitted

oc get route --all-namespaces -o json | jq '.items[] | select ( .status.ingress[].conditions[] | .type == "Admitted" and .status == "False")'

## Prettier output (columns)
oc get route --all-namespaces -o json | jq -r '["namespace", "name", "reason"], (.items[] | select ( .status.ingress[].conditions[] | .type == "Admitted" and .status == "False") | [.metadata.namespace, (.kind +"/"+.metadata.name), ( .status.ingress[].conditions[].reason)]) | @csv' | column -ts,

What does all means?

# as of 3.9
oc get '--raw=/apis?limit=500' | jq  -crM '.groups[] | .preferredVersion.groupVersion' | ( xargs -I {} oc get '--raw=/apis/{}?limit=500' | jq -crM '.resources[] | select(.categories | values | .[]  | . == "all" ) | .name' )

Reference: openshift/origin#18396 (comment)

List ALL resources/types

oc get '--raw=/apis?limit=500' | jq  -crM '.groups[] | .preferredVersion.groupVersion' | ( xargs -I {} oc get '--raw=/apis/{}?limit=500' | jq -crM '.resources[] | .name' )

List of Projects

oc get project -o custom-columns=name:.metadata.name,project-set:.metadata.labels.name,product:.metadata.labels.product,product-owner:.metadata.annotations.product-owner,product-lead:.metadata.annotations.product-lead


oc get project -o custom-columns=NAME:.metadata.name,PROJECT-SET:.metadata.labels.name,DISPLAY-NAME:.metadata.annotations.openshift.io/display-name

oc get project -o 'custom-columns=NAME:.metadata.name,PROJECT-SET:.metadata.labels.name,TEAM:.metadata.labels.team,DISPLAY-NAME:.metadata.annotations.openshift\.io/display-name'

oc get project -o 'custom-columns=NAME:.metadata.name,PROJECT-SET:.metadata.labels.name,TEAM:.metadata.labels.team,MCIO:.metadata.labels.mcio,DISPLAY-NAME:.metadata.annotations.openshift\.io/display-name'

Number of PVC per namespace

oc get pvc -o custom-columns=namesapce:.metadata.namespace --no-headers --all-namespaces | sort | uniq -c | sort -n

Estimated disk consumption of images for a namespace

#currently selected project/namespace

# Option 1
oc get imagestreams -o json | jq -r '.items[] | {imageStream:.metadata.name, image:.status.tags[]?.items[].image} | (.imageStream + "@" + .image)' | xargs -I {} oc get 'imagestreamimage/{}' -o json |  jq -r '.image.dockerImageLayers[] | (.name + "\t" + (.size | tostring))'  | sort | uniq | awk '{ sum += $2 } END { print sum }' | awk '{ foo = $1 / 1024 / 1024 / 1024 ; print foo "GB" }'

# Option 2
oc get imagestreams -o json | jq -r '.items[] | {imageStream:.metadata.name, image:.status.tags[]?.items[].image} | (.imageStream + "@" + .image)' | xargs -I {} oc get 'imagestreamimage/{}' -o json |  jq -r '.image.dockerImageLayers[] | {name: .name, size: .size}' | jq -sr 'unique_by(.name) | .[].size' | awk '{ sum += $1 } END { print (sum / 1024 / 1024 / 1024) "GB" }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment