From f629d4b607fd1e3115b6fe078deacadc70bc0836 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Tue, 24 Sep 2024 17:29:37 +0530 Subject: [PATCH] chore: make component config traverse part of search --- query/resource_selector.go | 47 ++++++++++++++++++++++++++------------ types/resource_selector.go | 4 +--- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/query/resource_selector.go b/query/resource_selector.go index 9e234c10..211f9862 100644 --- a/query/resource_selector.go +++ b/query/resource_selector.go @@ -8,6 +8,7 @@ import ( "github.com/flanksource/commons/collections" "github.com/flanksource/commons/duration" "github.com/flanksource/commons/logger" + "github.com/lib/pq" "github.com/google/uuid" "github.com/patrickmn/go-cache" @@ -224,6 +225,37 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou } } + if resourceSelector.Search != "" { + if strings.HasPrefix(resourceSelector.Search, "component_config_traverse") && table == "components" { + // search: component_config_traverse=72143d48-da4a-477f-bac1-1e9decf188a6,outgoing,Kubernetes::Pod,Kubernetes::Node + items := strings.Split(resourceSelector.Search, "=") + if len(items) == 2 { + // Args should be componentID, direction and types (compID,direction,type1,type2,type3) + args := strings.SplitN(items[1], ",", 3) + if len(args) == 3 { + resourceSelector.Functions.ComponentConfigTraversal = &types.ComponentConfigTraversalArgs{ + ComponentID: args[0], + Direction: args[1], + Types: pq.StringArray(strings.Split(args[2], ",")), + } + } + } + } else { + var prefixQueries []*gorm.DB + if resourceSelector.Name == "" { + prefixQueries = append(prefixQueries, ctx.DB().Where("name ILIKE ?", resourceSelector.Search+"%")) + } + if resourceSelector.TagSelector == "" && table == "config_items" { + prefixQueries = append(prefixQueries, ctx.DB().Where("EXISTS (SELECT 1 FROM jsonb_each_text(tags) WHERE value ILIKE ?)", resourceSelector.Search+"%")) + } + if resourceSelector.LabelSelector == "" { + prefixQueries = append(prefixQueries, ctx.DB().Where("EXISTS (SELECT 1 FROM jsonb_each_text(labels) WHERE value ILIKE ?)", resourceSelector.Search+"%")) + } + + query = OrQueries(query, prefixQueries...) + } + } + if resourceSelector.Functions.ComponentConfigTraversal != nil { args := resourceSelector.Functions.ComponentConfigTraversal if table == "components" { @@ -231,21 +263,6 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou } } - if resourceSelector.Search != "" { - var prefixQueries []*gorm.DB - if resourceSelector.Name == "" { - prefixQueries = append(prefixQueries, ctx.DB().Where("name ILIKE ?", resourceSelector.Search+"%")) - } - if resourceSelector.TagSelector == "" && table == "config_items" { - prefixQueries = append(prefixQueries, ctx.DB().Where("EXISTS (SELECT 1 FROM jsonb_each_text(tags) WHERE value ILIKE ?)", resourceSelector.Search+"%")) - } - if resourceSelector.LabelSelector == "" { - prefixQueries = append(prefixQueries, ctx.DB().Where("EXISTS (SELECT 1 FROM jsonb_each_text(labels) WHERE value ILIKE ?)", resourceSelector.Search+"%")) - } - - query = OrQueries(query, prefixQueries...) - } - return query, nil } diff --git a/types/resource_selector.go b/types/resource_selector.go index 23a556a1..657f53dc 100644 --- a/types/resource_selector.go +++ b/types/resource_selector.go @@ -17,14 +17,12 @@ import ( "k8s.io/apimachinery/pkg/labels" ) -// +kubebuilder:object:generate=true type ComponentConfigTraversalArgs struct { ComponentID string `yaml:"component_id,omitempty" json:"component_id,omitempty"` Direction string `yaml:"direction,omitempty" json:"direction,omitempty"` Types pq.StringArray `yaml:"types,omitempty" json:"types,omitempty"` } -// +kubebuilder:object:generate=true type Functions struct { // It uses the config_id linked to the componentID to lookup up all the config relations and returns // a list of componentIDs that are linked to the found configIDs @@ -53,7 +51,7 @@ type ResourceSelector struct { Search string `yaml:"search,omitempty" json:"search,omitempty"` // Use custom functions for specific selections - Functions Functions `yaml:"function,omitempty" json:"function,omitempty"` + Functions Functions `yaml:"-" json:"-"` IncludeDeleted bool `yaml:"includeDeleted,omitempty" json:"includeDeleted,omitempty"`