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 applyComponents() accept slice of objects
Browse files Browse the repository at this point in the history
Instead of slice of names. This shifts 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 <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 14, 2020
1 parent df9e43b commit 184d2ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
12 changes: 5 additions & 7 deletions cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions cli/cmd/component-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,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 {
Expand All @@ -83,22 +86,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() {
Expand Down

0 comments on commit 184d2ab

Please sign in to comment.