Title: How to list the kubernetes pods based on any particular exit code, for example i need to list all the pods which has exitCode value 255
Tags: kubernetes;go-templates
Question: How to list the kubenetes pods based on any particular exitCode value. For example i need to list all the pods which has exitCode value = 255.
I have tried below command and it will give all pods along with all exitcodes.
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{..exitCode}{'\n'}{end}"
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{..exitCode}{'\n'}{end}"


Here is another answer: Below is the command to get the list of pod based on particular error code:
kubectl get pods  $(for i in  0 255; do kubectl get pods -o=jsonpath={.items[?(@..exitCode==$i)].metadata.name} ; echo ;done)
And we can use below command to delete pods based on particular error codes:
kubectl delete pods $(for i in  0 255; do kubectl get pods -o=jsonpath={.items[?(@..exitCode==$i)].metadata.name} ; echo ;done)
where value in for loop i.e 0 255 etc is the error code, you can give any error code here and then run this command.

Here is another answer: if I understand you correctly you may want to check out the Field Selectors.
Field selectors let you select Kubernetes resources based on the value
of one or more resource fields. Here are some example field selector
queries:
metadata.name=my-service
metadata.namespace!=default
status.phase=Pending
This kubectl command selects all Pods for which
the value of the status.phase field is Running:
kubectl get pods --field-selector status.phase=Running
Here is some more documentation regarding this.
Selector (field query) to filter on, supports '=',
'==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The
server only supports a limited number of field queries per type.
Please let me know if that helped.

Here is another answer: kubectl get pods -o=jsonpath=&quot;{range .items[*]}{.metadata.name}{'\t'}{..exitCode}{'\n'}{end}&quot; | grep 255

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:09/11/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<kubernetes><go-templates>', 'Title': 'How to list the kubernetes pods based on any particular exit code, for example i need to list all the pods which has exitCode value 255', 'ViewCount': 593, 'id': 57895880, 'user_AboutMe': None, 'user_AccountId': 12309682.0, 'user_DisplayName': 'Ankit', 'user_Reputation': 11.0, 'user_Views': 11.0, 'user_id': 8981575.0}}