如何在 kubectl 中查看当前上下文的配置详细信息?

How can I view the config details of the current context in kubectl?

我想查看 'config' 详细信息,如以下命令所示:

kubectl config view

然而,这显示了所有上下文的完整配置详细信息,我如何过滤它(或者可能有另一个命令),以查看当前上下文的配置详细信息?

kubectl config view --minify 仅显示当前上下文

执行此操作的云原生方法是使用命令的 JSON 输出,然后使用 jq:

对其进行过滤
kubectl config view -o json | jq '. as $o
    | ."current-context" as $current_context_name
    | $o.contexts[] | select(.name == $current_context_name) as $context
    | $o.clusters[] | select(.name == $context.context.cluster) as $cluster
    | $o.users[] | select(.name == $context.context.user) as $user
    | {"current-context-name": $current_context_name, context: $context, cluster: $cluster, user: $user}'

{
  "current-context-name": "docker-for-desktop",
  "context": {
    "name": "docker-for-desktop",
    "context": {
      "cluster": "docker-for-desktop-cluster",
      "user": "docker-for-desktop"
    }
  },
  "cluster": {
    "name": "docker-for-desktop-cluster",
    "cluster": {
      "server": "https://localhost:6443",
      "insecure-skip-tls-verify": true
    }
  },
  "user": {
    "name": "docker-for-desktop",
    "user": {
      "client-certificate-data": "REDACTED",
      "client-key-data": "REDACTED"
    }
  }
}

帮助我找出了一些 jq 位。

带有一点 jq 的 bash/kubectl,对于任何等效的上下文:

exec >/tmp/output &&
CONTEXT_NAME=kubernetes-admin@kubernetes \
CONTEXT_CLUSTER=$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${CONTEXT_NAME}\")].context.cluster}") \
CONTEXT_USER=$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${CONTEXT_NAME}\")].context.user}") && \
echo "[" && \
kubectl config view -o=json | jq  -j --arg CONTEXT_NAME "$CONTEXT_NAME" '.contexts[] | select(.name==$CONTEXT_NAME)' && \
echo "," && \
kubectl config view -o=json | jq  -j --arg CONTEXT_CLUSTER "$CONTEXT_CLUSTER" '.clusters[] | select(.name==$CONTEXT_CLUSTER)' && \
echo "," && \
kubectl config view -o=json | jq  -j --arg CONTEXT_USER "$CONTEXT_USER" '.users[] | select(.name==$CONTEXT_USER)' && \
echo -e "\n]\n" && \
exec >/dev/tty && \
cat /tmp/output | jq && \
rm -rf /tmp/output

您可以使用命令 kubectl config view --minify 仅获取当前上下文。

使用 --help 可以很方便地获取 kubectl 操作的选项。

kubectl config view --help

使用以下命令获取包括证书在内的完整配置

kubectl config view --minify --flatten