diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index 3c493f396..d2e8d6f44 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -64,7 +64,10 @@ func runClusterApply(cmd *cobra.Command, args []string) { //nolint:funlen func clusterApply(contextLogger *log.Entry) error { - c := initialize(contextLogger) + c, err := initialize(contextLogger) + if err != nil { + return fmt.Errorf("initializing: %w", err) + } exists := clusterExists(contextLogger, &c.terraformExecutor) if exists && !confirm { diff --git a/cli/cmd/cluster-destroy.go b/cli/cmd/cluster-destroy.go index 75295e710..ab355e499 100644 --- a/cli/cmd/cluster-destroy.go +++ b/cli/cmd/cluster-destroy.go @@ -48,7 +48,10 @@ func runClusterDestroy(cmd *cobra.Command, args []string) { } func clusterDestroy(contextLogger *log.Entry) error { - c := initialize(contextLogger) + c, err := initialize(contextLogger) + if err != nil { + return fmt.Errorf("initializing: %w", err) + } if !clusterExists(contextLogger, &c.terraformExecutor) { contextLogger.Println("Cluster already destroyed, nothing to do") diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index 5e70253ba..e7bd2ad2e 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -52,10 +52,10 @@ type cluster struct { // initialize does common initialization actions between cluster operations // and returns created objects to the caller for further use. -func initialize(contextLogger *log.Entry) *cluster { +func initialize(contextLogger *log.Entry) (*cluster, error) { lokoConfig, diags := getLokoConfig() if diags.HasErrors() { - contextLogger.Fatal(diags) + return nil, diags } p, diags := getConfiguredPlatform(lokoConfig, true) @@ -64,7 +64,7 @@ func initialize(contextLogger *log.Entry) *cluster { contextLogger.Error(diagnostic.Error()) } - contextLogger.Fatal("Errors found while loading cluster configuration") + return nil, fmt.Errorf("loading platform configuration") } // Get the configured backend for the cluster. Backend types currently supported: local, s3. @@ -74,7 +74,7 @@ func initialize(contextLogger *log.Entry) *cluster { contextLogger.Error(diagnostic.Error()) } - contextLogger.Fatal("Errors found while loading cluster configuration") + return nil, fmt.Errorf("loading backend configuration") } // Use a local backend if no backend is configured. @@ -84,12 +84,12 @@ func initialize(contextLogger *log.Entry) *cluster { assetDir, err := homedir.Expand(p.Meta().AssetDir) if err != nil { - contextLogger.Fatalf("Error expanding path: %v", err) + return nil, fmt.Errorf("expanding path %q: %v", p.Meta().AssetDir, err) } // Validate backend configuration. if err = b.Validate(); err != nil { - contextLogger.Fatalf("Failed to validate backend configuration: %v", err) + return nil, fmt.Errorf("validating backend configuration: %v", err) } ex := initializeTerraform(contextLogger, p, b) @@ -99,7 +99,7 @@ func initialize(contextLogger *log.Entry) *cluster { platform: p, lokomotiveConfig: lokoConfig, assetDir: assetDir, - } + }, nil } // initializeTerraform initialized Terraform directory using given backend and platform diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index 27c20253f..60bc634be 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -187,7 +187,10 @@ func readKubeconfigFromTerraformState(contextLogger *log.Entry) ([]byte, error) contextLogger.Warn("Kubeconfig file not found in assets directory, pulling kubeconfig from " + "Terraform state, this might be slow. Run 'lokoctl cluster apply' to fix it.") - c := initialize(contextLogger) + c, err := initialize(contextLogger) + if err != nil { + return nil, fmt.Errorf("initializing: %w", err) + } kubeconfig := ""