From f57d83e82512cd423583f2e41aeb35ad348fa21c Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Wed, 3 Nov 2021 04:01:33 +0000
Subject: [PATCH 03/12] support disable rollback for deploy cluster

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 Makefile                               | 10 +--
 cmd/deploy.go                          | 84 +++++++++++++++-----------
 cmd/opts.go                            | 38 ++++++------
 pkg/clusterdeployment/clusterdeploy.go |  6 +-
 4 files changed, 78 insertions(+), 60 deletions(-)

diff --git a/Makefile b/Makefile
index 3fac780..4b6d7c7 100644
--- a/Makefile
+++ b/Makefile
@@ -2,9 +2,11 @@ GIT_COMMIT ?= $(if $(shell git rev-parse --short HEAD),$(shell git rev-parse --s
 SOURCE_DATE_EPOCH ?= $(if $(shell date +%s),$(shell date +%s),$(error "date failed"))
 VERSION := $(shell cat ./VERSION)
 # eggo arch amd64/arm64
-ARCH ?= amd64
-ifeq ($(shell uname -p),aarch64)
-ARCH ?= arm64
+ifndef ARCH
+ARCH = amd64
+ifeq ($(shell uname -p), aarch64)
+ARCH = arm64
+endif
 endif
 
 EXTRALDFLAGS :=
@@ -21,7 +23,7 @@ GO_BUILD := CGO_ENABLED=0 GOARCH=$(ARCH) $(GO)
 
 .PHONY: eggo
 eggo:
-	@echo "build eggo starting..."
+	@echo "build eggo of $(ARCH) starting..."
 	@$(GO_BUILD) build -ldflags '$(LDFLAGS) $(STATIC_LDFLAGS)' -o bin/eggo .
 	@echo "build eggo done!"
 local:
diff --git a/cmd/deploy.go b/cmd/deploy.go
index 094fb1d..e21bcc5 100644
--- a/cmd/deploy.go
+++ b/cmd/deploy.go
@@ -25,6 +25,47 @@ import (
 	"isula.org/eggo/pkg/utils"
 )
 
+func removeFailedNodes(cstatus *api.ClusterStatus, conf *DeployConfig) {
+	// if partial success, just update config of cluster, remove failed nodes
+	if cstatus.FailureCnt == 0 {
+		return
+	}
+
+	var tmp []*HostConfig
+	for _, n := range conf.Masters {
+		if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
+			continue
+		}
+		tmp = append(tmp, n)
+	}
+	conf.Masters = tmp
+
+	tmp = nil
+	for _, n := range conf.Workers {
+		if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
+			continue
+		}
+		tmp = append(tmp, n)
+	}
+	conf.Workers = tmp
+
+	tmp = nil
+	for _, n := range conf.Etcds {
+		if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
+			continue
+		}
+		tmp = append(tmp, n)
+	}
+	conf.Etcds = tmp
+
+	if err := saveDeployConfig(conf, savedDeployConfigPath(conf.ClusterID)); err != nil {
+		fmt.Printf("Warn: failed to save config!!!\n")
+		fmt.Printf("	you can call \"eggo delete --id %s [failed nodes id]\" to remove failed node from your cluster.\n", conf.ClusterID)
+		return
+	}
+	fmt.Printf("update config of cluster: %s", conf.ClusterID)
+}
+
 func deploy(conf *DeployConfig) error {
 	if err := saveDeployConfig(conf, savedDeployConfigPath(conf.ClusterID)); err != nil {
 		return fmt.Errorf("save deploy config failed: %v", err)
@@ -32,47 +73,18 @@ func deploy(conf *DeployConfig) error {
 
 	ccfg := toClusterdeploymentConfig(conf)
 
-	cstatus, err := clusterdeployment.CreateCluster(ccfg)
+	cstatus, err := clusterdeployment.CreateCluster(ccfg, opts.deployEnableRollback)
 	if err != nil {
 		return err
 	}
 
-	if cstatus.FailureCnt > 0 {
-		// if partial success, just update config of cluster, remove failed nodes
-		var tmp []*HostConfig
-		for _, n := range conf.Masters {
-			if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
-				continue
-			}
-			tmp = append(tmp, n)
-		}
-		conf.Masters = tmp
-
-		tmp = nil
-		for _, n := range conf.Workers {
-			if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
-				continue
-			}
-			tmp = append(tmp, n)
-		}
-		conf.Workers = tmp
-
-		tmp = nil
-		for _, n := range conf.Etcds {
-			if success, ok := cstatus.StatusOfNodes[n.Ip]; ok && !success {
-				continue
-			}
-			tmp = append(tmp, n)
-		}
-		conf.Etcds = tmp
-
-		err = saveDeployConfig(conf, savedDeployConfigPath(conf.ClusterID))
-		if err != nil {
-			fmt.Printf("")
-			clusterdeployment.RemoveCluster(ccfg)
-			return fmt.Errorf("update config of cluster failed: %v", err)
+	// if disable rollback, just ignore error, and wait user to cleanup
+	if opts.deployEnableRollback {
+		removeFailedNodes(&cstatus, conf)
+	} else {
+		if cstatus.FailureCnt > 0 {
+			fmt.Printf("Warn: you can call \"eggo delete --id %s [failed nodes id]\" to remove failed node from your cluster.\n", conf.ClusterID)
 		}
-		fmt.Printf("update config of cluster: %s", conf.ClusterID)
 	}
 
 	fmt.Print(cstatus.Show())
diff --git a/cmd/opts.go b/cmd/opts.go
index 2b81b07..f5204f2 100644
--- a/cmd/opts.go
+++ b/cmd/opts.go
@@ -24,24 +24,25 @@ import (
 )
 
 type eggoOptions struct {
-	name             string
-	templateConfig   string
-	masters          []string
-	nodes            []string
-	etcds            []string
-	loadbalance      string
-	username         string
-	password         string
-	deployConfig     string
-	cleanupConfig    string
-	cleanupClusterID string
-	debug            bool
-	version          bool
-	joinType         string
-	joinClusterID    string
-	joinYaml         string
-	joinHost         HostConfig
-	delClusterID     string
+	name                 string
+	templateConfig       string
+	masters              []string
+	nodes                []string
+	etcds                []string
+	loadbalance          string
+	username             string
+	password             string
+	deployConfig         string
+	deployEnableRollback bool
+	cleanupConfig        string
+	cleanupClusterID     string
+	debug                bool
+	version              bool
+	joinType             string
+	joinClusterID        string
+	joinYaml             string
+	joinHost             HostConfig
+	delClusterID         string
 }
 
 var opts eggoOptions
@@ -64,6 +65,7 @@ func setupEggoCmdOpts(eggoCmd *cobra.Command) {
 func setupDeployCmdOpts(deployCmd *cobra.Command) {
 	flags := deployCmd.Flags()
 	flags.StringVarP(&opts.deployConfig, "file", "f", defaultDeployConfigPath(), "location of cluster deploy config file, default $HOME/.eggo/deploy.yaml")
+	flags.BoolVarP(&opts.deployEnableRollback, "rollback", "", true, "rollback failed node to cleanup")
 }
 
 func setupCleanupCmdOpts(cleanupCmd *cobra.Command) {
diff --git a/pkg/clusterdeployment/clusterdeploy.go b/pkg/clusterdeployment/clusterdeploy.go
index ee57ec3..27167c4 100644
--- a/pkg/clusterdeployment/clusterdeploy.go
+++ b/pkg/clusterdeployment/clusterdeploy.go
@@ -225,7 +225,7 @@ func rollbackFailedNoeds(handler api.ClusterDeploymentAPI, nodes []*api.HostConf
 	}
 }
 
-func CreateCluster(cc *api.ClusterConfig) (api.ClusterStatus, error) {
+func CreateCluster(cc *api.ClusterConfig, deployEnableRollback bool) (api.ClusterStatus, error) {
 	cstatus := api.ClusterStatus{
 		StatusOfNodes: make(map[string]bool),
 	}
@@ -262,7 +262,9 @@ func CreateCluster(cc *api.ClusterConfig) (api.ClusterStatus, error) {
 		return cstatus, err
 	}
 	// rollback failed nodes
-	rollbackFailedNoeds(handler, failedNodes)
+	if deployEnableRollback {
+		rollbackFailedNoeds(handler, failedNodes)
+	}
 	// update status of cluster
 	if failedNodes != nil {
 		var failureIDs []string
-- 
2.25.1

