Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apis/keda/v1alpha1/scaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ func (so *ScaledObject) HasPausedAnnotation() bool {
}

// NeedToBePausedByAnnotation will check whether ScaledObject needs to be paused based on PausedAnnotation or PausedReplicaCount
// If the paused-replicas annotation value is "false", it is treated as if the annotation was not set.
func (so *ScaledObject) NeedToBePausedByAnnotation() bool {
_, pausedReplicasAnnotationFound := so.GetAnnotations()[PausedReplicasAnnotation]
if pausedReplicasAnnotationFound {
return true
if val, pausedReplicasAnnotationFound := so.GetAnnotations()[PausedReplicasAnnotation]; pausedReplicasAnnotationFound {
if val != "false" {
return true
}
}

return getBoolAnnotation(so, PausedAnnotation)
Expand Down
18 changes: 18 additions & 0 deletions apis/keda/v1alpha1/scaledobject_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,24 @@ func TestNeedToBePausedByAnnotation(t *testing.T) {
pausedReplicaCount: &pausedReplicaCount,
expectResult: true,
},
{
name: "PausedReplicasAnnotation with false string value",
annotations: map[string]string{PausedReplicasAnnotation: "false"},
pausedReplicaCount: nil,
expectResult: false,
},
{
name: "PausedReplicasAnnotation false with PausedAnnotation true",
annotations: map[string]string{PausedAnnotation: "true", PausedReplicasAnnotation: "false"},
pausedReplicaCount: nil,
expectResult: true, // Falls back to PausedAnnotation when PausedReplicasAnnotation is "false"
},
{
name: "PausedReplicasAnnotation false with PausedAnnotation false",
annotations: map[string]string{PausedAnnotation: "false", PausedReplicasAnnotation: "false"},
pausedReplicaCount: nil,
expectResult: false,
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion controllers/keda/scaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (r *ScaledObjectReconciler) ensureScaledObjectLabel(ctx context.Context, lo

func (r *ScaledObjectReconciler) checkIfTargetResourceReachPausedCount(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) bool {
pausedReplicaCount, pausedReplicasAnnotationFound := scaledObject.GetAnnotations()[kedav1alpha1.PausedReplicasAnnotation]
if !pausedReplicasAnnotationFound {
if !pausedReplicasAnnotationFound || pausedReplicaCount == "false" {
return true
}
pausedReplicaCountNum, err := strconv.ParseInt(pausedReplicaCount, 10, 32)
Expand Down
4 changes: 4 additions & 0 deletions pkg/scaling/executor/scale_scaledobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,13 @@ func getIdleOrMinimumReplicaCount(scaledObject *kedav1alpha1.ScaledObject) (bool

// GetPausedReplicaCount returns the paused replica count of the ScaledObject.
// If not paused, it returns nil.
// If the annotation value is "false", it is treated as if the annotation was not set.
func GetPausedReplicaCount(scaledObject *kedav1alpha1.ScaledObject) (*int32, error) {
if scaledObject.Annotations != nil {
if val, ok := scaledObject.Annotations[kedav1alpha1.PausedReplicasAnnotation]; ok {
if val == "false" {
return nil, nil
}
conv, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return nil, err
Expand Down
71 changes: 71 additions & 0 deletions pkg/scaling/executor/scale_scaledobjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,74 @@ func TestNoScaleFromIdleReplicasToMinReplicasWhenActiveAndPausedScaleOutAnnotati
condition := scaledObject.Status.Conditions.GetActiveCondition()
assert.Equal(t, true, condition.IsTrue())
}

func TestGetPausedReplicaCount(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
expectedResult *int32
expectedError bool
}{
{
name: "No annotations",
annotations: nil,
expectedResult: nil,
expectedError: false,
},
{
name: "Annotation not present",
annotations: map[string]string{"other": "value"},
expectedResult: nil,
expectedError: false,
},
{
name: "Valid numeric value",
annotations: map[string]string{v1alpha1.PausedReplicasAnnotation: "5"},
expectedResult: func() *int32 { v := int32(5); return &v }(),
expectedError: false,
},
{
name: "Zero value",
annotations: map[string]string{v1alpha1.PausedReplicasAnnotation: "0"},
expectedResult: func() *int32 { v := int32(0); return &v }(),
expectedError: false,
},
{
name: "False string value",
annotations: map[string]string{v1alpha1.PausedReplicasAnnotation: "false"},
expectedResult: nil,
expectedError: false,
},
{
name: "Invalid value",
annotations: map[string]string{v1alpha1.PausedReplicasAnnotation: "invalid"},
expectedResult: nil,
expectedError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
scaledObject := &v1alpha1.ScaledObject{
ObjectMeta: v1.ObjectMeta{
Annotations: test.annotations,
},
}

result, err := GetPausedReplicaCount(scaledObject)

if test.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

if test.expectedResult == nil {
assert.Nil(t, result)
} else {
assert.NotNil(t, result)
assert.Equal(t, *test.expectedResult, *result)
}
})
}
}
Loading