From 47528b417e0119b88fd8f5f06f149dd4b60e2de1 Mon Sep 17 00:00:00 2001 From: Anuj Agrawal Date: Mon, 2 Dec 2024 18:59:44 +0530 Subject: [PATCH] refactor: introduce DefaultHistoryLimit constant for CronFederatedHPA Signed-off-by: Anuj Agrawal --- pkg/util/helper/cronfederatedhpa.go | 7 +++++-- pkg/util/helper/cronfederatedhpa_test.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/util/helper/cronfederatedhpa.go b/pkg/util/helper/cronfederatedhpa.go index 639d5d2aba22..b94ad95b87e7 100644 --- a/pkg/util/helper/cronfederatedhpa.go +++ b/pkg/util/helper/cronfederatedhpa.go @@ -22,6 +22,9 @@ import ( autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1" ) +// DefaultHistoryLimit defines the default number of history entries to keep +const DefaultHistoryLimit = 3 + // IsCronFederatedHPARuleSuspend returns true if the CronFederatedHPA is suspended. func IsCronFederatedHPARuleSuspend(rule autoscalingv1alpha1.CronFederatedHPARule) bool { if rule.Suspend == nil { @@ -33,7 +36,7 @@ func IsCronFederatedHPARuleSuspend(rule autoscalingv1alpha1.CronFederatedHPARule // GetCronFederatedHPASuccessHistoryLimits returns the successful history limits of the CronFederatedHPA. func GetCronFederatedHPASuccessHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int { if rule.SuccessfulHistoryLimit == nil { - return 3 + return DefaultHistoryLimit } return int(*rule.SuccessfulHistoryLimit) } @@ -41,7 +44,7 @@ func GetCronFederatedHPASuccessHistoryLimits(rule autoscalingv1alpha1.CronFedera // GetCronFederatedHPAFailedHistoryLimits returns the failed history limits of the CronFederatedHPA. func GetCronFederatedHPAFailedHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int { if rule.FailedHistoryLimit == nil { - return 3 + return DefaultHistoryLimit } return int(*rule.FailedHistoryLimit) } diff --git a/pkg/util/helper/cronfederatedhpa_test.go b/pkg/util/helper/cronfederatedhpa_test.go index f2283ce17c3b..3654657dd3d7 100644 --- a/pkg/util/helper/cronfederatedhpa_test.go +++ b/pkg/util/helper/cronfederatedhpa_test.go @@ -68,19 +68,19 @@ func TestGetCronFederatedHPASuccessHistoryLimits(t *testing.T) { expected int }{ { - name: "successful history limit is nil", + name: "returns default limit when history limit is unspecified", rule: autoscalingv1alpha1.CronFederatedHPARule{}, - expected: 3, + expected: DefaultHistoryLimit, }, { - name: "successful history limit is set to 5", + name: "returns custom limit when specified", rule: autoscalingv1alpha1.CronFederatedHPARule{ SuccessfulHistoryLimit: ptr.To[int32](5), }, expected: 5, }, { - name: "successful history limit is set to 0", + name: "returns zero when limit is explicitly set to zero", rule: autoscalingv1alpha1.CronFederatedHPARule{ SuccessfulHistoryLimit: ptr.To[int32](0), }, @@ -103,19 +103,19 @@ func TestGetCronFederatedHPAFailedHistoryLimits(t *testing.T) { expected int }{ { - name: "failed history limit is nil", + name: "returns default limit when history limit is unspecified", rule: autoscalingv1alpha1.CronFederatedHPARule{}, - expected: 3, + expected: DefaultHistoryLimit, }, { - name: "failed history limit is set to 5", + name: "returns custom limit when specified", rule: autoscalingv1alpha1.CronFederatedHPARule{ FailedHistoryLimit: ptr.To[int32](5), }, expected: 5, }, { - name: "failed history limit is set to 0", + name: "returns zero when limit is explicitly set to zero", rule: autoscalingv1alpha1.CronFederatedHPARule{ FailedHistoryLimit: ptr.To[int32](0), },