Skip to content

Commit

Permalink
chore: make component config traverse part of search
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 24, 2024
1 parent 43c77ab commit f629d4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
47 changes: 32 additions & 15 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -224,28 +225,44 @@ 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" {
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 == "" {
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
}

Expand Down
4 changes: 1 addition & 3 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`

Expand Down

0 comments on commit f629d4b

Please sign in to comment.