From cccfa49601adf7895c56b653143309ebc0ca26bb Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 25 Sep 2020 13:16:12 +0200 Subject: [PATCH] cli/cmd: make upgradeComponent() return error To remove a dependency on log.Entry to make moving this code around easier and to avoid hiding function complexity from logger. Part of #630 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster-apply.go | 4 +++- cli/cmd/cluster.go | 21 +++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index 5ddfaee3a..2b0b9bcaf 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -129,7 +129,9 @@ func clusterApply(contextLogger *log.Entry) error { } for _, c := range charts { - cu.upgradeComponent(c.Name, c.Namespace) + if err := cu.upgradeComponent(c.Name, c.Namespace); err != nil { + return fmt.Errorf("upgrading controlplane component %q: %w", c.Name, err) + } } } diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index 769ae244c..2ede30afd 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -192,30 +192,25 @@ func (c controlplaneUpdater) getControlplaneValues(name string) (map[string]inte return values, nil } -func (c controlplaneUpdater) upgradeComponent(component, namespace string) { - contextLogger := c.contextLogger.WithFields(log.Fields{ - "action": "controlplane-upgrade", - "component": component, - }) - +func (c controlplaneUpdater) upgradeComponent(component, namespace string) error { actionConfig, err := util.HelmActionConfig(namespace, c.kubeconfig) if err != nil { - contextLogger.Fatalf("Failed initializing helm: %v", err) + return fmt.Errorf("initializing Helm action: %w", err) } helmChart, err := c.getControlplaneChart(component) if err != nil { - contextLogger.Fatalf("Loading chart from assets failed: %v", err) + return fmt.Errorf("loading chart from assets: %w", err) } values, err := c.getControlplaneValues(component) if err != nil { - contextLogger.Fatalf("Failed to get kubernetes values.yaml from Terraform: %v", err) + return fmt.Errorf("getting chart values from Terraform: %w", err) } exists, err := util.ReleaseExists(*actionConfig, component) if err != nil { - contextLogger.Fatalf("Failed checking if controlplane component is installed: %v", err) + return fmt.Errorf("checking if controlplane component is installed: %w", err) } if !exists { @@ -230,7 +225,7 @@ func (c controlplaneUpdater) upgradeComponent(component, namespace string) { if _, err := install.Run(helmChart, values); err != nil { fmt.Println("Failed!") - contextLogger.Fatalf("Installing controlplane component failed: %v", err) + return fmt.Errorf("installing controlplane component: %w", err) } fmt.Println("Done.") @@ -245,8 +240,10 @@ func (c controlplaneUpdater) upgradeComponent(component, namespace string) { if _, err := update.Run(component, helmChart, values); err != nil { fmt.Println("Failed!") - contextLogger.Fatalf("Updating chart failed: %v", err) + return fmt.Errorf("updating controlplane component: %w", err) } fmt.Println("Done.") + + return nil }