Skip to content

Commit 0131500

Browse files
author
Atanas Chuchev
committed
Refactor the filtering ConfigMaps by labels/annotations
1 parent ca6480f commit 0131500

5 files changed

Lines changed: 260 additions & 65 deletions

File tree

deploy/charts/disco-agent/templates/configmap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ data:
109109
resource: pods
110110
- kind: k8s-dynamic
111111
name: ark/configmaps
112-
include-resources-by-labels:
113-
conjur.org/name: "conjur-connect-configmap"
114112
config:
115113
resource-type:
116114
version: v1
117115
resource: configmaps
116+
include-resources-by-labels:
117+
conjur.org/name: "conjur-connect-configmap"

deploy/charts/disco-agent/tests/__snapshot__/configmap_test.yaml.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ custom-cluster-description:
9797
resource: pods
9898
- kind: k8s-dynamic
9999
name: ark/configmaps
100-
include-resources-by-labels:
101-
conjur.org/name: "conjur-connect-configmap"
102100
config:
103101
resource-type:
104102
version: v1
105103
resource: configmaps
104+
include-resources-by-labels:
105+
conjur.org/name: "conjur-connect-configmap"
106106
kind: ConfigMap
107107
metadata:
108108
labels:
@@ -212,12 +212,12 @@ custom-cluster-name:
212212
resource: pods
213213
- kind: k8s-dynamic
214214
name: ark/configmaps
215-
include-resources-by-labels:
216-
conjur.org/name: "conjur-connect-configmap"
217215
config:
218216
resource-type:
219217
version: v1
220218
resource: configmaps
219+
include-resources-by-labels:
220+
conjur.org/name: "conjur-connect-configmap"
221221
kind: ConfigMap
222222
metadata:
223223
labels:
@@ -327,12 +327,12 @@ custom-period:
327327
resource: pods
328328
- kind: k8s-dynamic
329329
name: ark/configmaps
330-
include-resources-by-labels:
331-
conjur.org/name: "conjur-connect-configmap"
332330
config:
333331
resource-type:
334332
version: v1
335333
resource: configmaps
334+
include-resources-by-labels:
335+
conjur.org/name: "conjur-connect-configmap"
336336
kind: ConfigMap
337337
metadata:
338338
labels:
@@ -442,12 +442,12 @@ defaults:
442442
resource: pods
443443
- kind: k8s-dynamic
444444
name: ark/configmaps
445-
include-resources-by-labels:
446-
conjur.org/name: "conjur-connect-configmap"
447445
config:
448446
resource-type:
449447
version: v1
450448
resource: configmaps
449+
include-resources-by-labels:
450+
conjur.org/name: "conjur-connect-configmap"
451451
kind: ConfigMap
452452
metadata:
453453
labels:

examples/machinehub.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,6 @@ data-gatherers:
132132
config:
133133
resource-type:
134134
version: v1
135-
resource: configmaps
135+
resource: configmaps
136+
include-resources-by-labels:
137+
conjur.org/name: "conjur-connect-configmap"

pkg/datagatherer/k8sdynamic/dynamic.go

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ func (g *DataGathererDynamic) Fetch() (any, int, error) {
415415

416416
// filter by labels
417417
labels := resource.GetLabels()
418-
if !matchesLabelFilter(labels, g.includeLabels, g.excludeLabels) {
418+
if !matchesFilter(labels, g.includeLabels, g.excludeLabels) {
419419
continue
420420
}
421421

422422
// filter by annotations
423423
annotations := resource.GetAnnotations()
424-
if !matchesAnnotationFilter(annotations, g.includeAnnotations, g.excludeAnnotations) {
424+
if !matchesFilter(annotations, g.includeAnnotations, g.excludeAnnotations) {
425425
continue
426426
}
427427

@@ -619,15 +619,15 @@ func isIncludedNamespace(namespace string, namespaces []string) bool {
619619
return slices.Contains(namespaces, namespace)
620620
}
621621

622-
// matchesLabelFilter checks if the resource labels match the include/exclude filters.
623-
// If includeLabels is set, all key-value pairs must match for the resource to be included.
622+
// matchesFilter checks if the resource metadata (labels or annotations) match the include/exclude filters.
623+
// If includeFilters is set, all key-value pairs must match for the resource to be included.
624624
// An empty string value means "match any value for this key" (key-only matching).
625-
// If excludeLabels is set, any matching key-value pair will exclude the resource.
626-
func matchesLabelFilter(resourceLabels, includeLabels, excludeLabels map[string]string) bool {
627-
// Check exclude labels first
628-
if len(excludeLabels) > 0 {
629-
for key, value := range excludeLabels {
630-
if resourceValue, exists := resourceLabels[key]; exists {
625+
// If excludeFilters is set, any matching key-value pair will exclude the resource.
626+
func matchesFilter(resourceMetadata, includeFilters, excludeFilters map[string]string) bool {
627+
// Check exclude filters first
628+
if len(excludeFilters) > 0 {
629+
for key, value := range excludeFilters {
630+
if resourceValue, exists := resourceMetadata[key]; exists {
631631
// If exclude value is empty, exclude any resource with this key
632632
// Otherwise, only exclude if the value also matches
633633
if value == "" || resourceValue == value {
@@ -637,49 +637,12 @@ func matchesLabelFilter(resourceLabels, includeLabels, excludeLabels map[string]
637637
}
638638
}
639639

640-
// Check include labels
641-
if len(includeLabels) > 0 {
642-
for key, value := range includeLabels {
643-
resourceValue, exists := resourceLabels[key]
640+
// Check include filters
641+
if len(includeFilters) > 0 {
642+
for key, value := range includeFilters {
643+
resourceValue, exists := resourceMetadata[key]
644644
if !exists {
645-
// Required label key is missing, filter it out
646-
return false
647-
}
648-
// If include value is empty, we only care that the key exists
649-
// Otherwise, the value must also match
650-
if value != "" && resourceValue != value {
651-
return false
652-
}
653-
}
654-
}
655-
656-
return true
657-
}
658-
659-
// matchesAnnotationFilter checks if the resource annotations match the include/exclude filters.
660-
// If includeAnnotations is set, all key-value pairs must match for the resource to be included.
661-
// An empty string value means "match any value for this key" (key-only matching).
662-
// If excludeAnnotations is set, any matching key-value pair will exclude the resource.
663-
func matchesAnnotationFilter(resourceAnnotations, includeAnnotations, excludeAnnotations map[string]string) bool {
664-
// Check exclude annotations first
665-
if len(excludeAnnotations) > 0 {
666-
for key, value := range excludeAnnotations {
667-
if resourceValue, exists := resourceAnnotations[key]; exists {
668-
// If exclude value is empty, exclude any resource with this key
669-
// Otherwise, only exclude if the value also matches
670-
if value == "" || resourceValue == value {
671-
return false
672-
}
673-
}
674-
}
675-
}
676-
677-
// Check include annotations
678-
if len(includeAnnotations) > 0 {
679-
for key, value := range includeAnnotations {
680-
resourceValue, exists := resourceAnnotations[key]
681-
if !exists {
682-
// Required annotation key is missing, filter it out
645+
// Required key is missing, filter it out
683646
return false
684647
}
685648
// If include value is empty, we only care that the key exists

0 commit comments

Comments
 (0)