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
}
Rcorre 的答案是正确的,但是对于 N 个资源,它向集群发出 N 个请求(因此对于许多资源,这种方法非常慢)。而且,找不到资源(没有实例)使用 kubectl get的速度非常慢。
有一个更好的方法来请求多个资源:
kubectl get pods,svc,secrets
而不是
kubectl get pods
kubectl get svc
kubectl get secrets
所以答案是:
#!/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
或者
#!/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"
#!/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