Skip to content

Commit

Permalink
Filter on GroupVersionResourceList (#5451)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdmanv authored and Ilya Kislenko committed Apr 24, 2019
1 parent b6bf53f commit bdbc0ef
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
8 changes: 5 additions & 3 deletions pkg/filter/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (s *CRDSuite) TestCRDMatcher(c *C) {
c.Assert(gvrs, Not(HasLen), 0)

// We assume there's at least one CRD in the cluster.
c.Assert(g.Include(gvrs), Not(HasLen), 0)
c.Assert(g.Exclude(gvrs), Not(HasLen), 0)
c.Assert(len(g.Exclude(gvrs))+len(g.Include(gvrs)), Equals, len(gvrs))
igvrs := GroupVersionResourceList(gvrs).Include(g)
egvrs := GroupVersionResourceList(gvrs).Exclude(g)
c.Assert(igvrs, Not(HasLen), 0)
c.Assert(egvrs, Not(HasLen), 0)
c.Assert(len(igvrs)+len(egvrs), Equals, len(gvrs))
}
43 changes: 30 additions & 13 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

type ResourceRequirement struct {
Group string
Version string
Resource string
Group string `json:"group,omitempty"`
Version string `json:"version,omitempty"`
Resource string `json:"resource,omitempty"`
}

func (r ResourceRequirement) Matches(gvr schema.GroupVersionResource) bool {
Expand Down Expand Up @@ -42,23 +42,40 @@ func (g ResourceMatcher) All(gvr schema.GroupVersionResource) bool {
return true
}

func (g ResourceMatcher) Include(gvrs []schema.GroupVersionResource) []schema.GroupVersionResource {
return g.apply(gvrs, false)
type GroupVersionResourceList []schema.GroupVersionResource

func (g GroupVersionResourceList) Include(ms ...ResourceMatcher) GroupVersionResourceList {
return g.apply(ms, false)
}

func (g ResourceMatcher) Exclude(gvrs []schema.GroupVersionResource) []schema.GroupVersionResource {
return g.apply(gvrs, true)
func (g GroupVersionResourceList) Exclude(ms ...ResourceMatcher) GroupVersionResourceList {
return g.apply(ms, true)
}

func (g ResourceMatcher) apply(gvrs []schema.GroupVersionResource, exclude bool) []schema.GroupVersionResource {
if g.Empty() {
return gvrs
func (g GroupVersionResourceList) apply(ms []ResourceMatcher, exclude bool) GroupVersionResourceList {
m := joinResourceMatchers(ms...)
if m.Empty() {
return g
}
filtered := make([]schema.GroupVersionResource, 0, len(gvrs))
for _, gvr := range gvrs {
if exclude != g.Any(gvr) {
filtered := make([]schema.GroupVersionResource, 0, len(g))
for _, gvr := range g {
if exclude != m.Any(gvr) {
filtered = append(filtered, gvr)
}
}
return filtered
}

func joinResourceMatchers(ms ...ResourceMatcher) ResourceMatcher {
n := 0
for _, m := range ms {
n += len(m)
}
gvr := make(ResourceMatcher, n)
i := 0
for _, m := range ms {
copy(gvr[i:], []ResourceRequirement(m))
i += len(m)
}
return gvr
}
42 changes: 30 additions & 12 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ func (s *FilterSuite) TestGroupVersionResourceAnyAll(c *C) {

func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
for _, tc := range []struct {
g ResourceMatcher
gvrs []schema.GroupVersionResource
include []schema.GroupVersionResource
exclude []schema.GroupVersionResource
m ResourceMatcher
gvrs GroupVersionResourceList
include GroupVersionResourceList
exclude GroupVersionResourceList
}{
{
g: nil,
m: nil,
gvrs: []schema.GroupVersionResource{
schema.GroupVersionResource{},
},
Expand All @@ -220,7 +220,7 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
},
},
{
g: ResourceMatcher{},
m: ResourceMatcher{},
gvrs: []schema.GroupVersionResource{
schema.GroupVersionResource{},
},
Expand All @@ -232,7 +232,7 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
},
},
{
g: ResourceMatcher{ResourceRequirement{}},
m: ResourceMatcher{ResourceRequirement{}},
gvrs: []schema.GroupVersionResource{
schema.GroupVersionResource{},
},
Expand All @@ -242,7 +242,7 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
exclude: []schema.GroupVersionResource{},
},
{
g: ResourceMatcher{ResourceRequirement{}},
m: ResourceMatcher{ResourceRequirement{}},
gvrs: []schema.GroupVersionResource{
schema.GroupVersionResource{
Group: "mygroup",
Expand All @@ -256,7 +256,7 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
exclude: []schema.GroupVersionResource{},
},
{
g: ResourceMatcher{
m: ResourceMatcher{
ResourceRequirement{
Group: "mygroup",
},
Expand All @@ -274,7 +274,7 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
exclude: []schema.GroupVersionResource{},
},
{
g: ResourceMatcher{
m: ResourceMatcher{
ResourceRequirement{
Group: "mygroup",
},
Expand Down Expand Up @@ -334,7 +334,25 @@ func (s *FilterSuite) TestGroupVersionResourceIncludeExclude(c *C) {
},
},
} {
c.Check(tc.g.Include(tc.gvrs), DeepEquals, tc.include)
c.Check(tc.g.Exclude(tc.gvrs), DeepEquals, tc.exclude)
c.Check(tc.gvrs.Include(tc.m), DeepEquals, tc.include)
c.Check(tc.gvrs.Exclude(tc.m), DeepEquals, tc.exclude)
}
}

func (s *FilterSuite) TestJoin(c *C) {
for _, tc := range []struct {
m []ResourceMatcher
out ResourceMatcher
}{
{
m: []ResourceMatcher{ResourceMatcher{}, ResourceMatcher{}},
out: ResourceMatcher{},
},
{
m: []ResourceMatcher{ResourceMatcher{}, ResourceMatcher{}},
out: ResourceMatcher{},
},
} {
c.Check(joinResourceMatchers(tc.m...), DeepEquals, tc.out)
}
}
12 changes: 6 additions & 6 deletions pkg/filter/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import (

type Specs map[schema.GroupVersionResource][]unstructured.Unstructured

func (s Specs) keys() []schema.GroupVersionResource {
gvrs := make([]schema.GroupVersionResource, 0, len(s))
func (s Specs) keys() GroupVersionResourceList {
gvrs := make(GroupVersionResourceList, 0, len(s))
for gvr := range s {
gvrs = append(gvrs, gvr)
}
return gvrs
}

func (s Specs) Include(g ResourceMatcher) Specs {
gvrs := g.Include(s.keys())
func (s Specs) Include(ms ...ResourceMatcher) Specs {
gvrs := s.keys().Include(ms...)
ret := make(Specs, len(gvrs))
for _, gvr := range gvrs {
ret[gvr] = s[gvr]
}
return ret
}

func (s Specs) Exclude(g ResourceMatcher) Specs {
gvrs := g.Exclude(s.keys())
func (s Specs) Exclude(ms ...ResourceMatcher) Specs {
gvrs := s.keys().Exclude(ms...)
ret := make(Specs, len(gvrs))
for _, gvr := range gvrs {
ret[gvr] = s[gvr]
Expand Down

0 comments on commit bdbc0ef

Please sign in to comment.