Reputation: 34071
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?
Upvotes: 18
Views: 24491
Reputation: 19
I used @andrewdotn's answer above and tweaked it a bit to use yq
run directly against $KUBECONFIG
yq '. as $o
| ."current-context" as $current_context_name
| $o.contexts[] | select(.name == $current_context_name) as $context
| ($current_context_name + ":" + $context.context.namespace)' $KUBECONFIG
With some post processing this is a nice little context string for my PS1
Upvotes: 0
Reputation: 849
If you just want the name of the current context...
Powershell
kubectl config view | find --% "current"
Bash
kubectl config view | grep "current"
Windows Command Prompt
kubectl config view | find "current"
Upvotes: 0
Reputation: 17689
use the below command to get the full config including certificates
kubectl config view --minify --flatten
Upvotes: 14
Reputation: 4969
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
Upvotes: 1
Reputation: 61689
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
Upvotes: 2
Reputation: 34873
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.
Upvotes: 5
Reputation: 18161
kubectl config view --minify
displays only the current context
Upvotes: 38