diff --git a/pkg/server/server.go b/pkg/server/server.go index 43a7d4e920..0481056b59 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -214,11 +214,17 @@ func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config kubeConfig = filepath.Join(config.ControlConfig.DataDir, "kubeconfig-k3s.yaml") def = false } - + kubeConfigSymlink := kubeConfig if config.ControlConfig.KubeConfigOutput != "" { kubeConfig = config.ControlConfig.KubeConfigOutput } + if isSymlink(kubeConfigSymlink) { + if err := os.Remove(kubeConfigSymlink); err != nil { + logrus.Errorf("failed to remove kubeconfig symlink") + } + } + if err = clientaccess.AgentAccessInfoToKubeConfig(kubeConfig, url, clientToken); err != nil { logrus.Errorf("Failed to generate kubeconfig: %v", err) } @@ -234,6 +240,12 @@ func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config os.Chmod(kubeConfig, os.FileMode(0644)) } + if kubeConfigSymlink != kubeConfig { + if err := writeConfigSymlink(kubeConfig, kubeConfigSymlink); err != nil { + logrus.Errorf("failed to write kubeconfig symlink: %v", err) + } + } + logrus.Infof("Wrote kubeconfig %s", kubeConfig) if def { logrus.Infof("Run: %s kubectl", filepath.Base(os.Args[0])) @@ -307,3 +319,23 @@ func setNoProxyEnv(config *config.Control) error { }, ",") return os.Setenv("NO_PROXY", envList) } + +func writeConfigSymlink(kubeconfig, kubeconfigSymlink string) error { + if err := os.Remove(kubeconfigSymlink); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to remove %s file: %v", kubeconfigSymlink, err) + } + if err := os.MkdirAll(filepath.Dir(kubeconfigSymlink), 0755); err != nil { + return fmt.Errorf("failed to create path for symlink: %v", err) + } + if err := os.Symlink(kubeconfig, kubeconfigSymlink); err != nil { + return fmt.Errorf("failed to create symlink: %v", err) + } + return nil +} + +func isSymlink(config string) bool { + if fi, err := os.Lstat(config); err == nil && (fi.Mode()&os.ModeSymlink == os.ModeSymlink) { + return true + } + return false +}