1

Is there a simple method (that won't require googling at every use) to get names of all deployment configs with no running pods (scaled to 0) in Kubernetes / Openshift? Methods without JSON tokens and awk please.

The docs of oc get dc --help are way too long to decipher for the occasional need.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
  • 1
    Can you clarify your question what exactly you want to show? DeploymentConfigs with no healthy pods? DeploymentConfigs that are scaled to 0? To provide a good answer I would also be interested to hear your use-case? – Simon May 24 '20 at 20:16
  • Added this info (zero replicas). Some of our data scientists scale down their pods to zero replicas instead of properly deleting deployment configs and thus we need to clean up those stopped pods for them occasionally,.. by deleting dc's for stopped pods only. – mirekphd May 24 '20 at 20:27
  • why without awk? – suren May 24 '20 at 23:39
  • 1
    There's no way to avoid long complex commands in the kubernetes world. Write yourself an easy-to-use memorably-named bash or python script that does what you need, and save it in your ```~/bin``` directory. Put a few comments in the top of the script explaining what it does and whence you sourced the information. You will thank yourself later, and your colleagues will thank you when you share it with them. – Ed Randall May 30 '20 at 09:28

1 Answers1

4

The only CLI arg for advanced filtering without working with JSON is a --field-selector, but it has a limited scope which not include spec.replicas field.

So, there will be some magic around JSON with other flag - jsonpath.

Here is a command to filter and print names of all deployments which are scaled to 0:

kubectl get deployments --all-namespaces -o=jsonpath='{range .items[?(@.spec.replicas==0)]}{.metadata.name}{"\n"}{end}'

Jsonpath reference is here.

Anton Kostenko
  • 8,200
  • 2
  • 30
  • 37
  • It certainly works, so I upvoted, but with accepting I will wait for API changes that get rid of the unnecessary complexity and hence fragility here (gaining some traction here on SO will also help with feature request on Github) – mirekphd May 30 '20 at 10:59
  • Openshift users please note that `deployments` exist only for running pods, so if we look for zero-replica pods, this method will find consistently nothing, until we switch to deployment configs (`oc get dc`) – mirekphd May 30 '20 at 11:01
  • 1
    Note also that `--all-namespaces` requires cluster admin rights, so if your getting RBAC errors, just drop it and proceed one namespace at a time. – mirekphd May 30 '20 at 11:02