From 657b8d287759093e22f40410f2589bbb88be0141 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Fri, 29 Sep 2023 22:44:32 +0800 Subject: [PATCH] feature(main): reset cluster not delete nodes (#3991) * feature(main): reset cluster not delete nodes Signed-off-by: cuisongliu * feature(main): reset cluster not delete nodes Signed-off-by: cuisongliu (cherry picked from commit 6fc0bea73f5b2c0f3c3e39de5cd926c6738249ee) --------- Signed-off-by: cuisongliu --- pkg/runtime/k3s/k3s.go | 4 ++-- pkg/runtime/k3s/lifecycle.go | 18 ++++++++++++++++-- pkg/runtime/kubernetes/reset.go | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/runtime/k3s/k3s.go b/pkg/runtime/k3s/k3s.go index 225c19b069b..2cb318a4541 100644 --- a/pkg/runtime/k3s/k3s.go +++ b/pkg/runtime/k3s/k3s.go @@ -94,13 +94,13 @@ func (k *K3s) ScaleUp(masters []string, nodes []string) error { func (k *K3s) ScaleDown(masters []string, nodes []string) error { if len(masters) != 0 { logger.Info("master %s will be deleted", masters) - if err := k.resetNodes(masters); err != nil { + if err := k.removeNodes(masters); err != nil { return err } } if len(nodes) != 0 { logger.Info("worker %s will be deleted", nodes) - return k.resetNodes(nodes) + return k.removeNodes(nodes) } return nil } diff --git a/pkg/runtime/k3s/lifecycle.go b/pkg/runtime/k3s/lifecycle.go index c580e5bbd71..9015297e934 100644 --- a/pkg/runtime/k3s/lifecycle.go +++ b/pkg/runtime/k3s/lifecycle.go @@ -30,6 +30,17 @@ import ( ) func (k *K3s) resetNodes(nodes []string) error { + eg, _ := errgroup.WithContext(context.Background()) + for i := range nodes { + node := nodes[i] + eg.Go(func() error { + return k.resetNode(node) + }) + } + return eg.Wait() +} + +func (k *K3s) removeNodes(nodes []string) error { eg, _ := errgroup.WithContext(context.Background()) for i := range nodes { node := nodes[i] @@ -50,7 +61,7 @@ func (k *K3s) resetNode(host string) error { if removeKubeConfigErr != nil { logger.Error("failed to clean node, exec command %s failed, %v", removeKubeConfig, removeKubeConfigErr) } - if slices.Contains(k.cluster.GetNodeIPList(), host) { + if slices.Contains(k.cluster.GetNodeIPAndPortList(), host) { vipAndPort := fmt.Sprintf("%s:%d", k.cluster.GetVIP(), k.config.APIServerPort) ipvsclearErr := k.remoteUtil.IPVSClean(host, vipAndPort) if ipvsclearErr != nil { @@ -63,7 +74,10 @@ func (k *K3s) resetNode(host string) error { // TODO: remove from API func (k *K3s) deleteNode(node string) error { //remove master - masterIPs := strings.RemoveFromSlice(k.cluster.GetMasterIPList(), node) + masterIPs := k.cluster.GetMasterIPList() + if slices.Contains(k.cluster.GetMasterIPAndPortList(), node) { + masterIPs = strings.RemoveFromSlice(k.cluster.GetMasterIPList(), node) + } if len(masterIPs) > 0 { // TODO: do we need draining first? if err := k.removeNode(node); err != nil { diff --git a/pkg/runtime/kubernetes/reset.go b/pkg/runtime/kubernetes/reset.go index 2d65ae992cd..ebce74b3813 100644 --- a/pkg/runtime/kubernetes/reset.go +++ b/pkg/runtime/kubernetes/reset.go @@ -81,7 +81,7 @@ func (k *KubeadmRuntime) resetNode(node string, cleanHook func()) error { if removeKubeConfigErr != nil { logger.Error("failed to clean node, exec command %s failed, %v", removeKubeConfig, removeKubeConfigErr) } - if slices.Contains(k.cluster.GetNodeIPList(), node) { + if slices.Contains(k.cluster.GetNodeIPAndPortList(), node) { ipvscleanErr := k.execIPVSClean(node) if ipvscleanErr != nil { logger.Error("failed to clean node route and ipvs failed, %v", ipvscleanErr)