Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support grammar in resource selector .Matches #1312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c Check) GetLabelsMatcher() labels.Labels {
}

func (c Check) GetFieldsMatcher() fields.Fields {
return noopMatcher{}
return genericFieldMatcher{c.AsMap()}
}

type checkLabelsProvider struct {
Expand Down
10 changes: 8 additions & 2 deletions models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"encoding/json"
"fmt"

"github.com/flanksource/commons/logger"
"github.com/google/uuid"
Expand Down Expand Up @@ -149,7 +148,14 @@ type genericFieldMatcher struct {
}

func (c genericFieldMatcher) Get(key string) string {
return fmt.Sprintf("%v", c.Fields[key])
val := c.Fields[key]
switch v := val.(type) {
case string:
return v
default:
marshalled, _ := json.Marshal(v)
return string(marshalled)
}
}

func (c genericFieldMatcher) Has(key string) bool {
Expand Down
37 changes: 1 addition & 36 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

var AllowedColumnFieldsInComponents = []string{
"owner",
"topology_type",
"topology_id",
"parent_id",
"type", // Deprecated. Use resource_selector.types instead
}

// Ensure interface compliance
var (
_ types.ResourceSelectable = Component{}
Expand Down Expand Up @@ -340,7 +332,7 @@ func (c Component) GetLabelsMatcher() labels.Labels {
}

func (c Component) GetFieldsMatcher() fields.Fields {
return componentFieldsProvider{c}
return genericFieldMatcher{c.AsMap()}
}

type componentLabelsProvider struct {
Expand All @@ -356,33 +348,6 @@ func (c componentLabelsProvider) Has(key string) bool {
return ok
}

type componentFieldsProvider struct {
Component
}

func (c componentFieldsProvider) Get(key string) string {
if lo.Contains(AllowedColumnFieldsInComponents, key) {
return fmt.Sprintf("%v", c.AsMap()[key])
}

v := c.Properties.Find(key)
if v == nil {
return ""
}

return fmt.Sprintf("%v", v.GetValue())
}

func (c componentFieldsProvider) Has(key string) bool {
if lo.Contains(AllowedColumnFieldsInComponents, key) {
_, ok := c.AsMap()[key]
return ok
}

v := c.Properties.Find(key)
return v != nil
}

var ComponentID = func(c Component) string {
return c.ID.String()
}
Expand Down
31 changes: 1 addition & 30 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ const (
AnalysisTypeTechDebt AnalysisType = "technical_debt"
)

var AllowedColumnFieldsInConfigs = []string{"config_class", "external_id"}

type RelatedConfigDirection string

const (
Expand Down Expand Up @@ -277,34 +275,7 @@ func (c ConfigItem) GetLabelsMatcher() labels.Labels {
}

func (c ConfigItem) GetFieldsMatcher() fields.Fields {
return configFields{c}
}

type configFields struct {
ConfigItem
}

func (c configFields) Get(key string) string {
if lo.Contains(AllowedColumnFieldsInConfigs, key) {
return fmt.Sprintf("%v", c.AsMap()[key])
}

v := c.Properties.Find(key)
if v == nil {
return ""
}

return fmt.Sprintf("%v", v.GetValue())
}

func (c configFields) Has(key string) bool {
if lo.Contains(AllowedColumnFieldsInConfigs, key) {
_, ok := c.AsMap()[key]
return ok
}

v := c.Properties.Find(key)
return v != nil
return genericFieldMatcher{c.AsMap()}
}

type configLabels struct {
Expand Down
36 changes: 3 additions & 33 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,45 +140,15 @@ func SetResourceSelectorClause(
) (*gorm.DB, error) {
searchSetAgent := false

var searchConditions []string
if resourceSelector.Name != "" {
searchConditions = append(searchConditions, fmt.Sprintf("name = %q", resourceSelector.Name))
}

if resourceSelector.ID != "" {
searchConditions = append(searchConditions, fmt.Sprintf("id = %q", resourceSelector.ID))
}

if len(resourceSelector.Health) != 0 {
searchConditions = append(searchConditions, fmt.Sprintf("health = %q", resourceSelector.Health))
}

if resourceSelector.Namespace != "" {
searchConditions = append(searchConditions, fmt.Sprintf("namespace = %q", resourceSelector.Namespace))
}

if len(resourceSelector.Types) > 0 {
searchConditions = append(searchConditions, fmt.Sprintf("type = %q", strings.Join(resourceSelector.Types, ",")))
}

if len(resourceSelector.Statuses) > 0 {
searchConditions = append(searchConditions, fmt.Sprintf("status = %q", strings.Join(resourceSelector.Statuses, ",")))
}

if len(searchConditions) > 0 {
joined := strings.Join(searchConditions, " ")
resourceSelector.Search += fmt.Sprintf(" %s", joined)
}

qm, err := GetModelFromTable(table)
if err != nil {
return nil, fmt.Errorf("grammar parsing not implemented for table: %s", table)
}

if resourceSelector.Search != "" {
qf, err := grammar.ParsePEG(resourceSelector.Search)
if peg := resourceSelector.ToPeg(false); peg != "" {
qf, err := grammar.ParsePEG(peg)
if err != nil {
return nil, fmt.Errorf("error parsing grammar[%s]: %w", resourceSelector.Search, err)
return nil, fmt.Errorf("error parsing grammar[%s]: %w", peg, err)
}

flatFields := grammar.FlatFields(qf)
Expand Down
4 changes: 4 additions & 0 deletions tests/query_resource_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ var _ = ginkgo.Describe("SearchResourceSelectors", func() {
})

for _, test := range testData {
// if test.description != "labels | IN Query" {
// continue
// }

ginkgo.It(test.description, func() {
items, err := query.SearchResources(DefaultContext, test.query)
Expect(err).To(BeNil())
Expand Down
Loading
Loading