Disabling context-switching that happens with kubectl config view --flatten

8/12/2019

I have a command-line utility to start/stop VMs that I am extending to managing Kubernetes clusters. It can save the kubeconfig currently, as well as merge a new configuration into a user's existing ~/.kube/config - but the command I am using (from How to merge kubectl config file with ~/.kube/config?) also switches the active context:

KUBECONFIG=<temporary_path_to_newly_created_config>:~/.kube/config kubectl config view --flatten`

Is there a way to prevent the active context from switching unless I want it to? Am I using the wrong command with kubectl config view --flatten and is there a merging option that makes no changes to the active context?

-- kaipmdh
kubectl
kubernetes

2 Answers

8/20/2019

Why kubectl behaves as it does.

  1. Actually there is no systemwide current-context value. You might have several current-context values in the kubeconfig files specified in the $KUBECONFIG variable or another files specified with the parameter --kubeconfig=.
  2. There is default kubeconfig file ~/.kube/config that kubectl uses when the $KUBECONFIG variable is empty and parameter --kubeconfig= is not provided.
  3. Every time you start kubectl, it reads and merges values from the kubeconfig files to build its configuration for the current run.
  4. While merging kubeconfig files, among conflicting values the one from the first file wins.
  5. When you direct kubectl to use-context with the command KUBECONFIG=config1:config2 kubectl config use-context my_context, it saves context into the file listed first in the $KUBECONFIG variable (config1).

Hence it’s not entirely accurate to say that kubectl "switches active context". It saves context to where you point and reads [possibly another] context from where you point. The current-context for the current kubectl run is set based on its command-line parameters, contents of the kubeconfig files and the order they are listed in the $KUBECONFIG variable.

Now back to the question.

Q:

Is there a way to prevent the active context from switching unless I want it to? and is there a merging option that makes no changes to the active context?

A:

There is no way to prevent the "active context" from switching because the current-context stored in the default and other kubeconfig files is assigned as "active" for the current run each time kubectl is launched. Also, kubectl shouldn't be considered as a plain text parsing & merging tool.

Speaking about default configuration in the particular case you've described, to re-use the previously used or preferred context or the one you believe "is current", you should list the kubeconfig file with that context the first in the $KUBECONFIG variable:

KUBECONFIG=~/.kube/config:new_config_to_merge kubectl config view --flatten > ...

NOTE. If none of the kubeconfig files contain not empty current-context value, the error will appear: error: current-context is not set.

Please see Organizing Cluster Access Using kubeconfig Files

-- mebius99
Source: StackOverflow

8/12/2019

1. Use kubectl config use-context my-cluster-name

or

2. Install kubectx

The second option will make your life so much easier, of course for both solution you have to set your config files in KUBECONFIG

-- FL3SH
Source: StackOverflow