danielfoehrKn / kubeswitch

The kubectx for operators.
https://danielfoehrkn.medium.com/the-case-of-kubeswitch-aff4b6a04ae7
Apache License 2.0
832 stars 80 forks source link

Unable to fetch namespaces #107

Open jeanfrancoisgratton opened 9 months ago

jeanfrancoisgratton commented 9 months ago

First off, I must be doing something wrong....

In my .zshrc, I export KUBECONFIG dynamically, this way: export KUBECONFIG="$(printf '%s\n' ~/.kube/configs/*.k8sconfig 2>/dev/null | awk '{printf "%s:", $0}' | sed 's/:$//')" 2>/dev/null

This allows my variable to be up-to-date at each start of my shell, regardless of the contents in ~/.kube/configs/ . All of my kube config files have the .k8sconfig extension.

My switch-config.yaml file is this: kind: SwitchConfig version: v1alpha1 kubeconfigStores:

The problem I have right now is that I can select a context, but once done, if I come back with ks ns it breaks with the following error: WARN[0000] failed to retrieve current namespaces: unable to create rest config: invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable

Why is that, and how may I correct my setup ? Obviously I am missing something.

I currently have 2 config files in .kube/configs, but it will soon grow. I've tried exporting KUBECONFIG= one of those files. What happens then is that when I run switcher, it lists the contexts from KUBECONFIG, and then the contexts from the 2 files in .kube/configs/ .. This means one file is listed twice.

Moreover, regardless of which context I choose, when it exits and then I try switcher ns, regardless of which config file switch to, switcher ns will always lists the namespaces from the first selected file. As if my selection is disregarded past the first occurence.

jeanfrancoisgratton commented 9 months ago

Trying to find some work-arounds, I've stumbled on this:

I have 2 config files: .kube/configs/cfg1.yaml, .kube/configs/cfg2.yaml. I've then ran:

kind: SwitchConfig version: v1alpha1 kubeconfigStores:

Ok 2 things:

danielfoehrKn commented 6 months ago

Do you use the switcher binary only - or also the switch script - which is required to set the KUBECONFIG environment variable? To verify, check that after running switch, that the environment variable is set like so:

KUBECONFIG=/Users/<my_user>/.kube/.switch_tmp/config.3093589165.tmp

a further switcher ns command will still fetch cfg1's namespaces

Another clue that the new KUBECONFIG env variable is not set, as the namespace command simply reads the currently set KUBECONFIG env variable to determine the kubeconfig to use.

My 2 cfg files are duplicated (so 4 files show, 2 with the .kube/ prefix, 2 without.

Probably the contexts without the switch prefix are the contexts taken from your current Kubeconfig file set via the KUBECONFIG environment variable (it also checks this file to be compatible with kubectx, but ignores it when it is a kubeswitch-created temporary kubeconfig to not show duplicate entries)