Skip to content

Commit

Permalink
add confirm for cluster remove
Browse files Browse the repository at this point in the history
Signed-off-by: xin.li <xin.li@daocloud.io>
  • Loading branch information
my-git9 committed Aug 18, 2022
1 parent 5181bc1 commit b437890
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
54 changes: 40 additions & 14 deletions cmd/argocd/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func printClusterDetails(clusters []argoappv1.Cluster) {

// NewClusterRemoveCommand returns a new instance of an `argocd cluster rm` command
func NewClusterRemoveCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clientcmd.PathOptions) *cobra.Command {
var noPrompt bool
var command = &cobra.Command{
Use: "rm SERVER/NAME",
Short: "Remove cluster credentials",
Expand All @@ -276,31 +277,56 @@ argocd cluster rm cluster-name`,
}
conn, clusterIf := headless.NewClientOrDie(clientOpts, c).NewClusterClientOrDie()
defer io.Close(conn)
var numOfClusters = len(args)
var isConfirmAll bool = false

for _, clusterSelector := range args {
clusterQuery := getQueryBySelector(clusterSelector)
var lowercaseAnswer string
if !noPrompt {
if numOfClusters == 1 {
lowercaseAnswer = cli.AskToProceedS("Are you sure you want to remove '" + clusterSelector + "'? Any Apps deploying to this cluster will go to health status Unknown.[y/n] ")
} else {
if !isConfirmAll {
lowercaseAnswer = cli.AskToProceedS("Are you sure you want to remove '" + clusterSelector + "'? Any Apps deploying to this cluster will go to health status Unknown.[y/n/A] where 'A' is to remove all specified apps and their resources without prompting ")
if lowercaseAnswer == "a" {
lowercaseAnswer = "y"
isConfirmAll = true
}
} else {
lowercaseAnswer = "y"
}
}
} else {
lowercaseAnswer = "y"
}

// get the cluster name to use as context to delete RBAC on cluster
clst, err := clusterIf.Get(ctx, clusterQuery)
errors.CheckError(err)
if lowercaseAnswer == "y" {
// get the cluster name to use as context to delete RBAC on cluster
clst, err := clusterIf.Get(ctx, clusterQuery)
errors.CheckError(err)

// remove cluster
_, err = clusterIf.Delete(ctx, clusterQuery)
errors.CheckError(err)
fmt.Printf("Cluster '%s' removed\n", clusterSelector)
// remove cluster
_, err = clusterIf.Delete(ctx, clusterQuery)
errors.CheckError(err)
fmt.Printf("Cluster '%s' removed\n", clusterSelector)

// remove RBAC from cluster
conf, err := getRestConfig(pathOpts, clst.Name)
errors.CheckError(err)
// remove RBAC from cluster
conf, err := getRestConfig(pathOpts, clst.Name)
errors.CheckError(err)

clientset, err := kubernetes.NewForConfig(conf)
errors.CheckError(err)
clientset, err := kubernetes.NewForConfig(conf)
errors.CheckError(err)

err = clusterauth.UninstallClusterManagerRBAC(clientset)
errors.CheckError(err)
err = clusterauth.UninstallClusterManagerRBAC(clientset)
errors.CheckError(err)
} else {
fmt.Println("The command to remove '" + clusterSelector + "' was cancelled.")
}
}
},
}
command.Flags().BoolVarP(&noPrompt, "yes", "y", false, "Turn off prompting to confirm remove of cluster resources")
return command
}

Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_cluster_rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ argocd cluster rm cluster-name

```
-h, --help help for rm
-y, --yes Turn off prompting to confirm remove of cluster resources
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/fixture/cluster/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ func (a *Actions) Get() *Actions {
func (a *Actions) DeleteByName() *Actions {
a.context.t.Helper()

a.runCli("cluster", "rm", a.context.name)
a.runCli("cluster", "rm", a.context.name, "--yes")
return a
}

func (a *Actions) DeleteByServer() *Actions {
a.context.t.Helper()

a.runCli("cluster", "rm", a.context.server)
a.runCli("cluster", "rm", a.context.server, "--yes")
return a
}

Expand Down

0 comments on commit b437890

Please sign in to comment.