Skip to content

Commit

Permalink
Allow field KV general matching
Browse files Browse the repository at this point in the history
Currently all matching support is limited to map-list representations;
this adds support for a more general "key:value" based matching
expression for any `interface{}` field (non-map-list-like).
  • Loading branch information
rexagod committed May 15, 2023
1 parent 3b95dd1 commit c805566
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,7 @@ 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, annotations, "bar:baz"] # metadata.annotations["bar:baz"] = "baz" (if present, ignored otherwise)
```
10 changes: 10 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,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 @@ -332,6 +332,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

0 comments on commit c805566

Please sign in to comment.