Skip to content

Commit

Permalink
Strictly match Group in ResourceTypeMatcher: K8s core group (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhamilton authored Apr 19, 2022
1 parent 843724b commit a5f36d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package filter

import (
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -28,9 +28,19 @@ type ResourceTypeRequirement struct {
Resource string `json:"resource,omitempty"`
}

// Matches returns true if group, version and resource values match or are empty
// Matches returns true if group, version and resource values match or are all empty
// Group must match exactly because K8s core group is ""
// Version and resource requirement values of "" match any value
func (r ResourceTypeRequirement) Matches(gvr schema.GroupVersionResource) bool {
return matches(r.Group, gvr.Group) && matches(r.Version, gvr.Version) && matches(r.Resource, gvr.Resource)
if r.Empty() {
return true
}
return r.Group == gvr.Group && matches(r.Version, gvr.Version) && matches(r.Resource, gvr.Resource)
}

// Empty returns true if ResourceTypeRequirement has no fields set
func (rtr ResourceTypeRequirement) Empty() bool {
return rtr.Group == "" && rtr.Version == "" && rtr.Resource == ""
}

func matches(sel, val string) bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"

. "gopkg.in/check.v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand Down Expand Up @@ -337,12 +337,12 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
Group: "mygroup",
Version: "yourversion",
},
},
exclude: []schema.GroupVersionResource{
{
Group: "yourgroup",
Version: "myversion",
},
},
exclude: []schema.GroupVersionResource{
{
Group: "yourgroup",
Version: "yourversion",
Expand Down

0 comments on commit a5f36d8

Please sign in to comment.