Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

microk8s.kubectl does not respect $KUBECONFIG var #259

Closed
nicja opened this issue Dec 21, 2018 · 6 comments
Closed

microk8s.kubectl does not respect $KUBECONFIG var #259

nicja opened this issue Dec 21, 2018 · 6 comments
Labels

Comments

@nicja
Copy link

nicja commented Dec 21, 2018

I know from #180 that currently we cannot edit the default config file used by microk8s.kubectl, however the output of microk8s.kubectl config -h says that:

2. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimitting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the
last file in the list.

This is not happening however. Even when this env var is set, microk8s.kubectl is still referring to /snap/microk8s/current/client.config

It would be great if we could specify this in an ENV var as I work with a lot of existing CI scripts where it would not be feasible to add --kubeconfig /xxx/xxx/config to each. It would be great to be able to set this in a config var in lieu of being able to edit the default config file

@ktsakalozos
Copy link
Member

ktsakalozos commented Dec 23, 2018

Hi @nicja

Thank you for opening this issue.

The problem we are facing is because the kubectl wrapper has the path to kubeconfig file hard coded https://github.com/ubuntu/microk8s/blob/master/microk8s-resources/wrappers/microk8s-kubectl.wrapper#L9 . Instead we should have an arguments file under https://github.com/ubuntu/microk8s/tree/master/microk8s-resources/default-args and have the kubectl wrapper append any arguments to the command. This is aligned with what we do for the rest of the MicroK8s daemons and would allow users to define their own location of the kubeconfig file. If no location is set kubectl should use the $KUBECONFIG environment variable.

@losinggeneration
Copy link

losinggeneration commented Jan 2, 2019

Assuming the wrapper would have access to $KUBECONFIG, the following might work for kubectl.wrapper:

${SNAP}/kubectl --kubeconfig="${KUBECONFIG:-${SNAP}/client.config}" "$@"

--- edited to add quotes to handle paths with spaces in $KUBECONFIG

@marcbrandner
Copy link

marcbrandner commented Jan 11, 2019

Hi all,

@losinggeneration
I wasn't able to get your solution to work in my environment. I got messages like this when executing kubectl:
/snap/bin/microk8s.kubectl: line 5: SNAP: unbound variable
I think I am just using it incorrectly.

But I was able to use a custom kubeconfig the following way (beware: it's a very dirty workaround with room for improvement, but I thought I'd share it anyway):

# Using the snap alias as written in the docs
sudo snap alias microk8s.kubectl kubectl
# Writing the read-only config into a writable file
kubectl config view > ~/.kube/config
# Creating a wrapper shell script (kubectlx) that runs kubectl with the kubeconfig option
echo -e '#!/bin/bash \nkubectl --kubeconfig=~/.kube/config \"$@\"' > /snap/bin/kubectlx
# Making the wrapper shell script executable
chmod +x /snap/bin/kubectlx
# Setting a shell alias for kubectlx
echo "alias kubectl=kubectlx" >> ~/.bash_profile
source ~/.bash_profile

Now I am able to write to the config.

kubectl config set-context mycontext --cluster=microk8s-cluster --user=admin --namespace=my-namespace
returns
Context "mycontext" created.

kubectl config get-contexts
gives me

CURRENT   NAME        CLUSTER            AUTHINFO   NAMESPACE
          microk8s    microk8s-cluster   admin
*         mycontext   microk8s-cluster   admin      my-namespace

Regards,
Marc

--- Update: Corrected a syntax mistake in a shell command.

@reyou
Copy link

reyou commented Feb 5, 2019

Any update on this?
I believe the issue I opened relates to this one;

#304

Or is there any quick workaround? @marcbrandner suggested answer did not work for me.
I am experiencing same behavior.

@nicja
Copy link
Author

nicja commented May 21, 2019

It is now possible to work around this in the latest snap version (1.14.1) by editing /var/snap/microk8s/current/args/kubectl and changing --kubeconfig to point to your own preferred config file, or in my case:

--kubeconfig=${KUBECONFIG}

@stale
Copy link

stale bot commented Apr 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Apr 15, 2020
@stale stale bot closed this as completed Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants