Skip to content

Commit 5ce46d5

Browse files
authored
Merge pull request #2067 from rexagod/colon-general-matching
feat: allow field KV general matching
2 parents 535085e + e52fbce commit 5ce46d5

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

docs/customresourcestate-metrics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ Examples:
522522
523523
# if the value to be matched is a number or boolean, the value is compared as a number or boolean
524524
[status, conditions, "[value=66]", name] # status.conditions[1].name = "b"
525+
526+
# For generally matching against a field in an object schema, use the following syntax:
527+
[metadata, "name=foo"] # if v, ok := metadata[name]; ok && v == "foo" { return v; } else { /* ignore */ }
525528
```
526529

527530
### Wildcard matching of version and kind fields

pkg/customresourcestate/registry_factory.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,16 @@ func compilePath(path []string) (out valuePath, _ error) {
623623
part: part,
624624
op: func(m interface{}) interface{} {
625625
if mp, ok := m.(map[string]interface{}); ok {
626+
kv := strings.Split(part, "=")
627+
if len(kv) == 2 /* k=v */ {
628+
key := kv[0]
629+
val := kv[1]
630+
if v, ok := mp[key]; ok {
631+
if v == val {
632+
return v
633+
}
634+
}
635+
}
626636
return mp[part]
627637
} else if s, ok := m.([]interface{}); ok {
628638
i, err := strconv.Atoi(part)

pkg/customresourcestate/registry_factory_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ func Test_values(t *testing.T) {
349349
newEachValue(t, 0, "type", "Provisioned"),
350350
newEachValue(t, 1, "type", "Ready"),
351351
}},
352+
{name: "= expression matching", each: &compiledInfo{
353+
compiledCommon: compiledCommon{
354+
labelFromPath: map[string]valuePath{
355+
"bar": mustCompilePath(t, "metadata", "annotations", "bar=baz"),
356+
},
357+
},
358+
}, wantResult: []eachValue{
359+
newEachValue(t, 1, "bar", "baz"),
360+
}},
352361
}
353362
for _, tt := range tests {
354363
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)