@@ -141,7 +141,8 @@ func (u *Updater) Apply(ctx context.Context, baseObj *unstructured.Unstructured)
141141 }
142142 updateErr := u .client .Status ().Update (ctx , obj )
143143 if errors .IsConflict (updateErr ) && u .enableAggressiveConflictResolution {
144- resolved , resolveErr := u .tryRefreshForStatusUpdate (ctx , baseObj )
144+ u .logger .V (1 ).Info ("Status update conflict detected" )
145+ resolved , resolveErr := u .tryRefresh (ctx , baseObj , isSafeForStatusUpdate )
145146 if resolveErr != nil {
146147 return resolveErr
147148 }
@@ -173,7 +174,8 @@ func (u *Updater) Apply(ctx context.Context, baseObj *unstructured.Unstructured)
173174 }
174175 updateErr := u .client .Update (ctx , obj )
175176 if errors .IsConflict (updateErr ) && u .enableAggressiveConflictResolution {
176- resolved , resolveErr := u .tryRefreshForUpdate (ctx , baseObj )
177+ u .logger .V (1 ).Info ("Update conflict detected" )
178+ resolved , resolveErr := u .tryRefresh (ctx , baseObj , isSafeForUpdate )
177179 if resolveErr != nil {
178180 return resolveErr
179181 }
@@ -191,25 +193,24 @@ func (u *Updater) Apply(ctx context.Context, baseObj *unstructured.Unstructured)
191193 return err
192194}
193195
194- func (u * Updater ) tryRefreshForStatusUpdate (ctx context.Context , obj * unstructured.Unstructured ) (bool , error ) {
195- u .logger .V (1 ).Info ("Status update conflict detected" )
196- // Re-fetch object with client.
197- current := & unstructured.Unstructured {}
198- current .SetGroupVersionKind (obj .GroupVersionKind ())
199- objectKey := client .ObjectKeyFromObject (obj )
200- if err := u .client .Get (ctx , objectKey , current ); err != nil {
201- err = fmt .Errorf ("refreshing object %s/%s: %w" , objectKey .Namespace , objectKey .Name , err )
202- return false , err
203- }
196+ func isSafeForStatusUpdate (_ logr.Logger , _ * unstructured.Unstructured , _ * unstructured.Unstructured ) bool {
197+ return true
198+ }
204199
205- obj .Object = current .Object
206- u .logger .V (1 ).Info ("Refreshed object" , "namespace" ,
207- objectKey .Namespace , "name" , objectKey .Name , "gvk" , obj .GroupVersionKind ())
208- return true , nil
200+ func isSafeForUpdate (logger logr.Logger , inMemory * unstructured.Unstructured , onCluster * unstructured.Unstructured ) bool {
201+ if ! reflect .DeepEqual (inMemory .Object ["spec" ], onCluster .Object ["spec" ]) {
202+ // Diff in object spec. Nothing we can do about it -> Fail.
203+ logger .V (1 ).Info ("Not refreshing object due to spec mismatch" ,
204+ "namespace" , inMemory .GetNamespace (),
205+ "name" , inMemory .GetName (),
206+ "gkv" , inMemory .GroupVersionKind (),
207+ )
208+ return false
209+ }
210+ return true
209211}
210212
211- func (u * Updater ) tryRefreshForUpdate (ctx context.Context , obj * unstructured.Unstructured ) (bool , error ) {
212- u .logger .V (1 ).Info ("Update conflict detected" )
213+ func (u * Updater ) tryRefresh (ctx context.Context , obj * unstructured.Unstructured , isSafe func (logger logr.Logger , inMemory * unstructured.Unstructured , onCluster * unstructured.Unstructured ) bool ) (bool , error ) {
213214 // Re-fetch object with client.
214215 current := & unstructured.Unstructured {}
215216 current .SetGroupVersionKind (obj .GroupVersionKind ())
@@ -219,19 +220,15 @@ func (u *Updater) tryRefreshForUpdate(ctx context.Context, obj *unstructured.Uns
219220 return false , err
220221 }
221222
222- if ! reflect .DeepEqual (obj .Object ["spec" ], current .Object ["spec" ]) {
223- // Diff in object spec. Nothing we can do about it -> Fail.
224- u .logger .V (1 ).Info ("Not refreshing object due to spec mismatch" ,
225- "namespace" , objectKey .Namespace ,
226- "name" , objectKey .Name ,
227- "gkv" , obj .GroupVersionKind (),
228- )
223+ if ! isSafe (u .logger , obj , current ) {
229224 return false , nil
230225 }
231226
232227 obj .Object = current .Object
233228 u .logger .V (1 ).Info ("Refreshed object" ,
234- "namespace" , objectKey .Namespace , "name" , objectKey .Name , "gvk" , obj .GroupVersionKind ())
229+ "namespace" , objectKey .Namespace ,
230+ "name" , objectKey .Name ,
231+ "gvk" , obj .GroupVersionKind ())
235232 return true , nil
236233}
237234
0 commit comments