@@ -207,7 +207,7 @@ func (s *supervisor) UpdateStatusMode(ctx context.Context) error {
207207 })
208208}
209209
210- func (s * supervisor ) processApplyEvent (ctx context.Context , e event.ApplyEvent , syncStats * stats.ApplyEventStats , objectStatusMap ObjectStatusMap , unknownTypeResources map [core.ID ]struct {}, resourceMap map [core.ID ]client.Object , declaredResources * declared. Resources ) status.Error {
210+ func (s * supervisor ) processApplyEvent (ctx context.Context , e event.ApplyEvent , syncStats * stats.ApplyEventStats , objectStatusMap ObjectStatusMap , unknownTypeResources map [core.ID ]struct {}, resourceMap map [core.ID ]client.Object ) status.Error {
211211 id := idFrom (e .Identifier )
212212 syncStats .Add (e .Status )
213213
@@ -231,15 +231,6 @@ func (s *supervisor) processApplyEvent(ctx context.Context, e event.ApplyEvent,
231231 case event .ApplyFailed :
232232 objectStatus .Actuation = actuation .ActuationFailed
233233 handleMetrics (ctx , "update" , e .Error )
234- // If apply failed for an ignore-mutation object, delete it from the ignore cache.
235- // Normally the cached object should be updated by the remediator when it
236- // receives a watch event - This is a fallback to force a live lookup the
237- // next time the applier runs.
238- iObj , found := declaredResources .GetIgnored (id )
239- if found {
240- klog .Infof ("Deleting object '%v' from the ignore cache (apply failed)" , core .GKNN (iObj ))
241- declaredResources .DeleteIgnored (id )
242- }
243234 switch e .Error .(type ) {
244235 case * applyerror.UnknownTypeError :
245236 unknownTypeResources [id ] = struct {}{}
@@ -254,7 +245,7 @@ func (s *supervisor) processApplyEvent(ctx context.Context, e event.ApplyEvent,
254245 case event .ApplySkipped :
255246 objectStatus .Actuation = actuation .ActuationSkipped
256247 // Skip event always includes an error with the reason
257- return s .handleApplySkippedEvent (e .Resource , id , e .Error )
248+ return s .handleApplySkippedEvent (ctx , e .Resource , id , e .Error )
258249
259250 default :
260251 return ErrorForResource (fmt .Errorf ("unexpected prune event status: %v" , e .Status ), id )
@@ -297,7 +288,7 @@ func (s *supervisor) processWaitEvent(e event.WaitEvent, syncStats *stats.WaitEv
297288}
298289
299290// handleApplySkippedEvent translates from apply skipped event into resource error.
300- func (s * supervisor ) handleApplySkippedEvent (obj * unstructured.Unstructured , id core.ID , err error ) status.Error {
291+ func (s * supervisor ) handleApplySkippedEvent (ctx context. Context , obj * unstructured.Unstructured , id core.ID , err error ) status.Error {
301292 var depErr * filter.DependencyPreventedActuationError
302293 if errors .As (err , & depErr ) {
303294 return SkipErrorForResource (err , id , depErr .Strategy )
@@ -317,11 +308,52 @@ func (s *supervisor) handleApplySkippedEvent(obj *unstructured.Unstructured, id
317308 return KptManagementConflictError (obj )
318309 }
319310
311+ var annotationErr * filter.AnnotationPreventedUpdateError
312+ if errors .As (err , & annotationErr ) {
313+ // For applies this is desired behavior, not unexpected. The following logic
314+ // re-applies just the CS metadata to ensure metadata does not drift.
315+ klog .Info ("Got AnnotationPreventedUpdateError" )
316+ if err := s .updateObjectMetadata (ctx , obj ); err != nil {
317+ return SkipErrorForResource (
318+ fmt .Errorf ("updating Config Sync metadata for ignore mutation object: %w" , err ),
319+ id , actuation .ActuationStrategyApply )
320+ }
321+ return nil
322+ }
323+
320324 return SkipErrorForResource (err , id , actuation .ActuationStrategyApply )
321325}
322326
327+ func (s * supervisor ) updateObjectMetadata (ctx context.Context , obj * unstructured.Unstructured ) error {
328+ // Using PartialObjectMetadata optimizes the client calls (uses metadata client under the hood)
329+ metaObj := & metav1.PartialObjectMetadata {}
330+ metaObj .Name = obj .GetName ()
331+ metaObj .Namespace = obj .GetNamespace ()
332+ metaObj .SetGroupVersionKind (obj .GroupVersionKind ())
333+ key := client.ObjectKey {
334+ Name : obj .GetName (),
335+ Namespace : obj .GetNamespace (),
336+ }
337+ if err := s .clientSet .Client .Get (ctx , key , metaObj ); err != nil {
338+ return err
339+ }
340+
341+ existing := metaObj .DeepCopy ()
342+ if metadata .UpdateConfigSyncMetadata (obj , metaObj ) {
343+ if err := s .clientSet .Client .Patch (ctx , metaObj , client .MergeFrom (existing ),
344+ client .FieldOwner (configsync .FieldManager )); err != nil {
345+ return err
346+ }
347+ klog .Infof ("Patched drifted CS metadata on %s" , key .String ())
348+ return nil
349+ }
350+ klog .Infof ("Skip patching CS metadata on %s" , key .String ())
351+ return nil
352+
353+ }
354+
323355// processPruneEvent handles PruneEvents from the Applier
324- func (s * supervisor ) processPruneEvent (ctx context.Context , e event.PruneEvent , syncStats * stats.PruneEventStats , objectStatusMap ObjectStatusMap , declaredResources * declared. Resources ) status.Error {
356+ func (s * supervisor ) processPruneEvent (ctx context.Context , e event.PruneEvent , syncStats * stats.PruneEventStats , objectStatusMap ObjectStatusMap ) status.Error {
325357 id := idFrom (e .Identifier )
326358 syncStats .Add (e .Status )
327359
@@ -340,13 +372,6 @@ func (s *supervisor) processPruneEvent(ctx context.Context, e event.PruneEvent,
340372 case event .PruneSuccessful :
341373 objectStatus .Actuation = actuation .ActuationSucceeded
342374 handleMetrics (ctx , "delete" , e .Error )
343-
344- iObj , found := declaredResources .GetIgnored (id )
345- if found {
346- klog .V (3 ).Infof ("Deleting object '%v' from the ignore cache" , core .GKNN (iObj ))
347- declaredResources .DeleteIgnored (id )
348- }
349-
350375 return nil
351376
352377 case event .PruneFailed :
@@ -510,15 +535,6 @@ func (s *supervisor) applyInner(ctx context.Context, eventHandler func(Event), d
510535 objStatusMap := make (ObjectStatusMap )
511536 objs := declaredResources .DeclaredObjects ()
512537
513- if err := s .cacheIgnoreMutationObjects (ctx , declaredResources ); err != nil {
514- sendErrorEvent (err , eventHandler )
515- return objStatusMap , syncStats
516- }
517-
518- if len (declaredResources .IgnoredObjects ()) > 0 {
519- klog .Infof ("%v mutation-ignored objects: %v" , len (declaredResources .IgnoredObjects ()), core .GKNNs (declaredResources .IgnoredObjects ()))
520- }
521-
522538 // disabledObjs are objects for which the management are disabled
523539 // through annotation.
524540 enabledObjs , disabledObjs := partitionObjs (objs )
@@ -535,10 +551,8 @@ func (s *supervisor) applyInner(ctx context.Context, eventHandler func(Event), d
535551 }
536552 }
537553
538- objsToApply := handleIgnoredObjects (enabledObjs , declaredResources )
539-
540- klog .Infof ("%v objects to be applied: %v" , len (objsToApply ), core .GKNNs (objsToApply ))
541- resources , err := toUnstructured (objsToApply )
554+ klog .Infof ("%v objects to be applied: %v" , len (enabledObjs ), core .GKNNs (enabledObjs ))
555+ resources , err := toUnstructured (enabledObjs )
542556 if err != nil {
543557 sendErrorEvent (err , eventHandler )
544558 return objStatusMap , syncStats
@@ -613,7 +627,7 @@ func (s *supervisor) applyInner(ctx context.Context, eventHandler func(Event), d
613627 } else {
614628 klog .V (1 ).Info (e .ApplyEvent )
615629 }
616- if err := s .processApplyEvent (ctx , e .ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap , declaredResources ); err != nil {
630+ if err := s .processApplyEvent (ctx , e .ApplyEvent , syncStats .ApplyEvent , objStatusMap , unknownTypeResources , resourceMap ); err != nil {
617631 sendErrorEvent (err , eventHandler )
618632 }
619633 case event .PruneType :
@@ -622,7 +636,7 @@ func (s *supervisor) applyInner(ctx context.Context, eventHandler func(Event), d
622636 } else {
623637 klog .V (1 ).Info (e .PruneEvent )
624638 }
625- if err := s .processPruneEvent (ctx , e .PruneEvent , syncStats .PruneEvent , objStatusMap , declaredResources ); err != nil {
639+ if err := s .processPruneEvent (ctx , e .PruneEvent , syncStats .PruneEvent , objStatusMap ); err != nil {
626640 sendErrorEvent (err , eventHandler )
627641 }
628642 default :
@@ -836,37 +850,3 @@ func (s *supervisor) abandonObject(ctx context.Context, obj client.Object) error
836850 }
837851 return nil
838852}
839-
840- // cacheIgnoreMutationObjects gets the current cluster state of any declared objects with the ignore mutation annotation and puts it in the Resources ignore objects cache
841- // Returns any errors that occur
842- func (s * supervisor ) cacheIgnoreMutationObjects (ctx context.Context , declaredResources * declared.Resources ) error {
843- var objsToUpdate []client.Object
844- declaredObjs := declaredResources .DeclaredObjects ()
845-
846- for _ , obj := range declaredObjs {
847- if obj .GetAnnotations ()[metadata .LifecycleMutationAnnotation ] == metadata .IgnoreMutation {
848-
849- if _ , found := declaredResources .GetIgnored (core .IDOf (obj )); ! found {
850- // Fetch the cluster state of the object if not already in the cache
851- uObj := & unstructured.Unstructured {}
852- uObj .SetGroupVersionKind (obj .GetObjectKind ().GroupVersionKind ())
853- err := s .clientSet .Client .Get (ctx , client .ObjectKeyFromObject (obj ), uObj )
854-
855- // Object doesn't exist on the cluster
856- if apierrors .IsNotFound (err ) {
857- continue
858- }
859-
860- if err != nil {
861- return err
862- }
863-
864- objsToUpdate = append (objsToUpdate , uObj )
865- }
866- }
867- }
868-
869- declaredResources .UpdateIgnored (objsToUpdate ... )
870-
871- return nil
872- }
0 commit comments