diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index d2e8d6f44..5ddfaee3a 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -69,7 +69,11 @@ func clusterApply(contextLogger *log.Entry) error { return fmt.Errorf("initializing: %w", err) } - exists := clusterExists(contextLogger, &c.terraformExecutor) + exists, err := clusterExists(c.terraformExecutor) + if err != nil { + return fmt.Errorf("checking if cluster exists: %w", err) + } + if exists && !confirm { // TODO: We could plan to a file and use it when installing. if err := c.terraformExecutor.Plan(); err != nil { diff --git a/cli/cmd/cluster-destroy.go b/cli/cmd/cluster-destroy.go index ab355e499..854d77942 100644 --- a/cli/cmd/cluster-destroy.go +++ b/cli/cmd/cluster-destroy.go @@ -53,7 +53,12 @@ func clusterDestroy(contextLogger *log.Entry) error { return fmt.Errorf("initializing: %w", err) } - if !clusterExists(contextLogger, &c.terraformExecutor) { + exists, err := clusterExists(c.terraformExecutor) + if err != nil { + return fmt.Errorf("checking if cluster exists: %w", err) + } + + if !exists { contextLogger.Println("Cluster already destroyed, nothing to do") return nil diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index c1f684d3d..769ae244c 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -148,14 +148,14 @@ func initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Exe // clusterExists determines if cluster has already been created by getting all // outputs from the Terraform. If there is any output defined, it means 'terraform apply' // run at least once. -func clusterExists(contextLogger *log.Entry, ex *terraform.Executor) bool { +func clusterExists(ex terraform.Executor) (bool, error) { o := map[string]interface{}{} if err := ex.Output("", &o); err != nil { - contextLogger.Fatalf("Failed to check if cluster exists: %v", err) + return false, fmt.Errorf("getting Terraform output: %w", err) } - return len(o) != 0 + return len(o) != 0, nil } type controlplaneUpdater struct {