Kubernetes kubectl get secrets by type?

12/6/2018

I want to run kubectl and get all the secrets of type = X. Is this possible?

I.e if I want to get all secrets where type=tls

something like kubectl get secrets --type=tls?

-- nate
kubectl
kubernetes

3 Answers

10/24/2019

How about filed-selector:

$ kubectl get secrets --field-selector type=kubernetes.io/tls
-- Sean Z
Source: StackOverflow

12/6/2018

You can do it jsonpath. Something like this:

$ kubectl get secret -o=jsonpath='{range .items[*]}{.metadata.name} {.type}{"\n"}{end}' | grep -i tls

For example, to get all the type Opaque secrets:

$ kubectl get secret -o=jsonpath='{range .items[*]}{.metadata.name} {.type}{"\n"}{end}' | grep Opaque
dummy-secret Opaque
mysecretdelete Opaque

Update:

Now you can do this with the --field-selector option in kubectl:

$ kubectl get secrets --field-selector type=kubernetes.io/tls
$ kubectl get secret --field-selector type=kubernetes.io/service-account-token
-- Rico
Source: StackOverflow

3/14/2019

The accepted answer certainly works, but I was interested in finding a grep-less solution. Here's my contribution.

$ kubectl get secret -o=jsonpath='{.items[?(@.type=="Opaque")].metadata.name}'
dummy-secret mysecretdelete
-- Philip C
Source: StackOverflow