Listing all resources in a namespace

前端 未结 9 1309
半阙折子戏
半阙折子戏 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: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"
    

提交回复
热议问题