Skip to content

Commit

Permalink
✨ add short-circuit filter label
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Sep 29, 2023
1 parent c5852d5 commit e40801f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
11 changes: 6 additions & 5 deletions docs/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ labels:
- "konveyor.io/key"
```
## Reserved Labels
## Rule Labels
The analyzer defines some labels that have special meaning. Here is a list of all such labels:
The analyzer defines some labels that have special meanings:
- `konveyor.io/source`: Identifies source technology a rule or a ruleset applies to.
- `konveyor.io/target`: Identifies target technology a rule or a ruleset applies to.
- `konveyor.io/source`: Identifies source technology a rule or a ruleset applies to. The value can be a string with optional version range at the end e.g. "eap", "eap6", "eap7-" etc.
- `konveyor.io/target`: Identifies target technology a rule or a ruleset applies to. The value can be a string with optional version range at the end e.g. "eap", "eap6", "eap8+" etc.
- `konveyor.io/include`: Overrides filter behavior for a rule irrespective of the label selector used. The value can either be `always` or `never`. `always` will always filter-in this rule, `never` will always filter-out this rule.

## Label Selector
### Rule Label Selector

The analyzer CLI takes `--label-selector` as an option. It is a string expression that supports logical AND, OR and NOT operations. It can be used to filter-in/filter-out rules based on labels.

Expand Down
16 changes: 16 additions & 0 deletions engine/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"github.com/hashicorp/go-version"
)

const (
// a selector label takes precedance over any other label when matching
RuleIncludeLabel = "konveyor.io/include"
SelectAlways = "always"
SelectNever = "never"
)

const (
LabelValueFmt = "^[a-zA-Z0-9]([-a-zA-Z0-9. ]*[a-zA-Z0-9+-])?$"
LabelPrefixFmt = "^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$"
Expand All @@ -33,6 +40,15 @@ func AsString(key, value string) string {

func (l *LabelSelector[T]) Matches(v T) (bool, error) {
ruleLabels, _ := ParseLabels(v.GetLabels())
if val, ok := ruleLabels[RuleIncludeLabel]; ok &&
val != nil && len(val) > 0 {
switch val[0] {
case SelectAlways:
return true, nil
case SelectNever:
return false, nil
}
}
expr := getBooleanExpression(l.expr, ruleLabels)
val, err := l.language.Evaluate(expr, nil)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions engine/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,24 @@ func Test_ruleSelector_Matches(t *testing.T) {
},
want: true,
},
{
name: "rule has a explicit include=always label",
expr: "konveyor.io/source=test",
ruleLabels: []string{
"konveyor.io/include=always",
"konveyor.io/source=noTest", // this should make the selector not match, but 'always' selector takes precedance
},
want: true,
},
{
name: "rule has a explicit include=never label",
expr: "konveyor.io/source=test",
ruleLabels: []string{
"konveyor.io/include=never",
"konveyor.io/source=test", // this should make the selector match, but 'never' selector takes precedance
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit e40801f

Please sign in to comment.