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

fix: add != functionality for tags/labels in resource selector #1290

Merged
merged 1 commit into from
Jan 23, 2025
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
9 changes: 6 additions & 3 deletions query/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ var AgentMapper = func(ctx context.Context, id string) (any, error) {
return nil, fmt.Errorf("invalid agent: %s", id)
}

var JSONPathMapper = func(ctx context.Context, tx *gorm.DB, column string, path string, val string) *gorm.DB {
return tx.Where(fmt.Sprintf(`TRIM(BOTH '"' from jsonb_path_query_first(%s, '$.%s')::TEXT) = ?`, column, path), val)
var JSONPathMapper = func(ctx context.Context, tx *gorm.DB, column string, op types.QueryOperator, path string, val string) *gorm.DB {
if !slices.Contains([]types.QueryOperator{types.Eq, types.Neq}, op) {
op = types.Eq
}
return tx.Where(fmt.Sprintf(`TRIM(BOTH '"' from jsonb_path_query_first(%s, '$.%s')::TEXT) %s ?`, column, path, op), val)
}

var CommonFields = map[string]func(ctx context.Context, tx *gorm.DB, val string) (*gorm.DB, error){
Expand Down Expand Up @@ -230,7 +233,7 @@ func (qm QueryModel) Apply(ctx context.Context, q types.QueryField, tx *gorm.DB)

for _, column := range qm.JSONColumns {
if strings.HasPrefix(originalField, column) {
tx = JSONPathMapper(ctx, tx, column, strings.TrimPrefix(originalField, column+"."), val)
tx = JSONPathMapper(ctx, tx, column, q.Op, strings.TrimPrefix(originalField, column+"."), val)
q.Field = column
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/query_resource_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ var _ = ginkgo.Describe("Resoure Selector with PEG", ginkgo.Ordered, func() {
expectedIDs: []uuid.UUID{dummy.EKSCluster.ID, dummy.EC2InstanceB.ID},
resource: "config",
},
{
description: "config labels not equal query",
query: `labels.account=flanksource labels.environment!=production`,
expectedIDs: []uuid.UUID{dummy.KubernetesCluster.ID, dummy.EC2InstanceA.ID},
resource: "config",
},
{
description: "config array query",
query: `config.spec.template.spec.containers[0].name=logistics-api`,
Expand Down
Loading