Skip to content

Commit

Permalink
Add ResourceRequirement type with object reference (#5886)
Browse files Browse the repository at this point in the history
## Change Overview

Add ResourceRequirement type which includes an object reference.
This allows creating resource filters that can be scoped to a resource name
in addition to a resource type requirement (GVR)

## Pull request type

Please check the type of change your PR introduces:
- [ ] Work in Progress
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Trival/Minor
- [ ] Bugfix
- [x] Feature
  • Loading branch information
Vaibhav Kamra committed Jun 22, 2019
1 parent b46f598 commit a53f588
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand Down Expand Up @@ -42,6 +43,16 @@ func (g ResourceTypeMatcher) All(gvr schema.GroupVersionResource) bool {
return true
}

// ResourceMatcher constructs a resource matcher
// based on a `ResourceTypeMatcher`
func (g ResourceTypeMatcher) ResourceMatcher() ResourceMatcher {
rm := make(ResourceMatcher, 0, len(g))
for _, rtr := range g {
rm = append(rm, ResourceRequirement{ResourceTypeRequirement: rtr})
}
return rm
}

type GroupVersionResourceList []schema.GroupVersionResource

func (g GroupVersionResourceList) Include(ms ...ResourceTypeMatcher) GroupVersionResourceList {
Expand Down Expand Up @@ -79,3 +90,36 @@ func joinResourceTypeMatchers(ms ...ResourceTypeMatcher) ResourceTypeMatcher {
}
return gvr
}

// ResourceRequirement allows specifying a resource requirement by type and/or name
type ResourceRequirement struct {
v1.LocalObjectReference `json:",inline,omitempty"`
ResourceTypeRequirement `json:",inline,omitempty"`
}

type ResourceMatcher []ResourceRequirement

func (g ResourceMatcher) Empty() bool {
return len(g) == 0
}

// TypeMatcher constructs a resource type matcher
// based on a `ResourceMatcher`
//
// The `usageExclusion` flag should be set to true
// if the type matcher will be used as an exclude filter
func (rm ResourceMatcher) TypeMatcher(usageInclusion bool) ResourceTypeMatcher {
rtm := make(ResourceTypeMatcher, 0, len(rm))
for _, rr := range rm {
// Include the type requirement from the ResourceRequirement, if
// - There is no "Name" filter or
// - The intended usage of the returned type matcher is "inclusion"
// i.e. it is OK to include *all* resources that have this type
// but is not OK to exclude *all* resources that have this
// type
if rr.Name == "" || usageInclusion {
rtm = append(rtm, rr.ResourceTypeRequirement)
}
}
return rtm
}

0 comments on commit a53f588

Please sign in to comment.