mirror of https://github.com/k3s-io/k3s
Create symlink for kubeconfig when --write-kubeconfig is selected
parent
1d42fc9755
commit
4c6cf29e02
|
@ -214,11 +214,17 @@ func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config
|
||||||
kubeConfig = filepath.Join(config.ControlConfig.DataDir, "kubeconfig-k3s.yaml")
|
kubeConfig = filepath.Join(config.ControlConfig.DataDir, "kubeconfig-k3s.yaml")
|
||||||
def = false
|
def = false
|
||||||
}
|
}
|
||||||
|
kubeConfigSymlink := kubeConfig
|
||||||
if config.ControlConfig.KubeConfigOutput != "" {
|
if config.ControlConfig.KubeConfigOutput != "" {
|
||||||
kubeConfig = 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 {
|
if err = clientaccess.AgentAccessInfoToKubeConfig(kubeConfig, url, clientToken); err != nil {
|
||||||
logrus.Errorf("Failed to generate kubeconfig: %v", err)
|
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))
|
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)
|
logrus.Infof("Wrote kubeconfig %s", kubeConfig)
|
||||||
if def {
|
if def {
|
||||||
logrus.Infof("Run: %s kubectl", filepath.Base(os.Args[0]))
|
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)
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue