Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 4f421e7

Browse files
authored
Fix truncate history bug (#364)
1 parent f4ca46a commit 4f421e7

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

controllers/flinkcluster_observer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,11 @@ func (observer *ClusterStateObserver) truncateHistory(observed *ObservedClusterS
552552
historyLimit = 10
553553
}
554554

555-
history := make([]*appsv1.ControllerRevision, 0, len(revisions))
556-
historyLen := len(history)
557-
if historyLen <= historyLimit {
558-
return nil
559-
}
555+
nonLiveHistory := getNonLiveHistory(revisions, historyLimit)
556+
560557
// delete any non-live history to maintain the revision limit.
561-
history = history[:(historyLen - historyLimit)]
562-
for i := 0; i < len(history); i++ {
563-
if err := observer.history.DeleteControllerRevision(history[i]); err != nil {
558+
for i := 0; i < len(nonLiveHistory); i++ {
559+
if err := observer.history.DeleteControllerRevision(nonLiveHistory[i]); err != nil {
564560
return err
565561
}
566562
}

controllers/flinkcluster_util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,17 @@ func getUpdateState(observed ObservedClusterState) UpdateState {
519519
}
520520
return UpdateStateUpdating
521521
}
522+
523+
func getNonLiveHistory(revisions []*appsv1.ControllerRevision, historyLimit int) []*appsv1.ControllerRevision {
524+
525+
history := append([]*appsv1.ControllerRevision{}, revisions...)
526+
nonLiveHistory := make([]*appsv1.ControllerRevision, 0)
527+
528+
historyLen := len(history)
529+
if historyLen <= historyLimit {
530+
return nonLiveHistory
531+
}
532+
533+
nonLiveHistory = append(nonLiveHistory, history[:(historyLen-historyLimit)]...)
534+
return nonLiveHistory
535+
}

controllers/flinkcluster_util_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,18 @@ func TestGetFlinkAPIBaseURL(t *testing.T) {
553553
apiBaseURL = getFlinkAPIBaseURL(&cluster)
554554
assert.Equal(t, apiBaseURL, "http://mycluster-jobmanager.default.svc.my.domain:8004")
555555
}
556+
557+
func TestGetNonLiveHistory(t *testing.T) {
558+
revison0 := appsv1.ControllerRevision{Revision: int64(0)}
559+
revison1 := appsv1.ControllerRevision{Revision: int64(1)}
560+
revisions := []*appsv1.ControllerRevision{&revison0, &revison1}
561+
562+
historyLimit := 1
563+
nonLiveHistory := getNonLiveHistory(revisions, historyLimit)
564+
assert.Equal(t, len(nonLiveHistory), 1)
565+
assert.Equal(t, nonLiveHistory[0].Revision, int64(0))
566+
567+
historyLimit = 3
568+
nonLiveHistory = getNonLiveHistory(revisions, historyLimit)
569+
assert.Equal(t, len(nonLiveHistory), 0)
570+
}

0 commit comments

Comments
 (0)