@@ -70,6 +70,30 @@ func isFailedOrHibernated(as *amaltheadevv1alpha1.AmaltheaSession) bool {
7070 as .Status .State == amaltheadevv1alpha1 .Hibernated
7171}
7272
73+ // Returns a map ensuring that the labels/annotations desired do not
74+ // overwrite well-known values.
75+ // Reference: https://kubernetes.io/docs/reference/labels-annotations-taints/
76+ func cleanWellKnown (current map [string ]string , desired map [string ]string ) map [string ]string {
77+ preserved := map [string ]string {}
78+ for key , value := range current {
79+ domain , _ , _ := strings .Cut (key , "/" )
80+ if domain == "kubernetes.io" || domain == "k8s.io" || strings .HasSuffix (domain , ".kubernetes.io" ) ||
81+ strings .HasSuffix (domain , ".k8s.io" ) {
82+ preserved [key ] = value
83+ }
84+ }
85+
86+ clean := map [string ]string {}
87+ for key , value := range desired {
88+ clean [key ] = value
89+ }
90+
91+ for key , value := range preserved {
92+ clean [key ] = value
93+ }
94+ return clean
95+ }
96+
7397func (c ChildResource [T ]) Reconcile (ctx context.Context , clnt client.Client , cr * amaltheadevv1alpha1.AmaltheaSession ) ChildResourceUpdate [T ] { //nolint:gocyclo
7498 logger := log .FromContext (ctx )
7599 if c .Current == nil {
@@ -102,8 +126,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
102126 fallthrough
103127 case amaltheadevv1alpha1 .Always :
104128 current .Spec = desired .Spec
105- current .Labels = desired .Labels
106- current .Annotations = desired .Annotations
129+ current .Labels = cleanWellKnown ( current . Labels , desired .Labels )
130+ current .Annotations = cleanWellKnown ( current . Annotations , desired .Annotations )
107131 default :
108132 return fmt .Errorf ("attempting to reconcile ingress with unknown strategy %s" , strategy )
109133 }
@@ -160,8 +184,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
160184 current .Spec .Template .Spec .InitContainers = desired .Spec .Template .Spec .InitContainers
161185 current .Spec .Template .Spec .Volumes = desired .Spec .Template .Spec .Volumes
162186 current .Spec .Selector = desired .Spec .Selector
163- current .Labels = desired .Labels
164- current .Annotations = desired .Annotations
187+ current .Labels = cleanWellKnown ( current . Labels , desired .Labels )
188+ current .Annotations = cleanWellKnown ( current . Annotations , desired .Annotations )
165189 current .Spec .Template .Labels = desired .Spec .Template .Labels
166190 current .Spec .Template .Annotations = desired .Spec .Template .Annotations
167191 default :
@@ -197,25 +221,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
197221 // NOTE: If the desired storage class is nil then the current spec contains the name for the default storage class
198222 current .Spec .StorageClassName = desired .Spec .StorageClassName
199223 }
200- // Do not touch reserved labels and annotations
201- // Reference: https://kubernetes.io/docs/reference/labels-annotations-taints/
202- // TODO: handle labels
203- current .Labels = desired .Labels
204- preservedAnnotations := map [string ]string {}
205- for key := range current .Annotations {
206- domain , _ , _ := strings .Cut (key , "/" )
207- if domain == "kubernetes.io" || domain == "k8s.io" || strings .HasSuffix (domain , ".kubernetes.io" ) ||
208- strings .HasSuffix (domain , ".k8s.io" ) {
209- preservedAnnotations [key ] = current .Annotations [key ]
210- }
211- }
212- current .Annotations = desired .Annotations
213- if current .Annotations == nil {
214- current .Annotations = map [string ]string {}
215- }
216- for key := range preservedAnnotations {
217- current .Annotations [key ] = preservedAnnotations [key ]
218- }
224+ current .Labels = cleanWellKnown (current .Labels , desired .Labels )
225+ current .Annotations = cleanWellKnown (current .Annotations , desired .Annotations )
219226 default :
220227 return fmt .Errorf ("attempting to reconcile PVC with unknown strategy %s" , strategy )
221228 }
@@ -246,8 +253,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
246253 case amaltheadevv1alpha1 .Always :
247254 current .Spec .Ports = desired .Spec .Ports
248255 current .Spec .Selector = desired .Spec .Selector
249- current .Labels = desired .Labels
250- current .Annotations = desired .Annotations
256+ current .Labels = cleanWellKnown ( current . Labels , desired .Labels )
257+ current .Annotations = cleanWellKnown ( current . Annotations , desired .Annotations )
251258 default :
252259 return fmt .Errorf ("attempting to reconcile service with unknown strategy %s" , strategy )
253260 }
@@ -293,8 +300,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
293300 }
294301 current .Data = desired .Data
295302 current .StringData = preservedStringData
296- current .Labels = desired .Labels
297- current .Annotations = desired .Annotations
303+ current .Labels = cleanWellKnown ( current . Labels , desired .Labels )
304+ current .Annotations = cleanWellKnown ( current . Annotations , desired .Annotations )
298305 default :
299306 return fmt .Errorf ("attempting to reconcile secret with unknown strategy %s" , strategy )
300307 }
@@ -362,8 +369,8 @@ func (c ChildResource[T]) Reconcile(ctx context.Context, clnt client.Client, cr
362369 current .Spec .Template .Spec .Containers = desired .Spec .Template .Spec .Containers
363370 current .Spec .Template .Spec .InitContainers = desired .Spec .Template .Spec .InitContainers
364371 current .Spec .Template .Spec .Volumes = desired .Spec .Template .Spec .Volumes
365- current .Labels = desired .Labels
366- current .Annotations = desired .Annotations
372+ current .Labels = cleanWellKnown ( current . Labels , desired .Labels )
373+ current .Annotations = cleanWellKnown ( current . Annotations , desired .Annotations )
367374 current .Spec .Template .Annotations = desired .Spec .Template .Annotations
368375 default :
369376 return fmt .Errorf ("attempting to reconcile batchjob with unknown strategy %s" , strategy )
0 commit comments