Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
cli/cmd: make clusterExists return error
Browse files Browse the repository at this point in the history
To remove a dependency on log.Entry to make moving this code around
easier and to avoid hiding function complexity from logger.

Also, it no longer requires logger, so argument has been removed.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 16, 2020
1 parent ac1dfa0 commit 8fc5016
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion cli/cmd/cluster-destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8fc5016

Please sign in to comment.