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

11/20/2018

I'd like to see the 'config' details as shown by the command of:

kubectl config view

However this shows the entire config details of all contexts, how can I filter it (or perhaps there is another command), to view the config details of the CURRENT context?

-- Chris Stryczynski
kubectl
kubernetes

5 Answers

11/20/2018

kubectl config view --minify displays only the current context

-- Jordan Liggitt
Source: StackOverflow

11/20/2018

The cloud-native way to do this is to use the JSON output of the command, then filter it with 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"
    }
  }
}

This answer helped me figure out some of the jq bits.

-- andrewdotn
Source: StackOverflow

11/21/2018

The bash/kubectl with a little bit of jq, for any context equivalent:

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
-- Rico
Source: StackOverflow

11/21/2018

You can use the command kubectl config view --minify to get current context only.

It is handy to use --help to get the options what you could have for kubectl operations.

kubectl config view --help
-- marvelTracker
Source: StackOverflow

10/21/2019

use the below command to get the full config including certificates

kubectl config view --minify --flatten
-- P Ekambaram
Source: StackOverflow