Skip to content

Commit 1911d01

Browse files
yashmehrotramoshloop
authored andcommitted
chore: use struct templater for component resource selector
1 parent 2724864 commit 1911d01

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

pkg/topology/component_relationship.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,22 @@ import (
88
"github.com/flanksource/duty/job"
99
"github.com/flanksource/duty/models"
1010
"github.com/flanksource/duty/query"
11-
"github.com/flanksource/duty/types"
12-
"github.com/flanksource/gomplate/v3"
1311
"github.com/google/uuid"
1412
"github.com/pkg/errors"
1513
"github.com/samber/lo"
1614
"gorm.io/gorm/clause"
1715
)
1816

19-
func templateSelectorSearch(comp models.Component) (types.ResourceSelectors, error) {
17+
func templateSelector(ctx context.Context, comp *models.Component) error {
2018
for i, rs := range comp.Selectors {
2119
if rs.Search != "" {
22-
output, err := gomplate.RunTemplate(
23-
map[string]any{"self": comp.AsMap()},
24-
gomplate.Template{Template: rs.Search, LeftDelim: "$(", RightDelim: ")"},
25-
)
26-
if err != nil {
27-
return comp.Selectors, fmt.Errorf("error templating resource selector search for component[%s] search[%s]: %w", comp.ID, rs.Search, err)
20+
t := ctx.NewStructTemplater(map[string]any{"self": comp.AsMap()}, "template", nil)
21+
if err := t.Walk(&comp.Selectors[i]); err != nil {
22+
return fmt.Errorf("error templating resource selector search for component[%s] search[%s]: %w", comp.ID, rs.Search, err)
2823
}
29-
comp.Selectors[i].Search = output
3024
}
3125
}
32-
return comp.Selectors, nil
26+
return nil
3327
}
3428

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

4943
for _, component := range components {
5044
hash := component.Selectors.Hash()
51-
selectors, err := templateSelectorSearch(component)
52-
if err != nil {
45+
if err := templateSelector(ctx.Context, &component); err != nil {
5346
ctx.History.AddError(err)
5447
continue
5548
}
5649

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

0 commit comments

Comments
 (0)