Move default config location to /etc/rancher/k3s/k3s.yaml

pull/32/head v0.1.0-rc5
Darren Shepherd 2019-02-19 10:46:35 -07:00
parent 8690a277ed
commit b07727ae24
5 changed files with 23 additions and 6 deletions

View File

@ -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
```

View File

@ -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

View File

@ -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) {

View File

@ -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)
}

View File

@ -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)