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 upgradeComponent() 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.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 7, 2020
1 parent 736bbd4 commit cccfa49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
21 changes: 9 additions & 12 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.")
Expand All @@ -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
}

0 comments on commit cccfa49

Please sign in to comment.