164

I would like to see all resources in a namespace.

Doing kubectl get all will, despite of the name, not list things like services and ingresses.

If I know the the type I can explicitly ask for that particular type, but it seems there is also no command for listing all possible types. (Especially kubectl get does for example not list custom types).

How to show all resources before for example deleting that namespace?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
michas
  • 25,361
  • 15
  • 76
  • 121

13 Answers13

144

Based on this comment , the supported way to list all resources is to iterate through all the api versions listed by kubectl api-resources:

kubectl api-resources enumerates the resource types available in your cluster.

this means you can combine it with kubectl get to actually list every instance of every resource type in a namespace:

kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n <namespace>
Community
  • 1
  • 1
rcorre
  • 6,477
  • 3
  • 28
  • 33
  • 2
    It would be nice to have an updated answer, which could exclude forbidden GVK . – suryakrupa Mar 09 '19 at 20:41
  • 14
    just added `alias k8s-show-ns=" kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n"` as an alias to my rc file. thanks – Josh Beauregard Dec 23 '19 at 14:45
  • 3
    My slightly different take on it: `function kgetall { kubectl api-resources --verbs=list --namespaced -o name | xargs -n1 kubectl get --show-kind --ignore-not-found "$@" }`. Adding `complete -F __start_kubectl kgetall` will get you completion on this as well. – Ben Moss Jun 03 '20 at 16:44
  • 3
    what worked for me ```kubectl api-resources --verbs=list --namespaced -o name \ | xargs -n 1 kubectl get --show-kind --ignore-not-found -nl -n ``` – Alex Punnen Feb 04 '21 at 07:06
  • 2
    It can be useful to find out what resource name was that is listed in the namespace use the `-t` option in xargs. `xargs -t -n 1 ..` This is xargs debugging and will show the command that it is executing. Which api resource it was trying to list. – nelaaro Feb 10 '21 at 17:56
67

This may not get all resources but it may be what someone is looking for

kubectl get all,cm,secret,ing -A

This seems to get most of the resources, prefixed with the type.

At least, it gets:

  • pod
  • service
  • daemonset
  • deployment
  • replicaset
  • statefulset
  • job
  • configmap
  • secret
  • ingress

This doesn't get custom resources but does get services.

Else this does something similar:

for i in `kubectl api-resources | awk '{print $1}'`; do kubectl get $i; done

Running v1.13

Community
  • 1
  • 1
Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
36

I ended up needing this same functionality due to failed Helm deployments that left remnants in a specific namespace. Here's a function you can put in your bash profile:

function kubectlgetall {
  for i in $(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq); do
    echo "Resource:" $i
    
    if [ -z "$1" ]
    then
        kubectl get --ignore-not-found ${i}
    else
        kubectl -n ${1} get --ignore-not-found ${i}
    fi
  done
}

Usage: kubectlgetall <namespace>

Example: get all resources from the kafka namespace:

kubectlgetall kafka

Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
Jeremy Glover
  • 608
  • 1
  • 9
  • 10
  • 1
    Thank you for this. It would be nice if this functionality were added to kubctl rather than needing to do this kind of workaround, but this definitely solves my problems. – Vorticity Feb 05 '20 at 23:49
  • It doesn't need to be added "to kubectl" see https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/ – OneCricketeer May 10 '23 at 20:37
11

Complete solution

kubectl -n <NAMESPACE> get $(kubectl api-resources --namespaced=true --no-headers -o name | egrep -v 'events|nodes' | paste -s -d, - ) --no-headers
Michael A.
  • 1,071
  • 12
  • 21
9

Answer of rcorre is correct but for N resources it make N requests to cluster (so for a lot of resources this approach is very slow). Moreover, not found resources (that have not instances) are very slow for getting with kubectl get.

There is a better way to make a request for multiple resources:

kubectl get pods,svc,secrets

instead of

kubectl get pods
kubectl get svc
kubectl get secrets

So the answer is:

#!/usr/bin/env bash

# get all names and concatenate them with comma
NAMES="$(kubectl api-resources \
                 --namespaced \
                 --verbs list \
                 -o name \
           | tr '\n' ,)"

# ${NAMES:0:-1} -- because of `tr` command added trailing comma
# --show-kind is optional
kubectl get "${NAMES:0:-1}" --show-kind

or

#!/usr/bin/env bash

# get all names
NAMES=( $(kubectl api-resources \
                  --namespaced \
                  --verbs list \
                  -o name) )

