Listing all resources in a namespace

前端 未结 9 1277
半阙折子戏
半阙折子戏 2020-12-07 20:24

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.

相关标签:
9条回答
  • 2020-12-07 20:25

    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.

    0 讨论(0)
  • 2020-12-07 20:30

    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>/+"
    
    0 讨论(0)
  • 2020-12-07 20:33

    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"
    
    0 讨论(0)
  • 2020-12-07 20:34

    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

    0 讨论(0)
  • 2020-12-07 20:34

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

    0 讨论(0)
  • 2020-12-07 20:35

    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

    0 讨论(0)
提交回复
热议问题