@@ -166,15 +166,15 @@ func (r *reconciler) updateStatusKptGroup(ctx context.Context, resgroup *v1alpha
166
166
}
167
167
168
168
func (r * reconciler ) startReconcilingStatus (status v1alpha1.ResourceGroupStatus ) v1alpha1.ResourceGroupStatus {
169
- newStatus := v1alpha1. ResourceGroupStatus {
170
- ObservedGeneration : status . ObservedGeneration ,
171
- ResourceStatuses : status .ResourceStatuses ,
172
- SubgroupStatuses : status .SubgroupStatuses ,
173
- Conditions : []v1alpha1. Condition {
174
- newReconcilingCondition ( v1alpha1 . TrueConditionStatus , StartReconciling , startReconcilingMsg ) ,
175
- newStalledCondition (v1alpha1 .FalseConditionStatus , "" , "" ),
176
- } ,
177
- }
169
+ // Preserve existing status and only update fields that need to change.
170
+ // This avoids repeatedly updating the status just to update a timestamp.
171
+ // It also prevents fighting with other controllers updating the status.
172
+ newStatus := * status .DeepCopy ()
173
+ // Update conditions, if they've changed
174
+ newStatus . Conditions = updateCondition ( newStatus . Conditions ,
175
+ newReconcilingCondition (v1alpha1 .TrueConditionStatus , StartReconciling , startReconcilingMsg ))
176
+ newStatus . Conditions = updateCondition ( newStatus . Conditions ,
177
+ newStalledCondition ( v1alpha1 . FalseConditionStatus , "" , "" ))
178
178
return newStatus
179
179
}
180
180
@@ -186,32 +186,33 @@ func (r *reconciler) endReconcilingStatus(
186
186
status v1alpha1.ResourceGroupStatus ,
187
187
generation int64 ,
188
188
) v1alpha1.ResourceGroupStatus {
189
- // reset newStatus to make sure the former setting of newStatus does not carry over
190
- newStatus := v1alpha1.ResourceGroupStatus {}
189
+ // Preserve existing status and only update fields that need to change.
190
+ // This avoids repeatedly updating the status just to update a timestamp.
191
+ // It also prevents fighting with other controllers updating the status.
192
+ newStatus := * status .DeepCopy ()
191
193
startTime := time .Now ()
192
194
reconcileTimeout := getReconcileTimeOut (len (spec .Subgroups ) + len (spec .Resources ))
193
195
194
196
finish := make (chan struct {})
195
197
go func () {
198
+ defer close (finish )
196
199
newStatus .ResourceStatuses = r .computeResourceStatuses (ctx , id , status , spec .Resources , namespacedName )
197
200
newStatus .SubgroupStatuses = r .computeSubGroupStatuses (ctx , id , status , spec .Subgroups , namespacedName )
198
- close (finish )
199
201
}()
200
202
select {
201
203
case <- finish :
202
204
newStatus .ObservedGeneration = generation
203
- newStatus .Conditions = []v1alpha1.Condition {
204
- newReconcilingCondition (v1alpha1 .FalseConditionStatus , FinishReconciling , finishReconcilingMsg ),
205
- aggregateResourceStatuses (newStatus .ResourceStatuses ),
206
- }
205
+ // Update conditions, if they've changed
206
+ newStatus .Conditions = updateCondition (newStatus .Conditions ,
207
+ newReconcilingCondition (v1alpha1 .FalseConditionStatus , FinishReconciling , finishReconcilingMsg ))
208
+ newStatus .Conditions = updateCondition (newStatus .Conditions ,
209
+ aggregateResourceStatuses (newStatus .ResourceStatuses ))
207
210
case <- time .After (reconcileTimeout ):
208
- newStatus .ObservedGeneration = status .ObservedGeneration
209
- newStatus .ResourceStatuses = status .ResourceStatuses
210
- newStatus .SubgroupStatuses = status .SubgroupStatuses
211
- newStatus .Conditions = []v1alpha1.Condition {
212
- newReconcilingCondition (v1alpha1 .FalseConditionStatus , ExceedTimeout , exceedTimeoutMsg ),
213
- newStalledCondition (v1alpha1 .TrueConditionStatus , ExceedTimeout , exceedTimeoutMsg ),
214
- }
211
+ // Update conditions, if they've changed
212
+ newStatus .Conditions = updateCondition (newStatus .Conditions ,
213
+ newReconcilingCondition (v1alpha1 .FalseConditionStatus , ExceedTimeout , exceedTimeoutMsg ))
214
+ newStatus .Conditions = updateCondition (newStatus .Conditions ,
215
+ newStalledCondition (v1alpha1 .TrueConditionStatus , ExceedTimeout , exceedTimeoutMsg ))
215
216
}
216
217
217
218
metrics .RecordReconcileDuration (ctx , newStatus .Conditions [1 ].Reason , startTime )
0 commit comments