diff --git a/README.md b/README.md index 0c5fe527aa4e..8bf464fe5b95 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Quick Start ```bash sudo k3s server & -# Kubeconfig is written to /root/.kube/k3s.yaml +# Kubeconfig is written to /etc/rancher/k3s/k3s.yaml sudo k3s kubectl get node # On a different node run the below. NODE_TOKEN comes from /var/lib/rancher/k3s/server/node-token @@ -66,7 +66,7 @@ INFO[2019-01-22T15:16:20.273441984-07:00] Listening on :6443 INFO[2019-01-22T15:16:20.278383446-07:00] Writing manifest: /var/lib/rancher/k3s/server/manifests/coredns.yaml INFO[2019-01-22T15:16:20.474454524-07:00] Node token is available at /var/lib/rancher/k3s/server/node-token INFO[2019-01-22T15:16:20.474471391-07:00] To join node to cluster: k3s agent -s https://10.20.0.3:6443 -t ${NODE_TOKEN} -INFO[2019-01-22T15:16:20.541027133-07:00] Wrote kubeconfig /root/.kube/k3s.yaml +INFO[2019-01-22T15:16:20.541027133-07:00] Wrote kubeconfig /etc/rancher/k3s/k3s.yaml INFO[2019-01-22T15:16:20.541049100-07:00] Run: k3s kubectl ``` diff --git a/install.sh b/install.sh index 6ac91bde2d2b..a6449e884657 100755 --- a/install.sh +++ b/install.sh @@ -79,6 +79,7 @@ umount `cat /proc/self/mounts | awk '{print $2}' | grep '^/run/k3s'` umount `cat /proc/self/mounts | awk '{print $2}' | grep '^/var/lib/rancher/k3s'` rm -rf /var/lib/rancher/k3s +rm -rf /etc/rancher/k3s rm -f /usr/local/bin/k3s-uninstall.sh EOF diff --git a/pkg/datadir/datadir.go b/pkg/datadir/datadir.go index a5bc8b2903bd..e174a7a32510 100644 --- a/pkg/datadir/datadir.go +++ b/pkg/datadir/datadir.go @@ -10,6 +10,8 @@ import ( const ( DefaultDataDir = "/var/lib/rancher/k3s" DefaultHomeDataDir = "${HOME}/.rancher/k3s" + HomeConfig = "${HOME}/.kube/k3s.yaml" + GlobalConfig = "/etc/rancher/k3s/k3s.yaml" ) func Resolve(dataDir string) (string, error) { diff --git a/pkg/kubectl/main.go b/pkg/kubectl/main.go index 239bb5ca795e..2171c2db2e99 100644 --- a/pkg/kubectl/main.go +++ b/pkg/kubectl/main.go @@ -17,7 +17,7 @@ import ( func Main() { kubenv := os.Getenv("KUBECONFIG") if kubenv == "" { - config, err := server.HomeKubeConfig() + config, err := server.HomeKubeConfig(false) if _, serr := os.Stat(config); err == nil && serr == nil { os.Setenv("KUBECONFIG", config) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 907eec911f0c..b6370efc51df 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -16,6 +16,7 @@ import ( "github.com/pkg/errors" "github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/daemons/control" + "github.com/rancher/k3s/pkg/datadir" "github.com/rancher/k3s/pkg/deploy" "github.com/rancher/k3s/pkg/helm" "github.com/rancher/k3s/pkg/servicelb" @@ -147,8 +148,19 @@ func startNorman(ctx context.Context, config *Config) (string, error) { } } -func HomeKubeConfig() (string, error) { - return resolvehome.Resolve("${HOME}/.kube/k3s.yaml") +func HomeKubeConfig(write bool) (string, error) { + if write { + if os.Getuid() == 0 { + return datadir.GlobalConfig, nil + } + return resolvehome.Resolve(datadir.HomeConfig) + } + + if _, err := os.Stat(datadir.GlobalConfig); err == nil { + return datadir.GlobalConfig, nil + } + + return resolvehome.Resolve(datadir.HomeConfig) } func printTokens(certs, advertiseIP string, tlsConfig *dynamiclistener.UserConfig, config *config.Control) { @@ -177,7 +189,7 @@ func printTokens(certs, advertiseIP string, tlsConfig *dynamiclistener.UserConfi func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config *config.Control) { clientToken := FormatToken(config.Runtime.ClientToken, certs) url := fmt.Sprintf("https://localhost:%d", tlsConfig.HTTPSPort) - kubeConfig, err := HomeKubeConfig() + kubeConfig, err := HomeKubeConfig(true) def := true if err != nil { kubeConfig = filepath.Join(config.DataDir, "kubeconfig-k3s.yaml") @@ -199,6 +211,8 @@ func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config } else { logrus.Errorf("failed to set %s to mode %s: %v", kubeConfig, os.FileMode(mode), err) } + } else { + os.Chmod(kubeConfig, os.FileMode(0644)) } logrus.Infof("Wrote kubeconfig %s", kubeConfig)