Search code examples
kuberneteskubectl

Kubernetes kubectl get secrets by type?


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?


Solution

  • 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