From 83d0d2dcaa507b8776f3cc84175ece97626f5de4 Mon Sep 17 00:00:00 2001 From: windydayc Date: Fri, 26 Aug 2022 15:44:16 +0800 Subject: [PATCH] support yurtadm join to join multiple master nodes (#964) --- .../kubernetes/kubeadm/app/constants/constants.go | 6 +++--- .../kubernetes/kubeadm/app/discovery/token/token.go | 7 +++++-- pkg/yurtadm/cmd/join/join.go | 2 ++ pkg/yurtadm/cmd/join/phases/joinnode.go | 11 ++++++++++- pkg/yurtadm/cmd/join/phases/preflight.go | 3 ++- pkg/yurtadm/util/kubernetes/util.go | 2 +- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/util/kubernetes/kubeadm/app/constants/constants.go b/pkg/util/kubernetes/kubeadm/app/constants/constants.go index 12c40d6a6e3..f2853a8eaaa 100644 --- a/pkg/util/kubernetes/kubeadm/app/constants/constants.go +++ b/pkg/util/kubernetes/kubeadm/app/constants/constants.go @@ -447,13 +447,13 @@ var ( ControlPlaneComponents = []string{KubeAPIServer, KubeControllerManager, KubeScheduler} // MinimumControlPlaneVersion specifies the minimum control plane version kubeadm can deploy - MinimumControlPlaneVersion = version.MustParseSemantic("v1.21.0") + MinimumControlPlaneVersion = version.MustParseSemantic("v1.18.0") // MinimumKubeletVersion specifies the minimum version of kubelet which kubeadm supports - MinimumKubeletVersion = version.MustParseSemantic("v1.21.0") + MinimumKubeletVersion = version.MustParseSemantic("v1.18.0") // CurrentKubernetesVersion specifies current Kubernetes version supported by kubeadm - CurrentKubernetesVersion = version.MustParseSemantic("v1.22.0") + CurrentKubernetesVersion = version.MustParseSemantic("v1.18.0") // SupportedEtcdVersion lists officially supported etcd versions with corresponding Kubernetes releases SupportedEtcdVersion = map[uint8]string{ diff --git a/pkg/util/kubernetes/kubeadm/app/discovery/token/token.go b/pkg/util/kubernetes/kubeadm/app/discovery/token/token.go index 8389d98b03d..f0a4c0a1915 100644 --- a/pkg/util/kubernetes/kubeadm/app/discovery/token/token.go +++ b/pkg/util/kubernetes/kubeadm/app/discovery/token/token.go @@ -21,6 +21,7 @@ import ( "bytes" "context" "fmt" + "strings" "time" "github.com/pkg/errors" @@ -57,7 +58,8 @@ func RetrieveBootstrapConfig(data joindata.YurtJoinData) (*clientcmdapi.Config, clusterinfo := kubeconfigutil.GetClusterFromKubeConfig(cfg) return kubeconfigutil.CreateWithToken( - fmt.Sprintf("https://%s", data.ServerAddr()), + // If there are multiple master IP addresses, take the first one here + fmt.Sprintf("https://%s", strings.Split(data.ServerAddr(), ",")[0]), "kubernetes", TokenUser, clusterinfo.CertificateAuthorityData, @@ -79,7 +81,8 @@ func retrieveValidatedConfigInfo(client clientset.Interface, data joindata.YurtJ return nil, err } - endpoint := data.ServerAddr() + // If there are multiple master IP addresses, take the first one here + endpoint := strings.Split(data.ServerAddr(), ",")[0] insecureBootstrapConfig := buildInsecureBootstrapKubeConfig(endpoint, "kubernetes") clusterName := insecureBootstrapConfig.Contexts[insecureBootstrapConfig.CurrentContext].Cluster diff --git a/pkg/yurtadm/cmd/join/join.go b/pkg/yurtadm/cmd/join/join.go index 17f81ffd6b5..6b14d446597 100644 --- a/pkg/yurtadm/cmd/join/join.go +++ b/pkg/yurtadm/cmd/join/join.go @@ -195,6 +195,8 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri if len(args) > 1 { klog.Warningf("[preflight] WARNING: More than one API server endpoint supplied on command line %v. Using the first one.", args) } + // if join multiple masters, apiServerEndpoint may be like: + // 1.2.3.4:6443,1.2.3.5:6443,1.2.3.6:6443 apiServerEndpoint = args[0] } diff --git a/pkg/yurtadm/cmd/join/phases/joinnode.go b/pkg/yurtadm/cmd/join/phases/joinnode.go index e9702cfe17e..91fa7dfa657 100644 --- a/pkg/yurtadm/cmd/join/phases/joinnode.go +++ b/pkg/yurtadm/cmd/join/phases/joinnode.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -168,8 +169,16 @@ func addYurthubStaticYaml(data joindata.YurtJoinData, podManifestPath string) er } } + // There can be multiple master IP addresses + serverAddrs := strings.Split(data.ServerAddr(), ",") + for i := 0; i < len(serverAddrs); i++ { + serverAddrs[i] = fmt.Sprintf("https://%s", serverAddrs[i]) + } + + kubernetesServerAddrs := strings.Join(serverAddrs, ",") + ctx := map[string]string{ - "kubernetesServerAddr": fmt.Sprintf("https://%s", data.ServerAddr()), + "kubernetesServerAddr": kubernetesServerAddrs, "image": data.YurtHubImage(), "joinToken": data.JoinToken(), "workingMode": data.NodeRegistration().WorkingMode, diff --git a/pkg/yurtadm/cmd/join/phases/preflight.go b/pkg/yurtadm/cmd/join/phases/preflight.go index ee6bacff026..53cd9c0e8c1 100644 --- a/pkg/yurtadm/cmd/join/phases/preflight.go +++ b/pkg/yurtadm/cmd/join/phases/preflight.go @@ -71,7 +71,8 @@ func runPreflight(c workflow.RunData) error { Discovery: kubeadmapi.Discovery{ TLSBootstrapToken: data.JoinToken(), BootstrapToken: &kubeadmapi.BootstrapTokenDiscovery{ - APIServerEndpoint: data.ServerAddr(), + // If there are multiple master IP addresses, take the first one here + APIServerEndpoint: strings.Split(data.ServerAddr(), ",")[0], Token: data.JoinToken()}, }, ControlPlane: nil, diff --git a/pkg/yurtadm/util/kubernetes/util.go b/pkg/yurtadm/util/kubernetes/util.go index 3c6bcd6499c..73437884713 100644 --- a/pkg/yurtadm/util/kubernetes/util.go +++ b/pkg/yurtadm/util/kubernetes/util.go @@ -127,7 +127,7 @@ func CheckAndInstallKubelet(kubernetesResourceServer, clusterVersion string) err savePath := fmt.Sprintf("%s/kubernetes-node-linux-%s.tar.gz", constants.TmpDownloadDir, runtime.GOARCH) klog.V(1).Infof("Download kubelet from: %s", packageUrl) if err := util.DownloadFile(packageUrl, savePath, 3); err != nil { - return fmt.Errorf("Download kuelet fail: %w", err) + return fmt.Errorf("Download kubelet fail: %w", err) } if err := util.Untar(savePath, constants.TmpDownloadDir); err != nil { return err