Skip to content

Commit

Permalink
Merge pull request #2067 from rexagod/colon-general-matching
Browse files Browse the repository at this point in the history
feat: allow field KV general matching
  • Loading branch information
k8s-ci-robot committed Aug 29, 2023
2 parents 535085e + e52fbce commit 5ce46d5
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 @@ -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

0 comments on commit 5ce46d5

Please sign in to comment.