From c02067c93ba55a078529548777702dcb0913ba94 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 25 Sep 2020 13:53:09 +0200 Subject: [PATCH] cli/cmd: make applyComponents() accept slice of objects Instead of slice of names. This shifs the error handling of getting components to earlier stage, so we don't apply one component and then fail, because other one does not exist. Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster-apply.go | 12 +++++------- cli/cmd/component-apply.go | 17 ++++++++--------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index 2b0b9bcaf..e460396f7 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -145,17 +145,15 @@ func clusterApply(contextLogger *log.Entry) error { return nil } - componentsToApply := []string{} - for _, component := range c.lokomotiveConfig.RootConfig.Components { - componentsToApply = append(componentsToApply, component.Name) + componentObjects, err := componentNamesToObjects(componentNames(nil, *c.lokomotiveConfig)) + if err != nil { + return fmt.Errorf("getting component objects: %w", err) } contextLogger.Println("Applying component configuration") - if len(componentsToApply) > 0 { - if err := applyComponents(c.lokomotiveConfig, kubeconfig, componentsToApply...); err != nil { - return fmt.Errorf("applying component configuration: %v", err) - } + if err := applyComponents(c.lokomotiveConfig, kubeconfig, componentObjects); err != nil { + return fmt.Errorf("applying component configuration: %v", err) } return nil diff --git a/cli/cmd/component-apply.go b/cli/cmd/component-apply.go index 3e051ca47..8e573e1fe 100644 --- a/cli/cmd/component-apply.go +++ b/cli/cmd/component-apply.go @@ -72,7 +72,10 @@ func componentApply(contextLogger *log.Entry, componentsList []string) error { return diags } - componentsToApply := componentNames(componentsList, *lokoConfig) + componentObjects, err := componentNamesToObjects(componentNames(componentsList, *lokoConfig)) + if err != nil { + return fmt.Errorf("getting component objects: %w", err) + } kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false) if err != nil { @@ -81,22 +84,18 @@ func componentApply(contextLogger *log.Entry, componentsList []string) error { return fmt.Errorf("suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?") } - if err := applyComponents(lokoConfig, kubeconfig, componentsToApply); err != nil { + if err := applyComponents(lokoConfig, kubeconfig, componentObjects); err != nil { return fmt.Errorf("applying components: %w", err) } return nil } -func applyComponents(lokoConfig *config.Config, kubeconfig []byte, componentNames []string) error { - for _, componentName := range componentNames { +func applyComponents(lokoConfig *config.Config, kubeconfig []byte, componentObjects []components.Component) error { + for _, component := range componentObjects { + componentName := component.Metadata().Name fmt.Printf("Applying component '%s'...\n", componentName) - component, err := components.Get(componentName) - if err != nil { - return fmt.Errorf("getting component %q: %w", componentName, err) - } - componentConfigBody := lokoConfig.LoadComponentConfigBody(componentName) if diags := component.LoadConfig(componentConfigBody, lokoConfig.EvalContext); diags.HasErrors() {