Skip to content

Commit

Permalink
Add an InvertMatch flag to the resource meta filter for keeping non-m…
Browse files Browse the repository at this point in the history
…atching nodes
  • Loading branch information
jgustie committed Feb 1, 2024
1 parent fa0359f commit cc8a575
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/filters/resourcemeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type ResourceMetaFilter struct {
LabelSelector string `json:"labelSelector,omitempty" yaml:"labelSelector,omitempty"`
// Kubernetes selector matching annotations
AnnotationSelector string `json:"annotationSelector,omitempty" yaml:"annotationSelector,omitempty"`

// Invert the matching behavior (i.e. keep non-matching nodes).
InvertMatch bool `json:"invertMatch,omitempty" yaml:"invertMatch,omitempty"`
}

func (f *ResourceMetaFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
Expand All @@ -58,23 +61,23 @@ func (f *ResourceMetaFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error)
if m != nil {
if meta, err := n.GetMeta(); err != nil {
return nil, err
} else if !m.matchesMeta(meta) {
} else if m.matchesMeta(meta) == f.InvertMatch {
continue
}
}

if f.LabelSelector != "" {
if matched, err := n.MatchesLabelSelector(f.LabelSelector); err != nil {
return nil, err
} else if !matched {
} else if matched == f.InvertMatch {
continue
}
}

if f.AnnotationSelector != "" {
if matched, err := n.MatchesAnnotationSelector(f.AnnotationSelector); err != nil {
return nil, err
} else if !matched {
} else if matched == f.InvertMatch {
continue
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/filters/resourcemeta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ func TestResourceMetaFilter_Filter(t *testing.T) {
rmNode("foobar", nil, nil),
},
},
{
desc: "match name negate",
filter: ResourceMetaFilter{
Name: "foo.*",
InvertMatch: true,
},
input: []*yaml.RNode{
rmNode("foobar", nil, nil),
rmNode("barfoo", nil, nil),
},
expected: []*yaml.RNode{
rmNode("barfoo", nil, nil),
},
},
{
desc: "match annotation",
filter: ResourceMetaFilter{
Expand Down

0 comments on commit cc8a575

Please sign in to comment.