Skip to content

Commit

Permalink
chore: select related components via config
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 24, 2024
1 parent 1102de7 commit 43c77ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
18 changes: 18 additions & 0 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@ 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"
"k8s.io/apimachinery/pkg/fields"
"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.
Expand All @@ -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"`
Expand Down
24 changes: 24 additions & 0 deletions views/005_component_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 43c77ab

Please sign in to comment.