Skip to content

Commit

Permalink
Move default config location to /etc/rancher/k3s/k3s.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 19, 2019
1 parent 8690a27 commit b07727a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```

Expand Down
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/datadir/datadir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit b07727a

Please sign in to comment.