@@ -32,6 +32,7 @@ import (
32
32
"k8s.io/client-go/util/retry"
33
33
"k8s.io/klog/v2"
34
34
"sigs.k8s.io/controller-runtime/pkg/client"
35
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
35
36
36
37
autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
37
38
"github.com/karmada-io/karmada/pkg/metrics"
@@ -226,105 +227,101 @@ func (c *ScalingJob) addFailedExecutionHistory(
226
227
cronFHPA * autoscalingv1alpha1.CronFederatedHPA , errMsg string ) error {
227
228
_ , nextExecutionTime := c .scheduler .NextRun ()
228
229
229
- // Add success history record, return false if there is no such rule
230
- addFailedHistoryFunc := func () bool {
231
- exists := false
232
- for index , rule := range cronFHPA .Status .ExecutionHistories {
233
- if rule .RuleName != c .rule .Name {
234
- continue
235
- }
236
- failedExecution := autoscalingv1alpha1.FailedExecution {
237
- ScheduleTime : rule .NextExecutionTime ,
238
- ExecutionTime : & metav1.Time {Time : time .Now ()},
239
- Message : errMsg ,
240
- }
241
- historyLimits := helper .GetCronFederatedHPAFailedHistoryLimits (c .rule )
242
- if len (rule .FailedExecutions ) > historyLimits - 1 {
243
- rule .FailedExecutions = rule .FailedExecutions [:historyLimits - 1 ]
244
- }
245
- cronFHPA .Status .ExecutionHistories [index ].FailedExecutions =
246
- append ([]autoscalingv1alpha1.FailedExecution {failedExecution }, rule .FailedExecutions ... )
247
- cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime = & metav1.Time {Time : nextExecutionTime }
248
- exists = true
249
- break
230
+ // Add failed history record
231
+ addFailedHistoryFunc := func (index int ) {
232
+ failedExecution := autoscalingv1alpha1.FailedExecution {
233
+ ScheduleTime : cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime ,
234
+ ExecutionTime : & metav1.Time {Time : time .Now ()},
235
+ Message : errMsg ,
250
236
}
237
+ historyLimits := helper .GetCronFederatedHPAFailedHistoryLimits (c .rule )
238
+ if len (cronFHPA .Status .ExecutionHistories [index ].FailedExecutions ) > historyLimits - 1 {
239
+ cronFHPA .Status .ExecutionHistories [index ].FailedExecutions = cronFHPA .Status .ExecutionHistories [index ].FailedExecutions [:historyLimits - 1 ]
240
+ }
241
+ cronFHPA .Status .ExecutionHistories [index ].FailedExecutions =
242
+ append ([]autoscalingv1alpha1.FailedExecution {failedExecution }, cronFHPA .Status .ExecutionHistories [index ].FailedExecutions ... )
243
+ cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime = & metav1.Time {Time : nextExecutionTime }
244
+ }
251
245
252
- return exists
246
+ index := c .findExecutionHistory (cronFHPA .Status .ExecutionHistories )
247
+ if index < 0 {
248
+ // The failed history does not exist, it means the rule deleted, so just ignore it.
249
+ return nil
253
250
}
254
251
255
- return retry .RetryOnConflict (retry .DefaultRetry , func () (err error ) {
256
- // If this history not exist, it means the rule is suspended or deleted, so just ignore it.
257
- if exists := addFailedHistoryFunc (); ! exists {
252
+ var operationResult controllerutil.OperationResult
253
+ if err := retry .RetryOnConflict (retry .DefaultRetry , func () (err error ) {
254
+ operationResult , err = helper .UpdateStatus (context .Background (), c .client , cronFHPA , func () error {
255
+ addFailedHistoryFunc (index )
258
256
return nil
259
- }
257
+ })
258
+ return err
259
+ }); err != nil {
260
+ klog .Errorf ("Failed to add failed history record to CronFederatedHPA(%s/%s): %v" , cronFHPA .Namespace , cronFHPA .Name , err )
261
+ return err
262
+ }
260
263
261
- updateErr := c .client .Status ().Update (context .Background (), cronFHPA )
262
- if updateErr == nil {
263
- klog .V (4 ).Infof ("CronFederatedHPA(%s/%s) status has been updated successfully" , cronFHPA .Namespace , cronFHPA .Name )
264
- return nil
265
- }
264
+ if operationResult == controllerutil .OperationResultUpdatedStatusOnly {
265
+ klog .V (4 ).Infof ("CronFederatedHPA(%s/%s) status has been updated successfully" , cronFHPA .Namespace , cronFHPA .Name )
266
+ }
266
267
267
- updated := & autoscalingv1alpha1.CronFederatedHPA {}
268
- if err = c .client .Get (context .Background (), client.ObjectKey {Namespace : cronFHPA .Namespace , Name : cronFHPA .Name }, updated ); err == nil {
269
- cronFHPA = updated
270
- } else {
271
- klog .Errorf ("Get CronFederatedHPA(%s/%s) failed: %v" , cronFHPA .Namespace , cronFHPA .Name , err )
268
+ return nil
269
+ }
270
+
271
+ // findExecutionHistory finds the history record, returns -1 if there is no such rule.
272
+ func (c * ScalingJob ) findExecutionHistory (histories []autoscalingv1alpha1.ExecutionHistory ) int {
273
+ for index , rule := range histories {
274
+ if rule .RuleName == c .rule .Name {
275
+ return index
272
276
}
273
- return updateErr
274
- })
277
+ }
278
+ return - 1
275
279
}
276
280
277
281
func (c * ScalingJob ) addSuccessExecutionHistory (
278
282
cronFHPA * autoscalingv1alpha1.CronFederatedHPA ,
279
283
appliedReplicas , appliedMinReplicas , appliedMaxReplicas * int32 ) error {
280
284
_ , nextExecutionTime := c .scheduler .NextRun ()
281
285
282
- // Add success history record, return false if there is no such rule
283
- addSuccessHistoryFunc := func () bool {
284
- exists := false
285
- for index , rule := range cronFHPA .Status .ExecutionHistories {
286
- if rule .RuleName != c .rule .Name {
287
- continue
288
- }
289
- successExecution := autoscalingv1alpha1.SuccessfulExecution {
290
- ScheduleTime : rule .NextExecutionTime ,
291
- ExecutionTime : & metav1.Time {Time : time .Now ()},
292
- AppliedReplicas : appliedReplicas ,
293
- AppliedMaxReplicas : appliedMaxReplicas ,
294
- AppliedMinReplicas : appliedMinReplicas ,
295
- }
296
- historyLimits := helper .GetCronFederatedHPASuccessHistoryLimits (c .rule )
297
- if len (rule .SuccessfulExecutions ) > historyLimits - 1 {
298
- rule .SuccessfulExecutions = rule .SuccessfulExecutions [:historyLimits - 1 ]
299
- }
300
- cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions =
301
- append ([]autoscalingv1alpha1.SuccessfulExecution {successExecution }, rule .SuccessfulExecutions ... )
302
- cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime = & metav1.Time {Time : nextExecutionTime }
303
- exists = true
304
- break
286
+ // Add success history record
287
+ addSuccessHistoryFunc := func (index int ) {
288
+ successExecution := autoscalingv1alpha1.SuccessfulExecution {
289
+ ScheduleTime : cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime ,
290
+ ExecutionTime : & metav1.Time {Time : time .Now ()},
291
+ AppliedReplicas : appliedReplicas ,
292
+ AppliedMaxReplicas : appliedMaxReplicas ,
293
+ AppliedMinReplicas : appliedMinReplicas ,
294
+ }
295
+ historyLimits := helper .GetCronFederatedHPASuccessHistoryLimits (c .rule )
296
+ if len (cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions ) > historyLimits - 1 {
297
+ cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions = cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions [:historyLimits - 1 ]
305
298
}
299
+ cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions =
300
+ append ([]autoscalingv1alpha1.SuccessfulExecution {successExecution }, cronFHPA .Status .ExecutionHistories [index ].SuccessfulExecutions ... )
301
+ cronFHPA .Status .ExecutionHistories [index ].NextExecutionTime = & metav1.Time {Time : nextExecutionTime }
302
+ }
306
303
307
- return exists
304
+ index := c .findExecutionHistory (cronFHPA .Status .ExecutionHistories )
305
+ if index < 0 {
306
+ // The success history does not exist, it means the rule deleted, so just ignore it.
307
+ return nil
308
308
}
309
309
310
- return retry .RetryOnConflict (retry .DefaultRetry , func () (err error ) {
311
- // If this history not exist, it means the rule deleted, so just ignore it.
312
- if exists := addSuccessHistoryFunc (); ! exists {
310
+ var operationResult controllerutil.OperationResult
311
+ if err := retry .RetryOnConflict (retry .DefaultRetry , func () (err error ) {
312
+ operationResult , err = helper .UpdateStatus (context .Background (), c .client , cronFHPA , func () error {
313
+ addSuccessHistoryFunc (index )
313
314
return nil
314
- }
315
+ })
316
+ return err
317
+ }); err != nil {
318
+ klog .Errorf ("Failed to add success history record to CronFederatedHPA(%s/%s): %v" , cronFHPA .Namespace , cronFHPA .Name , err )
319
+ return err
320
+ }
315
321
316
- updateErr := c .client .Status ().Update (context .Background (), cronFHPA )
317
- if updateErr == nil {
318
- klog .V (4 ).Infof ("CronFederatedHPA(%s/%s) status has been updated successfully" , cronFHPA .Namespace , cronFHPA .Name )
319
- return err
320
- }
322
+ if operationResult == controllerutil .OperationResultUpdatedStatusOnly {
323
+ klog .V (4 ).Infof ("CronFederatedHPA(%s/%s) status has been updated successfully" , cronFHPA .Namespace , cronFHPA .Name )
324
+ }
321
325
322
- updated := & autoscalingv1alpha1.CronFederatedHPA {}
323
- if err = c .client .Get (context .Background (), client.ObjectKey {Namespace : cronFHPA .Namespace , Name : cronFHPA .Name }, updated ); err == nil {
324
- cronFHPA = updated
325
- } else {
326
- klog .Errorf ("Get CronFederatedHPA(%s/%s) failed: %v" , cronFHPA .Namespace , cronFHPA .Name , err )
327
- }
328
- return updateErr
329
- })
326
+ return nil
330
327
}
0 commit comments