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:36

If you are using kubectl krew plug-in, I will suggest using get-all. It can get almost 90% resources. included configmap, secret, endpoints, istio, etc

查看更多
趁早两清
3楼-- · 2020-05-14 14:42

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"
查看更多
登录 后发表回答