microk8s.kubectl does not respect `$KUBECONFIG` var

Created on 21 Dec 2018  路  6Comments  路  Source: ubuntu/microk8s

I know from https://github.com/ubuntu/microk8s/issues/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

inactive

Most helpful comment

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.

All 6 comments

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.

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

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.

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

https://github.com/ubuntu/microk8s/issues/304

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

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}

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jareks picture jareks  路  3Comments

singram picture singram  路  4Comments

toxsick picture toxsick  路  4Comments

NumesSanguis picture NumesSanguis  路  3Comments

carmine picture carmine  路  4Comments