Skip to content

Commit

Permalink
chore: use struct templater for component resource selector
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 26, 2024
1 parent 95e622e commit a0fc44c
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions pkg/topology/component_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@ import (
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/flanksource/gomplate/v3"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/samber/lo"
"gorm.io/gorm/clause"
)

func templateSelectorSearch(comp models.Component) (types.ResourceSelectors, error) {
func templateSelector(ctx context.Context, comp *models.Component) error {
for i, rs := range comp.Selectors {
if rs.Search != "" {
output, err := gomplate.RunTemplate(
map[string]any{"self": comp.AsMap()},
gomplate.Template{Template: rs.Search, LeftDelim: "$(", RightDelim: ")"},
)
if err != nil {
return comp.Selectors, fmt.Errorf("error templating resource selector search for component[%s] search[%s]: %w", comp.ID, rs.Search, err)
t := ctx.NewStructTemplater(map[string]any{"self": comp.AsMap()}, "template", nil)
if err := t.Walk(&comp.Selectors[i]); err != nil {
return fmt.Errorf("error templating resource selector search for component[%s] search[%s]: %w", comp.ID, rs.Search, err)
}
comp.Selectors[i].Search = output
}
}
return comp.Selectors, nil
return nil
}

var ComponentRelationshipSync = &job.Job{
Expand All @@ -48,13 +42,12 @@ var ComponentRelationshipSync = &job.Job{

for _, component := range components {
hash := component.Selectors.Hash()
selectors, err := templateSelectorSearch(component)
if err != nil {
if err := templateSelector(ctx.Context, &component); err != nil {
ctx.History.AddError(err)
continue
}

comps, err := query.FindComponents(ctx.Context, -1, selectors...)
comps, err := query.FindComponents(ctx.Context, -1, component.Selectors...)
if err != nil {
ctx.History.AddError(fmt.Sprintf("error getting components with selectors: %v. err: %v", component.Selectors, err))
continue
Expand Down

0 comments on commit a0fc44c

Please sign in to comment.