From 60c9a5ecbdfee06a7b0c95aede7a3247af4eceb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Thu, 19 Dec 2024 11:35:29 +0300 Subject: [PATCH] Do not attempt to truncate revision history if revisionHistoryLimit is negative --- .../statefulset/stateful_set_control.go | 2 +- .../statefulset/stateful_set_control_test.go | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/controller/statefulset/stateful_set_control.go b/pkg/controller/statefulset/stateful_set_control.go index e8048ed2d91..c5eef3c8ac9 100644 --- a/pkg/controller/statefulset/stateful_set_control.go +++ b/pkg/controller/statefulset/stateful_set_control.go @@ -194,7 +194,7 @@ func (ssc *defaultStatefulSetControl) truncateHistory( } historyLen := len(history) historyLimit := int(*set.Spec.RevisionHistoryLimit) - if historyLen <= historyLimit { + if historyLimit < 0 || historyLen <= historyLimit { return nil } // delete any non-live history to maintain the revision limit. diff --git a/pkg/controller/statefulset/stateful_set_control_test.go b/pkg/controller/statefulset/stateful_set_control_test.go index f5bb2a44b1b..b7ad4094cee 100644 --- a/pkg/controller/statefulset/stateful_set_control_test.go +++ b/pkg/controller/statefulset/stateful_set_control_test.go @@ -53,6 +53,7 @@ import ( "k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller/history" "k8s.io/kubernetes/pkg/features" + "k8s.io/utils/ptr" ) type invariantFunc func(set *apps.StatefulSet, om *fakeObjectManager) error @@ -2031,6 +2032,13 @@ func TestStatefulSetControlLimitsHistory(t *testing.T) { if err != nil { t.Fatalf("%s: %s", test.name, err) } + + if *set.Spec.RevisionHistoryLimit < 0 { + // If the revisionHistoryLimit is negative value, we don't truncate + // the revision history and it is incremental. + continue + } + if len(revisions) > int(*set.Spec.RevisionHistoryLimit)+2 { t.Fatalf("%s: %d greater than limit %d", test.name, len(revisions), *set.Spec.RevisionHistoryLimit) } @@ -2052,6 +2060,33 @@ func TestStatefulSetControlLimitsHistory(t *testing.T) { return burst(newStatefulSet(3)) }, }, + { + name: "zero revisionHistoryLimit", + invariants: assertMonotonicInvariants, + initial: func() *apps.StatefulSet { + sts := newStatefulSet(3) + sts.Spec.RevisionHistoryLimit = ptr.To(int32(0)) + return sts + }, + }, + { + name: "negative revisionHistoryLimit", + invariants: assertMonotonicInvariants, + initial: func() *apps.StatefulSet { + sts := newStatefulSet(3) + sts.Spec.RevisionHistoryLimit = ptr.To(int32(-2)) + return sts + }, + }, + { + name: "positive revisionHistoryLimit", + invariants: assertMonotonicInvariants, + initial: func() *apps.StatefulSet { + sts := newStatefulSet(3) + sts.Spec.RevisionHistoryLimit = ptr.To(int32(5)) + return sts + }, + }, } for i := range tests { testFn(t, &tests[i])