Skip to content

Commit 47277f2

Browse files
Allow Action's Matches function to specify a subresource.
In other parts of the system (notably in RBAC rules), the "resource/subresource" notation is common to specify an explicit subresource. This makes this notation available to tests that use the `Matches` function on client actions as well. Backwards compatibility is kept by ignoring the `Subresource` field if no specific subresource is defined in the resource string itself.
1 parent 8971422 commit 47277f2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

staging/src/k8s.io/client-go/testing/actions.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,18 @@ func (a ActionImpl) GetSubresource() string {
439439
return a.Subresource
440440
}
441441
func (a ActionImpl) Matches(verb, resource string) bool {
442+
// Stay backwards compatible.
443+
if !strings.Contains(resource, "/") {
444+
return strings.EqualFold(verb, a.Verb) &&
445+
strings.EqualFold(resource, a.Resource.Resource)
446+
}
447+
448+
parts := strings.SplitN(resource, "/", 2)
449+
topresource, subresource := parts[0], parts[1]
450+
442451
return strings.EqualFold(verb, a.Verb) &&
443-
strings.EqualFold(resource, a.Resource.Resource)
452+
strings.EqualFold(topresource, a.Resource.Resource) &&
453+
strings.EqualFold(subresource, a.Subresource)
444454
}
445455
func (a ActionImpl) DeepCopy() Action {
446456
ret := a

0 commit comments

Comments
 (0)