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: allow field KV general matching #2067

Merged
merged 1 commit into from
Aug 29, 2023
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
3 changes: 3 additions & 0 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ Examples:

# if the value to be matched is a number or boolean, the value is compared as a number or boolean
[status, conditions, "[value=66]", name] # status.conditions[1].name = "b"

# For generally matching against a field in an object schema, use the following syntax:
[metadata, "name=foo"] # if v, ok := metadata[name]; ok && v == "foo" { return v; } else { /* ignore */ }
```

### Wildcard matching of version and kind fields
Expand Down
10 changes: 10 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,16 @@ func compilePath(path []string) (out valuePath, _ error) {
part: part,
op: func(m interface{}) interface{} {
if mp, ok := m.(map[string]interface{}); ok {
kv := strings.Split(part, "=")
if len(kv) == 2 /* k=v */ {
key := kv[0]
val := kv[1]
if v, ok := mp[key]; ok {
if v == val {
return v
}
}
}
return mp[part]
} else if s, ok := m.([]interface{}); ok {
i, err := strconv.Atoi(part)
Expand Down
9 changes: 9 additions & 0 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ func Test_values(t *testing.T) {
newEachValue(t, 0, "type", "Provisioned"),
newEachValue(t, 1, "type", "Ready"),
}},
{name: "= expression matching", each: &compiledInfo{
compiledCommon: compiledCommon{
labelFromPath: map[string]valuePath{
"bar": mustCompilePath(t, "metadata", "annotations", "bar=baz"),
},
},
}, wantResult: []eachValue{
newEachValue(t, 1, "bar", "baz"),
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down