Issues Merging Multiple Kubeconfig Files and Context Switching

I’ve been trying to configure access to multiple Kubernetes clusters by merging multiple kubeconfig files, as explained in the (site.kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/). While the individual files seem to work fine, I’m facing issues when merging them and switching between contexts.

Here’s what I did:

  1. Created two kubeconfig files (config-demo and config-demo-2) with the following contexts:
  • config-demo contains contexts for dev-frontend, dev-storage, and exp-test.
  • config-demo-2 contains a new context, dev-ramp-up.

Merged the two files by setting the KUBECONFIG environment variable:

export KUBECONFIG="config-demo:config-demo-2"

Ran kubectl config view to verify the merged configuration, and the contexts appeared as expected.

Here’s the problem:

  • When I switch to the dev-ramp-up context using:
kubectl config use-context dev-ramp-up

and try running any command, I get errors like Error from server (Unauthorized): the server does not allow this request for this user on the current namespace.

  • On further investigation using:
kubectl auth whoami

it shows incorrect or missing user attributes for the context, even though the user credentials are properly defined in the kubeconfig file.

Questions:

  • Are there specific merging rules that could cause user credentials to be overwritten or ignored when combining multiple kubeconfig files?
  • How can I ensure that the correct user attributes are retained for all contexts when switching?
  • Is there a way to debug or validate the merged kubeconfig file to pinpoint where the issue is occurring?

If anyone has encountered a similar problem or has insights into resolving this, your help would be greatly appreciated!

The better and easy way to manage is this through GitHub - ahmetb/kubectx: Faster way to switch between clusters and namespaces in kubectl
Have you tried this already?