Skip to content

Commit

Permalink
feature(main): reset cluster not delete nodes (#3991)
Browse files Browse the repository at this point in the history
* feature(main): reset cluster not delete nodes

Signed-off-by: cuisongliu <cuisongliu@qq.com>

* feature(main): reset cluster not delete nodes

Signed-off-by: cuisongliu <cuisongliu@qq.com>
(cherry picked from commit 6fc0bea)

---------

Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu authored and sealos-ci-robot@sealos.io committed Sep 29, 2023
1 parent cb75187 commit 657b8d2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/runtime/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/runtime/k3s/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/kubernetes/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 657b8d2

Please sign in to comment.