From 43c77ab3356fc2bcd0b5b9e4ac3f49085de80e93 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Tue, 24 Sep 2024 02:32:00 +0530 Subject: [PATCH] chore: select related components via config --- query/resource_selector.go | 7 +++++++ types/resource_selector.go | 18 ++++++++++++++++++ views/005_component_views.sql | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/query/resource_selector.go b/query/resource_selector.go index fc7cba0d..9e234c10 100644 --- a/query/resource_selector.go +++ b/query/resource_selector.go @@ -224,6 +224,13 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou } } + if resourceSelector.Functions.ComponentConfigTraversal != nil { + args := resourceSelector.Functions.ComponentConfigTraversal + if table == "components" { + query = query.Where("id IN (SELECT id from lookup_component_config_id_related_components(?, ?, ?))", args.ComponentID, args.Direction, args.Types) + } + } + if resourceSelector.Search != "" { var prefixQueries []*gorm.DB if resourceSelector.Name == "" { diff --git a/types/resource_selector.go b/types/resource_selector.go index 1e99938f..23a556a1 100644 --- a/types/resource_selector.go +++ b/types/resource_selector.go @@ -9,6 +9,7 @@ import ( "github.com/flanksource/commons/collections" "github.com/flanksource/commons/hash" "github.com/flanksource/commons/logger" + "github.com/lib/pq" "gorm.io/gorm" "gorm.io/gorm/clause" "gorm.io/gorm/schema" @@ -16,6 +17,20 @@ 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 + ComponentConfigTraversal *ComponentConfigTraversalArgs `yaml:"component_config_traversal,omitempty" json:"component_config_traversal,omitempty"` +} + // +kubebuilder:object:generate=true type ResourceSelector struct { // Agent can be the agent id or the name of the agent. @@ -37,6 +52,9 @@ type ResourceSelector struct { // Search query that applies to the resource name, tag & labels. Search string `yaml:"search,omitempty" json:"search,omitempty"` + // Use custom functions for specific selections + Functions Functions `yaml:"function,omitempty" json:"function,omitempty"` + IncludeDeleted bool `yaml:"includeDeleted,omitempty" json:"includeDeleted,omitempty"` ID string `yaml:"id,omitempty" json:"id,omitempty"` diff --git a/views/005_component_views.sql b/views/005_component_views.sql index 822b3447..9065ad58 100644 --- a/views/005_component_views.sql +++ b/views/005_component_views.sql @@ -145,3 +145,27 @@ $$ language plpgsql; CREATE OR REPLACE VIEW component_types AS SELECT distinct on (type) type FROM components ORDER BY type asc; + +CREATE OR REPLACE FUNCTION lookup_component_config_id_related_components ( + component_id TEXT, + type_filter TEXT DEFAULT 'outgoing', + component_types TEXT[] DEFAULT '{}' +) +RETURNS TABLE (id UUID) AS $$ +BEGIN + RETURN QUERY + WITH related_config_ids AS ( + SELECT * + FROM related_configs_recursive( + (SELECT config_id FROM components WHERE components.id = $1::UUID), + $2, + FALSE, + 10 + ) + ) + SELECT components.id + FROM components + WHERE config_id IN (SELECT related_config_ids.id FROM related_config_ids) + AND type = ANY($3); +END; +$$ LANGUAGE plpgsql;