Skip to content

Commit 2d5fe12

Browse files
yashmehrotramoshloop
authored andcommitted
chore: template resource selector search
1 parent bbaf1e2 commit 2d5fe12

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pkg/topology/component_relationship.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ import (
99
"github.com/flanksource/duty/models"
1010
"github.com/flanksource/duty/query"
1111
"github.com/flanksource/duty/types"
12+
"github.com/flanksource/gomplate/v3"
1213
"github.com/google/uuid"
1314
"github.com/pkg/errors"
1415
"github.com/samber/lo"
1516
"gorm.io/gorm/clause"
1617
)
1718

18-
func injectComponentDataInSelectors(comp models.Component) types.ResourceSelectors {
19+
func templateSelectorSearch(comp models.Component) (types.ResourceSelectors, error) {
1920
for i, rs := range comp.Selectors {
20-
if rs.Functions.ComponentConfigTraversal != nil {
21-
comp.Selectors[i].Functions.ComponentConfigTraversal.ComponentID = comp.ID.String()
21+
if rs.Search != "" {
22+
output, err := gomplate.RunTemplate(map[string]any{"self": comp.AsMap()}, gomplate.Template{Template: rs.Search})
23+
if err != nil {
24+
return comp.Selectors, fmt.Errorf("error templating resource selector search for component[%s] search[%s]: %w", comp.ID, rs.Search, err)
25+
}
26+
comp.Selectors[i].Search = output
2227
}
2328
}
24-
return comp.Selectors
29+
return comp.Selectors, nil
2530
}
2631

2732
var ComponentRelationshipSync = &job.Job{
@@ -40,7 +45,12 @@ var ComponentRelationshipSync = &job.Job{
4045

4146
for _, component := range components {
4247
hash := component.Selectors.Hash()
43-
selectors := injectComponentDataInSelectors(component)
48+
selectors, err := templateSelectorSearch(component)
49+
if err != nil {
50+
ctx.History.AddError(err)
51+
continue
52+
}
53+
4454
comps, err := query.FindComponents(ctx.Context, -1, selectors...)
4555
if err != nil {
4656
ctx.History.AddError(fmt.Sprintf("error getting components with selectors: %v. err: %v", component.Selectors, err))

0 commit comments

Comments
 (0)