55 "strings"
66
77 corev1 "k8s.io/api/core/v1"
8+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+ k8slabels "k8s.io/apimachinery/pkg/labels"
810 "k8s.io/client-go/tools/cache"
911)
1012
@@ -42,11 +44,11 @@ func (l *LabelsAndAnnotations) Update(key string, cm *corev1.ConfigMap) error {
4244func (l * LabelsAndAnnotations ) SetLabels (namespace string , name string , kind string , labels map [string ]string ) bool {
4345 desired := map [string ]string {}
4446 if registry , ok := l .namespaces [namespace ]; ok {
45- registry .setLabels (name , kind , desired )
47+ registry .setLabels (name , kind , labels , desired )
4648 }
4749 if namespace != l .controllerNamespace {
4850 if registry , ok := l .namespaces [l .controllerNamespace ]; ok {
49- registry .setLabels (name , kind , desired )
51+ registry .setLabels (name , kind , labels , desired )
5052 }
5153 }
5254 return setValues (desired , labels )
@@ -55,24 +57,56 @@ func (l *LabelsAndAnnotations) SetLabels(namespace string, name string, kind str
5557func (l * LabelsAndAnnotations ) SetAnnotations (namespace string , name string , kind string , annotations map [string ]string ) bool {
5658 desired := map [string ]string {}
5759 if registry , ok := l .namespaces [namespace ]; ok {
58- registry .setAnnotations (name , kind , desired )
60+ registry .setAnnotations (name , kind , annotations , desired )
5961 }
6062 if namespace != l .controllerNamespace {
6163 if registry , ok := l .namespaces [l .controllerNamespace ]; ok {
62- registry .setAnnotations (name , kind , desired )
64+ registry .setAnnotations (name , kind , annotations , desired )
6365 }
6466 }
6567 return setValues (desired , annotations )
6668}
6769
70+ func (l * LabelsAndAnnotations ) SetObjectMetadata (namespace string , name string , kind string , meta * metav1.ObjectMeta ) bool {
71+ if meta == nil {
72+ return false
73+ }
74+ if meta .Labels == nil {
75+ meta .Labels = map [string ]string {}
76+ }
77+ if meta .Annotations == nil {
78+ meta .Annotations = map [string ]string {}
79+ }
80+ changed := false
81+ if registry , ok := l .namespaces [namespace ]; ok {
82+ if registry .filter (name , kind , meta .Labels , meta .Labels , meta .Annotations ) {
83+ changed = true
84+ }
85+ }
86+ if namespace != l .controllerNamespace {
87+ if registry , ok := l .namespaces [l .controllerNamespace ]; ok {
88+ if registry .filter (name , kind , meta .Labels , meta .Labels , meta .Annotations ) {
89+ changed = true
90+ }
91+ }
92+ }
93+ return changed
94+ }
95+
6896type Registry struct {
69- config map [string ]* corev1. ConfigMap
97+ config map [string ]* templateEntry
7098 log * slog.Logger
7199}
72100
101+ type templateEntry struct {
102+ cm * corev1.ConfigMap
103+ selector k8slabels.Selector
104+ invalid bool
105+ }
106+
73107func newRegistry (log * slog.Logger ) * Registry {
74108 return & Registry {
75- config : map [string ]* corev1. ConfigMap {},
109+ config : map [string ]* templateEntry {},
76110 log : log ,
77111 }
78112}
@@ -94,27 +128,53 @@ func (r *Registry) update(key string, cm *corev1.ConfigMap) error {
94128 slog .String ("name" , cm .Name ),
95129 )
96130 }
97- r .config [key ] = cm
131+ entry := & templateEntry {cm : cm }
132+ if cm .Data != nil {
133+ if selector , ok := cm .Data ["labelSelector" ]; ok && selector != "" {
134+ req , err := k8slabels .Parse (selector )
135+ if err != nil {
136+ r .log .Info ("Ignoring label-template due to invalid labelSelector" ,
137+ slog .String ("name" , cm .Name ),
138+ slog .String ("namespace" , cm .Namespace ),
139+ slog .String ("labelSelector" , selector ),
140+ slog .Any ("error" , err ),
141+ )
142+ entry .invalid = true
143+ } else {
144+ entry .selector = req
145+ }
146+ }
147+ }
148+ r .config [key ] = entry
98149 return nil
99150}
100151
101- func (r * Registry ) setLabels (name string , kind string , labels map [string ]string ) bool {
102- return r .filter (name , kind , labels , nil )
152+ func (r * Registry ) setLabels (name string , kind string , target map [ string ] string , labels map [string ]string ) bool {
153+ return r .filter (name , kind , target , labels , nil )
103154}
104155
105- func (r * Registry ) setAnnotations (name string , kind string , annotations map [string ]string ) bool {
106- return r .filter (name , kind , nil , annotations )
156+ func (r * Registry ) setAnnotations (name string , kind string , target map [ string ] string , annotations map [string ]string ) bool {
157+ return r .filter (name , kind , target , nil , annotations )
107158}
108159
109- func (r * Registry ) filter (name string , kind string , labels map [string ]string , annotations map [string ]string ) bool {
160+ func (r * Registry ) filter (name string , kind string , target map [ string ] string , labels map [string ]string , annotations map [string ]string ) bool {
110161 changed := false
111- for _ , cm := range r .config {
162+ for _ , entry := range r .config {
163+ if entry .invalid {
164+ continue
165+ }
166+ cm := entry .cm
112167 if ! matchKey (cm , "name" , name ) {
113168 continue
114169 }
115170 if ! matchKey (cm , "kind" , kind ) {
116171 continue
117172 }
173+ if entry .selector != nil {
174+ if target == nil || ! entry .selector .Matches (k8slabels .Set (target )) {
175+ continue
176+ }
177+ }
118178 excludes := exclude (cm )
119179 if labels != nil {
120180 for k , v := range cm .ObjectMeta .Labels {
0 commit comments