Listing all resources in a namespace

2020-05-14 13:53发布

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).

Any idea how to show all resources before for example deleting that namespace?

8条回答
爷、活的狠高调
2楼-- · 2020-05-14 14:15

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>
查看更多
▲ chillily
3楼-- · 2020-05-14 14:15

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>/+"
查看更多
混吃等死
4楼-- · 2020-05-14 14:17

the easy way for me to retrieve all the contents of the namespace was kubectl get all -n

查看更多
Root(大扎)
5楼-- · 2020-05-14 14:19

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> }
查看更多
Melony?
6楼-- · 2020-05-14 14:24

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
    kubectl -n ${1} get --ignore-not-found ${i}
  done
}

Usage: kubectlgetall <namespace>

Example: get all resources from the kafka namespace:

kubectlgetall kafka

查看更多
放荡不羁爱自由
7楼-- · 2020-05-14 14:30
kubectl get all --all-namespaces

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

At least, it gets:

  • pod
  • service
  • daemonset
  • deployment
  • replicaset
  • statefulset
  • job

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

Running v1.13

查看更多
登录 后发表回答