mirror of
https://github.com/rancher/rke.git
synced 2025-09-16 15:10:12 +00:00
Rebuild local admin if the current cp host is down
This commit is contained in:
@@ -171,26 +171,27 @@ func GetLocalKubeConfig(configPath string) string {
|
||||
}
|
||||
|
||||
func rebuildLocalAdminConfig(kubeCluster *Cluster) error {
|
||||
logrus.Infof("[reconcile] Rebuilding and update local kube config")
|
||||
var workingConfig string
|
||||
logrus.Infof("[reconcile] Rebuilding and updating local kube config")
|
||||
var workingConfig, newConfig string
|
||||
currentKubeConfig := kubeCluster.Certificates[pki.KubeAdminCommonName]
|
||||
caCrt := kubeCluster.Certificates[pki.CACertName].Certificate
|
||||
for _, cpHost := range kubeCluster.ControlPlaneHosts {
|
||||
newConfig := pki.GetKubeConfigX509WithData(
|
||||
"https://"+cpHost.Address+":6443",
|
||||
pki.KubeAdminCommonName,
|
||||
string(cert.EncodeCertPEM(caCrt)),
|
||||
string(cert.EncodeCertPEM(currentKubeConfig.Certificate)),
|
||||
string(cert.EncodePrivateKeyPEM(currentKubeConfig.Key)))
|
||||
|
||||
if (currentKubeConfig == pki.CertificatePKI{}) {
|
||||
kubeCluster.Certificates = make(map[string]pki.CertificatePKI)
|
||||
newConfig = getLocalAdminConfigWithNewAddress(kubeCluster.LocalKubeConfigPath, cpHost.Address)
|
||||
} else {
|
||||
kubeURL := fmt.Sprintf("https://%s:6443", cpHost.Address)
|
||||
caData := string(cert.EncodeCertPEM(caCrt))
|
||||
crtData := string(cert.EncodeCertPEM(currentKubeConfig.Certificate))
|
||||
keyData := string(cert.EncodePrivateKeyPEM(currentKubeConfig.Key))
|
||||
newConfig = pki.GetKubeConfigX509WithData(kubeURL, pki.KubeAdminCommonName, caData, crtData, keyData)
|
||||
}
|
||||
if err := pki.DeployAdminConfig(newConfig, kubeCluster.LocalKubeConfigPath); err != nil {
|
||||
return fmt.Errorf("Failed to redeploy local admin config with new host")
|
||||
}
|
||||
workingConfig = newConfig
|
||||
if _, err := GetK8sVersion(kubeCluster.LocalKubeConfigPath); err != nil {
|
||||
logrus.Infof("[reconcile] host [%s] is not active master on the cluster", cpHost.Address)
|
||||
continue
|
||||
} else {
|
||||
if _, err := GetK8sVersion(kubeCluster.LocalKubeConfigPath); err == nil {
|
||||
logrus.Infof("[reconcile] host [%s] is active master on the cluster", cpHost.Address)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -216,3 +217,14 @@ func getLocalConfigAddress(localConfigPath string) (string, error) {
|
||||
address := splittedAdress[1]
|
||||
return address[2:], nil
|
||||
}
|
||||
|
||||
func getLocalAdminConfigWithNewAddress(localConfigPath, cpAddress string) string {
|
||||
config, _ := clientcmd.BuildConfigFromFlags("", localConfigPath)
|
||||
config.Host = fmt.Sprintf("https://%s:6443", cpAddress)
|
||||
return pki.GetKubeConfigX509WithData(
|
||||
"https://"+cpAddress+":6443",
|
||||
pki.KubeAdminCommonName,
|
||||
string(config.CAData),
|
||||
string(config.CertData),
|
||||
string(config.KeyData))
|
||||
}
|
||||
|
@@ -17,13 +17,6 @@ func ReconcileCluster(kubeCluster, currentCluster *Cluster) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
// to handle if current local admin is down and we need to use new cp from the list
|
||||
if !isLocalConfigWorking(kubeCluster.LocalKubeConfigPath) {
|
||||
if err := rebuildLocalAdminConfig(kubeCluster); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
kubeClient, err := k8s.NewClient(kubeCluster.LocalKubeConfigPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to initialize new kubernetes client: %v", err)
|
||||
|
@@ -38,6 +38,13 @@ func (c *Cluster) GetClusterState() (*Cluster, error) {
|
||||
if _, err = os.Stat(c.LocalKubeConfigPath); !os.IsNotExist(err) {
|
||||
logrus.Infof("[state] Found local kube config file, trying to get state from cluster")
|
||||
|
||||
// to handle if current local admin is down and we need to use new cp from the list
|
||||
if !isLocalConfigWorking(c.LocalKubeConfigPath) {
|
||||
if err := rebuildLocalAdminConfig(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// initiate kubernetes client
|
||||
c.KubeClient, err = k8s.NewClient(c.LocalKubeConfigPath)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user