如何使用 cli 获取 kube-contexts 列表

How to get a list of kube-contexts using cli

我正在尝试获取 kube-contexts 列表(并过滤 gke 集群),with some tools 最终结果是:

kubectl config get-contexts | tr -s ' ' | cut -d ' ' -f 2 | grep gke

输出:

gke_dev-redacted
gke_prod-redacted

有没有更简单的方法(这不取决于命令输出不使用制表符,而是使用多个空格这一事实)。该命令不支持 yaml 或 json 输出:

--output yaml is not available in kubectl config get-contexts; resetting to default output format

您可以提供 --output 标志以仅显示上下文的名称,例如:

$ kubectl config get-contexts --output=name
minikube

GKE 上下文的 grep 很容易:

$ kubectl config get-contexts --output=name | grep "gke_*"