unable to list pods in any state except completed

4/1/2020

Problem: I want to list all pods except those NOT in Completed state.

field selector still outputs pods in Completed phase when Selected not Completed in kubectl command

Proposed Solution: observe that am also getting pods in Completed phase.

kubectl get pods --all-namespaces --field-selector=spec.nodeName=node1,status.phase!=Completed --no-headers
test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e nightly-2020-04-01-13-00-tokyo-demo-route-gt-4245049053 2/2 Running 0 13m
test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e nightly-2020-04-01-13-00-willows-405-a2a-1380625152 0/2 Completed 0 3h31m
test-2d7dbabf-f8cc-4c0b-af3b-80db52d2257e nightly-2020-04-01-13-00-willows-405-a2a-1464250510 0/2 Completed 0 7h33m
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.5", GitCommit:"2166946f41b36dea2c4626f90a77706f426cdea2", GitTreeState:"clean", BuildDate:"2019-03-25T15:19:22Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}

but when i do for Running it works and lists only Completed phase pods

kubectl get pods --all-namespaces --field-selector=spec.nodeName=node1,status.phase!=Running --no-headers
-- AhmFM
kubectl
kubernetes

2 Answers

4/1/2020

Try this:

kubectl get pod -o=json | jq '.items[]|select(any( .status.containerStatuses[]; .state.terminated.reason!="Completed"))|.metadata.name'

This will list down all the pods where the state of the pod is anything other than completed.

For testing change != with \== to list Completed pod.

Specifically, if you are looking for cronjobs containers only that were in not completed state using kubectl native jsonpath use this:

kubectl get pod -o jsonpath='{.items[?(@.status.containerStatuses[*].state.terminated.reason!="Completed")].metadata.name}'

I hope it helps.

-- Vaibhav Jain
Source: StackOverflow

4/1/2020

Completed is not a valid Pod Phase, use Succeeded instead.

Pending, Running, Succeeded, Failed and Unknown are the valid values for Pod Phase.

To get the non-completed pods,

kubectl get pods --all-namespaces --field-selector=spec.nodeName=node1,status.phase!=Succeeded --no-headers
-- franklinsijo
Source: StackOverflow