Skip to content

Commit

Permalink
🐛 respect match-all label values (#499) (#502)
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
Signed-off-by: Cherry Picker <[email protected]>

Signed-off-by: Pranav Gaikwad <[email protected]>
Signed-off-by: Cherry Picker <[email protected]>
Co-authored-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
github-actions[bot] and pranavgaikwad authored Feb 12, 2024
1 parent 4fc199d commit 734575f
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 36 deletions.
178 changes: 143 additions & 35 deletions docs/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,149 @@ The analyzer defines some labels that have special meanings:

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.

To filter-in all rules that have a label with key `konveyor.io/source` and value `eap6`:

```sh
--label-selector="konveyor.io/source=eap6"
```

To filter-in all rules that have a label with key `konveyor.io/source` and any value:

```sh
--label-selector="konveyor.io/source"
```

To perform a logical AND on matches of multiple rules using `&&` operator:

```sh
--label-selector="key1=val1 && key2"
```

To perform a logical OR on matches of multiple rules using `||` operator:

```sh
--label-selector="key1=val1 || key2"
```

To perform a NOT to filter-out rules that have `key1=val1` label set using `!` operator:

```sh
--label-selector="!key1=val1"
```

To group sub-expressions and control precedence using `(` and `)`:

```sh
--label-selector="(key1=val1 || key2=val2) && !val3"
```
Here are different scenarios of how rules will be filtered in or out based on a label selector expression:

* _Exact label value match_

* To filter-in all rules that have a label with key `konveyor.io/source` and value `kubernetes`:

```sh
--label-selector="konveyor.io/source=kubernetes"
```
The value `kubernetes` must be an exact string match in this case.

With the above label selector, only the rule `rule-000` will be matched from the following input rules:

```yaml
- ruleID: rule-000
labels:
- konveyor.io/source=kubernetes
[...]
- ruleID: rule-001
labels:
- konveyor.io/source=openshift
[...]
```

* _Any label value match_

* To filter-in all rules that have a label with key `konveyor.io/source` and _any_ value:

```sh
--label-selector="konveyor.io/source"
```

Only the key `konveyor.io/source` should be present in the rule labels no matter what value.

Both the rules `rule-000` and `rule-001` in the following input rules with the above label selector:

```yaml
- ruleID: rule-000
labels:
- konveyor.io/source=kubernetes
[...]
- ruleID: rule-001
labels:
- konveyor.io/source=openshift
[...]
```

* Some rules themselves have labels with only keys and no values. For instance:

```yaml
- ruleID: rule-000
labels:
- konveyor.io/source
[...]
```
Such rules will match on any value of the `konveyor.io/source` label provided in the label selector expression.

For instance, the rule `rule-000` above will match when the input expression is as follows:

```sh
--label-selector konveyor.io/source=kubernetes
```

* _Logical AND between multiple labels_

* To perform a logical AND between results of multiple label matches using `&&` operator:

```sh
--label-selector="konveyor.io/target=kubernetes && component=storage"
```

Only the rule `rule-001` from the following rules will match with the above label selector:

```yaml
- ruleID: rule-001
labels:
- konveyor.io/target=kubernetes
- component=storage
[...]
- ruleID: rule-002
labels:
- konveyor.io/target=kubernetes
[...]
```

* _Logical OR between multiple labels_

* To perform a logical OR between results of multiple label matches using `||` operator:

```sh
--label-selector="konveyor.io/target=kubernetes || konveyor.io/target=openshift"
```

Both the rules `rule-001` and `rule-002` will be matched with the above label selector:

```yaml
- ruleID: rule-001
labels:
- konveyor.io/source=kubernetes
[...]
- ruleID: rule-002
labels:
- konveyor.io/source=openshift
[...]
```


* _Logical NOT to filter-out a rule_

* Label selector can also be used to exclude rules using the `!` operator:

```sh
--label-selector="!component=network && konveyor.io/target=kubernetes"
```

From the following rules, the rules `rule-001` and `rule-003` will be matched with the above label selector:

```yaml
- ruleID: rule-001
labels:
- konveyor.io/target=kubernetes
- component=storage
[...]
- ruleID: rule-002
labels:
- konveyor.io/target=kubernetes
- component=network
[...]
- ruleID: rule-003
labels:
- konveyor.io/target=kubernetes
- component=compute
[...]
```


* _Grouping subexpressions_

* To group sub-expressions and control precedence using `(` and `)`:

```sh
--label-selector="(key1=val1 || key2=val2) && !val3"
```

## Dependency Labels

Expand Down
2 changes: 1 addition & 1 deletion engine/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func tokenize(expr string) []string {

func matchesAny(elem string, items []string) bool {
for _, item := range items {
if labelValueMatches(item, elem) {
if item == "" || labelValueMatches(item, elem) {
return true
}
}
Expand Down
27 changes: 27 additions & 0 deletions engine/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,33 @@ func Test_ruleSelector_Matches(t *testing.T) {
},
want: false,
},
{
name: "rule has a match-all source label",
expr: "konveyor.io/target=t1 && konveyor.io/source=t2",
ruleLabels: []string{
"konveyor.io/target=t1",
"konveyor.io/source",
},
want: true,
},
{
name: "rule has a specific source label",
expr: "konveyor.io/target=t1 && konveyor.io/source=t2",
ruleLabels: []string{
"konveyor.io/target=t1",
"konveyor.io/source=t3",
},
want: false,
},
{
name: "query contains match-all label",
expr: "konveyor.io/target=t1 && konveyor.io/source",
ruleLabels: []string{
"konveyor.io/target=t1",
"konveyor.io/source=t3",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 734575f

Please sign in to comment.