Merge pull request #5903 from anujagrawal699/fix/default-history-limit-constant

refactor: introduce DefaultHistoryLimit constant for CronFederatedHPA
This commit is contained in:
karmada-bot 2024-12-03 09:16:08 +08:00 committed by GitHub
commit 3323935575
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

View File

@ -22,6 +22,9 @@ import (
autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1" 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. // IsCronFederatedHPARuleSuspend returns true if the CronFederatedHPA is suspended.
func IsCronFederatedHPARuleSuspend(rule autoscalingv1alpha1.CronFederatedHPARule) bool { func IsCronFederatedHPARuleSuspend(rule autoscalingv1alpha1.CronFederatedHPARule) bool {
if rule.Suspend == nil { if rule.Suspend == nil {
@ -33,7 +36,7 @@ func IsCronFederatedHPARuleSuspend(rule autoscalingv1alpha1.CronFederatedHPARule
// GetCronFederatedHPASuccessHistoryLimits returns the successful history limits of the CronFederatedHPA. // GetCronFederatedHPASuccessHistoryLimits returns the successful history limits of the CronFederatedHPA.
func GetCronFederatedHPASuccessHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int { func GetCronFederatedHPASuccessHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int {
if rule.SuccessfulHistoryLimit == nil { if rule.SuccessfulHistoryLimit == nil {
return 3 return DefaultHistoryLimit
} }
return int(*rule.SuccessfulHistoryLimit) return int(*rule.SuccessfulHistoryLimit)
} }
@ -41,7 +44,7 @@ func GetCronFederatedHPASuccessHistoryLimits(rule autoscalingv1alpha1.CronFedera
// GetCronFederatedHPAFailedHistoryLimits returns the failed history limits of the CronFederatedHPA. // GetCronFederatedHPAFailedHistoryLimits returns the failed history limits of the CronFederatedHPA.
func GetCronFederatedHPAFailedHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int { func GetCronFederatedHPAFailedHistoryLimits(rule autoscalingv1alpha1.CronFederatedHPARule) int {
if rule.FailedHistoryLimit == nil { if rule.FailedHistoryLimit == nil {
return 3 return DefaultHistoryLimit
} }
return int(*rule.FailedHistoryLimit) return int(*rule.FailedHistoryLimit)
} }

View File

@ -68,19 +68,19 @@ func TestGetCronFederatedHPASuccessHistoryLimits(t *testing.T) {
expected int expected int
}{ }{
{ {
name: "successful history limit is nil", name: "returns default limit when history limit is unspecified",
rule: autoscalingv1alpha1.CronFederatedHPARule{}, rule: autoscalingv1alpha1.CronFederatedHPARule{},
expected: 3, expected: DefaultHistoryLimit,
}, },
{ {
name: "successful history limit is set to 5", name: "returns custom limit when specified",
rule: autoscalingv1alpha1.CronFederatedHPARule{ rule: autoscalingv1alpha1.CronFederatedHPARule{
SuccessfulHistoryLimit: ptr.To[int32](5), SuccessfulHistoryLimit: ptr.To[int32](5),
}, },
expected: 5, expected: 5,
}, },
{ {
name: "successful history limit is set to 0", name: "returns zero when limit is explicitly set to zero",
rule: autoscalingv1alpha1.CronFederatedHPARule{ rule: autoscalingv1alpha1.CronFederatedHPARule{
SuccessfulHistoryLimit: ptr.To[int32](0), SuccessfulHistoryLimit: ptr.To[int32](0),
}, },
@ -103,19 +103,19 @@ func TestGetCronFederatedHPAFailedHistoryLimits(t *testing.T) {
expected int expected int
}{ }{
{ {
name: "failed history limit is nil", name: "returns default limit when history limit is unspecified",
rule: autoscalingv1alpha1.CronFederatedHPARule{}, rule: autoscalingv1alpha1.CronFederatedHPARule{},
expected: 3, expected: DefaultHistoryLimit,
}, },
{ {
name: "failed history limit is set to 5", name: "returns custom limit when specified",
rule: autoscalingv1alpha1.CronFederatedHPARule{ rule: autoscalingv1alpha1.CronFederatedHPARule{
FailedHistoryLimit: ptr.To[int32](5), FailedHistoryLimit: ptr.To[int32](5),
}, },
expected: 5, expected: 5,
}, },
{ {
name: "failed history limit is set to 0", name: "returns zero when limit is explicitly set to zero",
rule: autoscalingv1alpha1.CronFederatedHPARule{ rule: autoscalingv1alpha1.CronFederatedHPARule{
FailedHistoryLimit: ptr.To[int32](0), FailedHistoryLimit: ptr.To[int32](0),
}, },