Skip to content
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
16 changes: 9 additions & 7 deletions docs/pages/reference/predicate-language.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ The language supports the following operators:

The language also supports the following functions:

| Functions (with examples) | Description |
|---------------------------------------|------------------------------------------------------------|
| `equals(labels["env"], "prod")` | resources with label key `env` equal to label value `prod` |
| `exists(labels["env"])` | resources with a label key `env`; label value unchecked |
| `!exists(labels["env"])` | resources without a label key `env`; label value unchecked |
| `search("foo", "bar", "some phrase")` | fuzzy match against common resource fields |
| `hasPrefix(name, "foo")` | resources with a name that starts with the prefix `foo` |
| Functions (with examples) | Description |
|----------------------------------------------|------------------------------------------------------------|
| `equals(labels["env"], "prod")` | resources with label key `env` equal to label value `prod` |
| `exists(labels["env"])` | resources with a label key `env`; label value unchecked |
| `!exists(labels["env"])` | resources without a label key `env`; label value unchecked |
| `search("foo", "bar", "some phrase")` | fuzzy match against common resource fields |
| `hasPrefix(name, "foo")` | resources with a name that starts with the prefix `foo` |
| `split(labels["foo"], ",")` | converts a delimited string into a list |
| `contains(split(labels["foo"], ","), "bar")` | determines if a value exists in a list |

See some [examples](cli.mdx#filter-examples) of the different ways you can filter resources.

Expand Down
7 changes: 7 additions & 0 deletions lib/services/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package services
import (
"fmt"
"io"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -779,6 +780,12 @@ func NewResourceExpression(expression string) (typical.Expression[types.Resource
"exists": typical.UnaryFunction[types.ResourceWithLabels](func(value string) (bool, error) {
return value != "", nil
}),
"split": typical.BinaryFunction[types.ResourceWithLabels](func(value string, delimiter string) ([]string, error) {
return strings.Split(value, delimiter), nil
}),
"contains": typical.BinaryFunction[types.ResourceWithLabels](func(list []string, value string) (bool, error) {
return slices.Contains(list, value), nil
}),
},
GetUnknownIdentifier: func(env types.ResourceWithLabels, fields []string) (any, error) {
if fields[0] == ResourceIdentifier {
Expand Down
69 changes: 69 additions & 0 deletions lib/services/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,75 @@ func TestResourceExpression_NameIdentifier(t *testing.T) {
require.True(t, match)
}

func TestResourceParserLabelExpansion(t *testing.T) {
t.Parallel()

// Server resource should use hostname when using name identifier.
server, err := types.NewServerWithLabels("server-name", types.KindNode, types.ServerSpecV2{
Hostname: "server-hostname",
}, map[string]string{"ip": "1.2.3.11,1.2.3.101,1.2.3.1", "foo": "bar"})
require.NoError(t, err)

tests := []struct {
expression string
assertion require.BoolAssertionFunc
}{
{
expression: `contains(split(labels["ip"], ","), "1.2.3.1")`,
assertion: require.True,
},
{
expression: `contains(split(labels.ip, ","), "1.2.3.1",)`,
assertion: require.True,
},
{
expression: `contains(split(labels["ip"], ","), "1.2.3.2")`,
assertion: require.False,
},
{
expression: `contains(split(labels.llama, ","), "1.2.3.2")`,
assertion: require.False,
},
{
expression: `contains(split(labels.ip, ","), "1.2.3.2")`,
assertion: require.False,
},
{
expression: `contains(split(labels.foo, ","), "bar")`,
assertion: require.True,
},
}

for _, test := range tests {
t.Run(test.expression, func(t *testing.T) {
expression, err := NewResourceExpression(test.expression)
require.NoError(t, err)

match, err := expression.Evaluate(server)
require.NoError(t, err)
test.assertion(t, match)
})
}
}

func BenchmarkContains(b *testing.B) {
server, err := types.NewServerWithLabels("server-name", types.KindNode, types.ServerSpecV2{
Hostname: "server-hostname",
}, map[string]string{"ip": "1.2.3.11|1.2.3.101|1.2.3.1"})
require.NoError(b, err)

expression, err := NewResourceExpression(`contains(split(labels["ip"], "|"), "1.2.3.1")`)
require.NoError(b, err)

b.ResetTimer()

for i := 0; i < b.N; i++ {
match, err := expression.Evaluate(server)
require.NoError(b, err)
require.True(b, match)
}
}

// TestParserHostCertContext tests set functions with a custom host cert
// context.
func TestParserHostCertContext(t *testing.T) {
Expand Down