From 818ee6e412761d2539ad81da1c7282617d218855 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 16 Nov 2023 11:06:06 -0500 Subject: [PATCH] Add ability to soft delete clusters (#474) * Add ability to soft delete clusters Use the detach api to implement this, useful for deleting clusters that were never properly created or deleting w/o draining services if you want to leave them in-place. * small tweaks * small make tweak, add notice for detach --- Makefile | 3 ++- cmd/plural/cd_clusters.go | 10 +++++++++- pkg/console/clusters.go | 5 +++++ pkg/console/console.go | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ebf401ad..5f4ad221 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ WAILS_BINDINGS_TAGS ?= bindings,generate WAILS_BINDINGS_BINARY_NAME ?= wailsbindings TAGS ?= $(WAILS_TAGS) OUTFILE ?= plural.o +GOBIN ?= go env GOBIN # Targets to run before other targets # install-tools - Install binaries required to run targets @@ -38,7 +39,7 @@ git-push: .PHONY: install install: - go install -ldflags '$(LDFLAGS)' . + go build -ldflags '$(LDFLAGS)' -o $(GOBIN)/plural . .PHONY: build-cli build-cli: ## Build a CLI binary for the host architecture without embedded UI diff --git a/cmd/plural/cd_clusters.go b/cmd/plural/cd_clusters.go index d4b9eecb..222680ae 100644 --- a/cmd/plural/cd_clusters.go +++ b/cmd/plural/cd_clusters.go @@ -67,7 +67,10 @@ func (p *Plural) cdClusterCommands() []cli.Command { Usage: "deregisters a cluster in plural cd, and drains all services (unless --soft is specified)", ArgsUsage: "CLUSTER_ID", Flags: []cli.Flag{ - cli.BoolFlag{Name: "soft", Usage: "deletes a cluster in our system but doesn't drain resources, leaving them untouched"}, + cli.BoolFlag{ + Name: "soft", + Usage: "deletes a cluster in our system but doesn't drain resources, leaving them untouched", + }, }, }, { @@ -220,6 +223,11 @@ func (p *Plural) handleDeleteCluster(c *cli.Context) error { return fmt.Errorf("this cluster does not exist") } + if c.Bool("soft") { + fmt.Println("detaching cluster from Plural CD, this will leave all workloads running.") + return p.ConsoleClient.DetachCluster(existing.ID) + } + return p.ConsoleClient.DeleteCluster(existing.ID) } diff --git a/pkg/console/clusters.go b/pkg/console/clusters.go index 413dc154..6a54349f 100644 --- a/pkg/console/clusters.go +++ b/pkg/console/clusters.go @@ -51,6 +51,11 @@ func (c *consoleClient) DeleteCluster(id string) error { return api.GetErrorResponse(err, "DeleteCluster") } +func (c *consoleClient) DetachCluster(id string) error { + _, err := c.client.DetachCluster(c.ctx, id) + return api.GetErrorResponse(err, "DetachCluster") +} + func (c *consoleClient) CreateCluster(attributes consoleclient.ClusterAttributes) (*consoleclient.CreateCluster, error) { newCluster, err := c.client.CreateCluster(c.ctx, attributes) if err != nil { diff --git a/pkg/console/console.go b/pkg/console/console.go index c9c6c321..5b7ffef1 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -22,6 +22,7 @@ type ConsoleClient interface { GetCluster(clusterId, clusterName *string) (*consoleclient.ClusterFragment, error) UpdateCluster(id string, attr consoleclient.ClusterUpdateAttributes) (*consoleclient.UpdateCluster, error) DeleteCluster(id string) error + DetachCluster(id string) error ListClusterServices(clusterId, handle *string) ([]*consoleclient.ServiceDeploymentEdgeFragment, error) CreateRepository(url string, privateKey, passphrase, username, password *string) (*consoleclient.CreateGitRepository, error) ListRepositories() (*consoleclient.ListGitRepositories, error)