Skip to content

Commit

Permalink
Compatible ResourceTypeMatcher with exact match for core (#1394)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ewhamilton and mergify[bot] committed Apr 21, 2022
1 parent 9e28eb2 commit 58d437a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
17 changes: 11 additions & 6 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ type ResourceTypeRequirement struct {
Resource string `json:"resource,omitempty"`
}

// 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
// K8sCoreGroupExactMatch is sentinel value that only matches K8s core group of ""
const K8sCoreGroupExactMatch = "core"

// Matches returns true if group, version and resource values match or are empty
// Group value of K8sCoreGroupExactMatch only matches K8s core group of ""
func (r ResourceTypeRequirement) Matches(gvr schema.GroupVersionResource) bool {
if r.Empty() {
return true
var groupMatch bool
if r.Group == K8sCoreGroupExactMatch {
groupMatch = gvr.Group == ""
} else {
groupMatch = matches(r.Group, gvr.Group)
}
return r.Group == gvr.Group && matches(r.Version, gvr.Version) && matches(r.Resource, gvr.Resource)
return groupMatch && matches(r.Version, gvr.Version) && matches(r.Resource, gvr.Resource)
}

// Empty returns true if ResourceTypeRequirement has no fields set
Expand Down
66 changes: 64 additions & 2 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,80 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
Group: "mygroup",
Version: "yourversion",
},
},
exclude: []schema.GroupVersionResource{
{
Group: "yourgroup",
Version: "myversion",
},
},
exclude: []schema.GroupVersionResource{
{
Group: "yourgroup",
Version: "yourversion",
},
},
},
{
m: ResourceTypeMatcher{
ResourceTypeRequirement{
Group: "core",
Resource: "myresource",
},
},
gvrs: []schema.GroupVersionResource{
{
Group: "",
Resource: "myresource",
},
{
Group: "core",
Resource: "myresource",
},
{
Group: "mygroup",
Resource: "myresource",
},
{
Group: "",
Resource: "yourresource",
},
{
Group: "core",
Resource: "yourresource",
},
{
Group: "mygroup",
Resource: "yourresource",
},
},
include: []schema.GroupVersionResource{
{
Group: "",
Resource: "myresource",
},
},
exclude: []schema.GroupVersionResource{
{
Group: "core",
Resource: "myresource",
},
{
Group: "mygroup",
Resource: "myresource",
},
{
Group: "",
Resource: "yourresource",
},
{
Group: "core",
Resource: "yourresource",
},
{
Group: "mygroup",
Resource: "yourresource",
},
},
},
} {
c.Check(tc.gvrs.Include(tc.m), DeepEquals, tc.include)
c.Check(tc.gvrs.Exclude(tc.m), DeepEquals, tc.exclude)
Expand Down

0 comments on commit 58d437a

Please sign in to comment.