# Now join names into single string delimited with comma
# Note *, not @
IFS=,
NAMES="${NAMES[*]}"
unset IFS

# --show-kind is enabled implicitly
kubectl get "$NAMES"
Ivan Vasilyev
  • 136
  • 2
  • 6
  • 2
    In fact, the same number of API requests will be made in both cases, you can check it if add `--v 6` parameter to `kubectl` command: ```kubectl get pods,svc,secrets --v 6``` When you call `kubectl` commands separately it causes config loading every time. In the combined command, the configuration will be requested once. Config loading takes about 10-12ms. – Max Koshel Nov 09 '19 at 18:08
  • 2
    You are right: the same number of API requests. The difference is that in case of `pods,svc,secrets` there are three requests within one HTTP/2 connection. But when you make call for each resource separately, you establish new HTTP/2 connection for each of them. – Ivan Vasilyev Nov 11 '19 at 06:03
  • I don't know why yet, but some resources like `issuers.certmanager.k8s.io` or `leases.coordination.k8s.io` requires more time to get than another (4 seconds vs 200ms for ordinar like `pods`). Whey I get them separately, time add up. When I get then at-once (`pods,svc,...`), time of call stay the same. – Ivan Vasilyev Nov 11 '19 at 06:09
9

If you are using kubectl krew plug-in, I will suggest using get-all.

It can get almost 90% of resources. Including configmap, secret, endpoints, istio, etc...

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Andy Wong
  • 3,676
  • 1
  • 21
  • 18
5

A Powershell implementation of rcorre's answer would look like

kubectl api-resources --verbs=list --namespaced -o name | `
%{ kubectl get $_ --show-kind --ignore-not-found -l <label>=<value> -n <namespace> }
delve
  • 51
  • 1
  • 4
2

All kubernetes objects are stored in etcd.

All objects are stored in ETCD v3 the following way:

/registry/<object_type>/<namespace>/<name>

I suggest just to take the list of all resources of some namespace from etcd v3 directly:

ETCDCTL_API=3 etcdctl --endpoints=<etcd_ip>:2379 get / --prefix --keys-only | grep -E "^/\w+/\w+/<namespace>/+"
nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • This is useful if you have a self-managed Kubernetes cluster - it won't be possible with GKE, EKS and other managed services. – RichVel Jun 12 '21 at 07:35
2

It's not a 100% solution, but for me works the following

kgetall='kubectl get namespace,replicaset,secret,nodes,job,daemonset,statefulset,ingress,configmap,pv,pvc,service,deployment,pod --all-namespaces'

and just call

kgetall

But obviously I expected that behavior from

kubectl get all --all-namespaces

in the first place.

Michael
  • 393
  • 2
  • 4
  • 20
2

Just covering some basics:

  • Resources can be fetched using the kubectl get command.
  • Resources can be filtered by namespace using the -n [NAMESPACE] or --namespace [NAMESPACE] flags.
    • I will use the -A (same as --all-namespaces) flag in the following examples for brevity.

Some of the other answers show how to list available resource types:

kubectl api-resources --verbs=list -o name

With this in mind, we can get all resources, leveraging xargs and sed:

kubectl get "all,$(kubectl api-resources --verbs=list -o name | xargs | sed 's/ /,/g')" -A

An alternative to xargs is to use tr:

kubectl get "$(kubectl api-resources --verbs=list -o name | tr '\n' ',')all" -A
Logan
  • 607
  • 8
  • 7
1

I think this could be se simplest way to print out all the resource name in a kubernetes cluster:

#!/bin/bash

for resource in [$(kubectl api-resources -o name | tr "\n" " ")]
do 
  kubectl get $resource --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
done

to specify the namespace you just need to modify the line in the loop setting --namespace=<your-namespace> and remove --all-namespaces

1

Here's a PowerShell version of what other's have said

function getall {
    # https://stackoverflow.com/questions/47691479/listing-all-resources-in-a-namespace
    [CmdletBinding()]
    param(
        $namespace
    )
    $types=kubectl api-resources --verbs=list --namespaced -o name
    kubectl get $($types -join ",") -n $namespace --show-kind
}
TeamDman
  • 649
  • 6
  • 26
0

Following this solution, in fish shell it looks like below.

Add the following function to your ~/.config/fish/config.fish

function kall
  kubectl -n $argv get (string join ',' (kubectl api-resources --namespaced --verbs list -o name))
end
Thorsten
  • 356
  • 4
  • 8