From 782855308cd9f817c6525617a2a45e9aea4859f5 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 4 Sep 2021 09:54:50 +0100
Subject: [PATCH 2/2] remove residual in infrastruce

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 pkg/clusterdeployment/binary/binary.go        | 30 -------------------
 .../binary/infrastructure/infrastructure.go   | 12 ++++----
 pkg/clusterdeployment/clusterdeploy.go        |  1 -
 3 files changed, 7 insertions(+), 36 deletions(-)

diff --git a/pkg/clusterdeployment/binary/binary.go b/pkg/clusterdeployment/binary/binary.go
index 4596047..246f547 100644
--- a/pkg/clusterdeployment/binary/binary.go
+++ b/pkg/clusterdeployment/binary/binary.go
@@ -444,37 +444,11 @@ func (bcp *BinaryClusterDeployment) PreDeleteClusterHooks() {
 	}
 }
 
-func clearResiduals(nodes []*api.HostConfig, confDir string) error {
-	if len(nodes) == 0 {
-		return nil
-	}
-
-	var strNodes []string
-	for _, n := range nodes {
-		strNodes = append(strNodes, n.Address)
-	}
-
-	shell := fmt.Sprintf("#!/bin/bash\nrm -rf %s\nexit 0", confDir)
-
-	checker := task.NewTaskIgnoreErrInstance(
-		&commontools.RunShellTask{
-			ShellName: "checkMaster",
-			Shell:     shell,
-		},
-	)
-
-	return nodemanager.RunTaskOnNodes(checker, strNodes)
-}
-
 func (bcp *BinaryClusterDeployment) PostDeleteClusterHooks() {
 	role := []uint16{api.Worker, api.Master, api.ETCD, api.LoadBalance}
 	if err := dependency.HookSchedule(bcp.config, bcp.config.Nodes, role, api.SchedulePostCleanup); err != nil {
 		logrus.Warnf("Ignore: Delete cluster PostHook failed: %v", err)
 	}
-	// clear all residual objects
-	if err := clearResiduals(bcp.config.Nodes, bcp.config.GetConfigDir()); err != nil {
-		logrus.Warnf("Ignore: Delete cluster PostHook failed: %v", err)
-	}
 }
 
 func (bcp *BinaryClusterDeployment) PreNodeJoinHooks(node *api.HostConfig) error {
@@ -588,8 +562,4 @@ func (bcp *BinaryClusterDeployment) PostNodeCleanupHooks(node *api.HostConfig) {
 	if err := dependency.HookSchedule(bcp.config, []*api.HostConfig{node}, role, api.SchedulePostCleanup); err != nil {
 		logrus.Warnf("Ignore: Delete Node PostHook failed: %v", err)
 	}
-
-	if err := clearResiduals([]*api.HostConfig{node}, bcp.config.GetConfigDir()); err != nil {
-		logrus.Warnf("Ignore: Delete cluster PostHook failed: %v", err)
-	}
 }
diff --git a/pkg/clusterdeployment/binary/infrastructure/infrastructure.go b/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
index 71ad610..634e338 100644
--- a/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
+++ b/pkg/clusterdeployment/binary/infrastructure/infrastructure.go
@@ -312,8 +312,9 @@ func NodeInfrastructureSetup(config *api.ClusterConfig, nodeID string, role uint
 }
 
 type DestroyInfraTask struct {
-	packageSrc *api.PackageSrcConfig
-	roleInfra  *api.RoleInfra
+	packageSrc   *api.PackageSrcConfig
+	roleInfra    *api.RoleInfra
+	k8sConfigDir string
 }
 
 func (it *DestroyInfraTask) Name() string {
@@ -348,7 +349,7 @@ func (it *DestroyInfraTask) Run(r runner.Runner, hcg *api.HostConfig) error {
 		return nil
 	}
 	copyTempDir := getCopyDefaultDir(hcg.UserName)
-	if _, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"rm -rf %s %s\"", dstDir, copyTempDir)); err != nil {
+	if _, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"rm -rf %s %s %s\"", dstDir, copyTempDir, it.k8sConfigDir)); err != nil {
 		return fmt.Errorf("rm dependency failed: %v", err)
 	}
 
@@ -429,8 +430,9 @@ func NodeInfrastructureDestroy(config *api.ClusterConfig, hostconfig *api.HostCo
 
 	itask := task.NewTaskIgnoreErrInstance(
 		&DestroyInfraTask{
-			packageSrc: &config.PackageSrc,
-			roleInfra:  roleInfra,
+			packageSrc:   &config.PackageSrc,
+			roleInfra:    roleInfra,
+			k8sConfigDir: config.GetConfigDir(),
 		})
 
 	if err := nodemanager.RunTaskOnNodes(itask, []string{hostconfig.Address}); err != nil {
diff --git a/pkg/clusterdeployment/clusterdeploy.go b/pkg/clusterdeployment/clusterdeploy.go
index f31ac42..ee57ec3 100644
--- a/pkg/clusterdeployment/clusterdeploy.go
+++ b/pkg/clusterdeployment/clusterdeploy.go
@@ -216,7 +216,6 @@ func rollbackFailedNoeds(handler api.ClusterDeploymentAPI, nodes []*api.HostConf
 	for _, n := range nodes {
 		// do best to cleanup, if error, just ignore
 		handler.ClusterNodeCleanup(n, n.Type)
-		handler.PostNodeCleanupHooks(n)
 		handler.MachineInfraDestroy(n)
 		rollIDs = append(rollIDs, n.Address)
 	}
-- 
2.25.1

