Skip to content

Commit 153727c

Browse files
committed
fix: tests
Signed-off-by: Soumya Ghosh Dastidar <[email protected]>
1 parent f4f5be2 commit 153727c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

controller/appcontroller.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"k8s.io/client-go/kubernetes"
4242
"k8s.io/client-go/tools/cache"
4343
"k8s.io/client-go/util/workqueue"
44+
"k8s.io/utils/ptr"
4445

4546
commitclient "github.com/argoproj/argo-cd/v3/commitserver/apiclient"
4647
"github.com/argoproj/argo-cd/v3/common"
@@ -2244,20 +2245,22 @@ func (ctrl *ApplicationController) shouldSelfHeal(app *appv1.Application, alread
22442245
return true, time.Duration(0)
22452246
}
22462247

2247-
timeSinceOperation := time.Since(app.Status.OperationState.FinishedAt.Time)
2248+
var timeSinceOperation *time.Duration
2249+
if app.Status.OperationState.FinishedAt != nil {
2250+
timeSinceOperation = ptr.To(time.Since(app.Status.OperationState.FinishedAt.Time))
2251+
}
22482252

22492253
// Reset counter if the prior sync was successful and the cooldown period is over OR if the revision has changed
2250-
if !alreadyAttempted || (len(app.Status.History) > 0 && timeSinceOperation >= ctrl.selfHealBackoffCooldown && app.Status.Sync.Status == appv1.SyncStatusCodeSynced) {
2251-
log.Printf("Resetting self-heal attempts count for %s/%s", app.Namespace, app.Name)
2254+
if !alreadyAttempted || (timeSinceOperation != nil && *timeSinceOperation >= ctrl.selfHealBackoffCooldown && app.Status.Sync.Status == appv1.SyncStatusCodeSynced) {
22522255
app.Status.OperationState.Operation.Sync.SelfHealAttemptsCount = 0
22532256
}
22542257

22552258
var retryAfter time.Duration
22562259
if ctrl.selfHealBackOff == nil {
2257-
if app.Status.OperationState.FinishedAt == nil {
2260+
if timeSinceOperation == nil {
22582261
retryAfter = ctrl.selfHealTimeout
22592262
} else {
2260-
retryAfter = ctrl.selfHealTimeout - timeSinceOperation
2263+
retryAfter = ctrl.selfHealTimeout - *timeSinceOperation
22612264
}
22622265
} else {
22632266
backOff := *ctrl.selfHealBackOff
@@ -2267,10 +2270,11 @@ func (ctrl *ApplicationController) shouldSelfHeal(app *appv1.Application, alread
22672270
for i := 0; i < steps; i++ {
22682271
delay = backOff.Step()
22692272
}
2270-
if app.Status.OperationState.FinishedAt == nil {
2273+
2274+
if timeSinceOperation == nil {
22712275
retryAfter = delay
22722276
} else {
2273-
retryAfter = delay - timeSinceOperation
2277+
retryAfter = delay - *timeSinceOperation
22742278
}
22752279
}
22762280
return retryAfter <= 0, retryAfter

controller/appcontroller_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func newFakeControllerWithResync(data *fakeData, appResyncPeriod time.Duration,
172172
time.Second,
173173
time.Minute,
174174
nil,
175-
time.Second,
175+
time.Minute,
176176
0,
177177
time.Second*10,
178178
common.DefaultPortArgoCDMetrics,
@@ -2643,10 +2643,18 @@ func TestSelfHealExponentialBackoff(t *testing.T) {
26432643
alreadyAttempted: false,
26442644
expectedAttempts: 0,
26452645
syncStatus: v1alpha1.SyncStatusCodeOutOfSync,
2646-
}, {
2646+
}, { // backoff will not reset as finished tme isn't >= cooldown
26472647
attempts: 6,
2648-
finishedAt: nil,
2649-
expectedDuration: 0,
2648+
finishedAt: ptr.To(metav1.Now()),
2649+
expectedDuration: 120 * time.Second,
2650+
shouldSelfHeal: false,
2651+
alreadyAttempted: true,
2652+
expectedAttempts: 6,
2653+
syncStatus: v1alpha1.SyncStatusCodeSynced,
2654+
}, { // backoff will reset as finished time is >= cooldown
2655+
attempts: 40,
2656+
finishedAt: &metav1.Time{Time: time.Now().Add(-(1 * time.Minute))},
2657+
expectedDuration: -60 * time.Second,
26502658
shouldSelfHeal: true,
26512659
alreadyAttempted: true,
26522660
expectedAttempts: 0,

0 commit comments

Comments
 (0)