@@ -282,7 +282,11 @@ func (c dynamicOperatorClient) applyOperatorStatus(ctx context.Context, fieldMan
282282 uncastOriginal , err := c .informer .Lister ().Get (c .configName )
283283 switch {
284284 case apierrors .IsNotFound (err ):
285- // do nothing and proceed with the apply
285+ // set last transitionTimes and then apply
286+ // If our cache improperly 404's (the lister wasn't synchronized), then we will improperly reset all the last transition times.
287+ // This isn't ideal, but we shouldn't hit this case unless a loop isn't waiting for HasSynced.
288+ v1helpers .SetApplyConditionsLastTransitionTime (& desiredConfiguration .Conditions , nil )
289+
286290 case err != nil :
287291 return fmt .Errorf ("unable to read existing %q: %w" , c .configName , err )
288292 default :
@@ -295,6 +299,24 @@ func (c dynamicOperatorClient) applyOperatorStatus(ctx context.Context, fieldMan
295299 return fmt .Errorf ("unable to extract status for %q: %w" , fieldManager , err )
296300 }
297301
302+ // set last transitionTimes to properly calculate a difference
303+ // It is possible for last transition time to shift a couple times until the cache updates to have the condition[*].status match,
304+ // but it will eventually settle. The failing sequence looks like
305+ /*
306+ 1. type=foo, status=false, time=t0.Now
307+ 2. type=foo, status=true, time=t1.Now
308+ 3. rapid update happens and the cache still indicates #1
309+ 4. type=foo, status=true, time=t2.Now (this *should* be t1.Now)
310+ */
311+ // Eventually the cache updates to see at #2 and we stop applying new times.
312+ // This only becomes pathological if the condition is also flapping, but if that happens the time should also update.
313+ switch {
314+ case desiredConfiguration != nil && desiredConfiguration .Conditions != nil && previouslyDesiredConfiguration != nil :
315+ v1helpers .SetApplyConditionsLastTransitionTime (& desiredConfiguration .Conditions , previouslyDesiredConfiguration .Conditions )
316+ case desiredConfiguration != nil && desiredConfiguration .Conditions != nil && previouslyDesiredConfiguration == nil :
317+ v1helpers .SetApplyConditionsLastTransitionTime (& desiredConfiguration .Conditions , nil )
318+ }
319+
298320 // canonicalize so the DeepEqual works consistently
299321 v1helpers .CanonicalizeStaticPodOperatorStatus (previouslyDesiredConfiguration )
300322 v1helpers .CanonicalizeStaticPodOperatorStatus (desiredConfiguration )
0 commit comments