Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🎉 add exclusion filters for labels and annotations #57

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/apis/gitopszombies/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ type Config struct {

// ExcludeResources configures filters to exclude resources from zombies list.
type ExcludeResources struct {
Cluster *string `json:"cluster,omitempty"`
Name *string `json:"name,omitempty"`
Namespace *string `json:"namespace,omitempty"`
Cluster *string `json:"cluster,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Name *string `json:"name,omitempty"`
Namespace *string `json:"namespace,omitempty"`
metav1.TypeMeta `json:",inline"`
}
14 changes: 14 additions & 0 deletions pkg/apis/gitopszombies/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions pkg/collector/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ func IgnoreRuleExclusions(cluster string, exclusions []v1.ExcludeResources) Filt
continue
}

if !resourceMatchesMetadata(res.GetAnnotations(), exclusion.Annotations) {
continue
}

if !resourceMatchesMetadata(res.GetLabels(), exclusion.Labels) {
continue
}

if resourceMatchesName(res, exclusion.Name) {
return true
}
Expand Down Expand Up @@ -215,6 +223,25 @@ func resourceMatchesNamespace(res unstructured.Unstructured, namespace *string)
return true
}

func resourceMatchesMetadata(resMetadata, metadata map[string]string) bool {
for key, val := range metadata {
v, ok := resMetadata[key]
if !ok {
return false
}

match, err := regexp.MatchString(`^`+val+`$`, v)
if err != nil {
klog.Error(err)
}
if !match {
return false
}
}

return true
}

func resourceMatchesName(res unstructured.Unstructured, name *string) bool {
if name != nil {
match, err := regexp.MatchString(`^`+*name+`$`, res.GetName())
Expand Down
50 changes: 50 additions & 0 deletions pkg/collector/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getExclusionListResourceSet() *unstructured.UnstructuredList {
Version: "v1",
Kind: "Backup",
})
res1.SetAnnotations(map[string]string{"test-annotation": "velero-capi-backup-1"})

res2 := unstructured.Unstructured{}
res2.SetName("velero-capi-backup-2")
Expand All @@ -55,6 +56,7 @@ func getExclusionListResourceSet() *unstructured.UnstructuredList {
Version: "v2",
Kind: "Backup",
})
res2.SetLabels(map[string]string{"test-label": "velero-capi-backup-2"})

res3 := unstructured.Unstructured{}
res3.SetName("velero-capi-backup-3")
Expand Down Expand Up @@ -387,6 +389,54 @@ func TestDiscovery(t *testing.T) {
list: getExclusionListResourceSet,
expectedPass: 0,
},
{
name: "Resources excluded from conf: match restricted by annotation",
filters: func() []FilterFunc {
return []FilterFunc{IgnoreRuleExclusions("test", []gitopszombiesv1.ExcludeResources{
{
Annotations: map[string]string{"test-annotation": "velero-capi-backup-1"},
},
})}
},
list: getExclusionListResourceSet,
expectedPass: 2,
},
{
name: "Resources excluded from conf: match restricted by annotation (regexp)",
filters: func() []FilterFunc {
return []FilterFunc{IgnoreRuleExclusions("test", []gitopszombiesv1.ExcludeResources{
{
Annotations: map[string]string{"test-annotation": "v.*"},
},
})}
},
list: getExclusionListResourceSet,
expectedPass: 2,
},
{
name: "Resources excluded from conf: match restricted by label",
filters: func() []FilterFunc {
return []FilterFunc{IgnoreRuleExclusions("test", []gitopszombiesv1.ExcludeResources{
{
Labels: map[string]string{"test-label": "velero-capi-backup-2"},
},
})}
},
list: getExclusionListResourceSet,
expectedPass: 2,
},
{
name: "Resources excluded from conf: match restricted by label (regexp)",
filters: func() []FilterFunc {
return []FilterFunc{IgnoreRuleExclusions("test", []gitopszombiesv1.ExcludeResources{
{
Labels: map[string]string{"test-label": "v.*"},
},
})}
},
list: getExclusionListResourceSet,
expectedPass: 2,
},
{
name: "Resources excluded from conf: match restricted by name",
filters: func() []FilterFunc {
Expand Down