Current context is not set Kubectl

Viewed 1315

Get the following output with Config view. Notice no "Current Context"

root@Bootstrap [ /etc ]# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.10.50:6443
  name: tanzu-m01
contexts:
- context:
    cluster: tanzu-m01
    user: tanzu-m01-admin
  name: tanzu-m01-admin@tanzu-m01
current-context: ""
kind: Config
preferences: {}
users:
- name: tanzu-m01-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
- name: tkgm-mgmt01-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

Using kubectl set-context >> shows context modified

root@Bootstrap [ /etc ]# kubectl config set-context tanzu-m01-admin@tanzu-m01
Context "tanzu-m01-admin@tanzu-m01" modified.

However on "kubectl config current-context" shows current-context not set

root@Bootstrap [ /etc ]# kubectl config current-context
error: current-context is not set

What am i missing ?

2 Answers

The correct command is:

kubectl config use-context ${CONTEXT}

You can confirm this with:

kubectl config --help

set-context     Set a context entry in kubeconfig
use-context     Set the current-context in a kubeconfig file

kubectl config set-context set properties of the specified context:

kubectl config set-context ${CONTEXT} \
--cluster=${CLUSTER} \
--user=user_${USER} \
--namespace=${NAMESPACE}

Below will give you the current contexts

kubectl config set current-context

If the current context is not seen then search for the context present

kubectl config get-contexts

Now you will have the list and now you set the context

kubectl config set current-context <context-name>
Related