diff --git a/pkg/fleetautoscalers/fleetautoscalers.go b/pkg/fleetautoscalers/fleetautoscalers.go index f1df1d8bbf..5ef77e0edf 100644 --- a/pkg/fleetautoscalers/fleetautoscalers.go +++ b/pkg/fleetautoscalers/fleetautoscalers.go @@ -28,6 +28,10 @@ import ( "strings" "time" + "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/apimachinery/pkg/util/uuid" + agonesv1 "agones.dev/agones/pkg/apis/agones/v1" autoscalingv1 "agones.dev/agones/pkg/apis/autoscaling/v1" listeragonesv1 "agones.dev/agones/pkg/client/listers/agones/v1" @@ -35,9 +39,6 @@ import ( "agones.dev/agones/pkg/gameservers" gssets "agones.dev/agones/pkg/gameserversets" "agones.dev/agones/pkg/util/runtime" - "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/apimachinery/pkg/util/uuid" ) var tlsConfig = &tls.Config{} @@ -319,6 +320,12 @@ func applyCounterOrListPolicy(c *autoscalingv1.CounterPolicy, l *autoscalingv1.L if err != nil { return 0, false, err } + // If the Aggregated Allocated Counts is 0 then desired capacity gets calculated as 0. If the + // capacity of 1 replica is equal to or greater than minimum capacity we can exit early. + if aggAllocatedCount <= 0 && capacity >= minCapacity { + return 1, true, nil + } + // The desired TOTAL capacity based on the Aggregated Allocated Counts (see applyBufferPolicy for explanation) desiredCapacity := int64(math.Ceil(float64(aggAllocatedCount*100) / float64(100-bufferPercent))) // Convert into a desired AVAILABLE capacity aka the buffer diff --git a/pkg/fleetautoscalers/fleetautoscalers_test.go b/pkg/fleetautoscalers/fleetautoscalers_test.go index 175971f660..0b1f9bee74 100644 --- a/pkg/fleetautoscalers/fleetautoscalers_test.go +++ b/pkg/fleetautoscalers/fleetautoscalers_test.go @@ -24,17 +24,18 @@ import ( "net/http/httptest" "testing" - agonesv1 "agones.dev/agones/pkg/apis/agones/v1" - autoscalingv1 "agones.dev/agones/pkg/apis/autoscaling/v1" - "agones.dev/agones/pkg/gameservers" - agtesting "agones.dev/agones/pkg/testing" - utilruntime "agones.dev/agones/pkg/util/runtime" "github.com/stretchr/testify/assert" admregv1 "k8s.io/api/admissionregistration/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/intstr" k8stesting "k8s.io/client-go/testing" + + agonesv1 "agones.dev/agones/pkg/apis/agones/v1" + autoscalingv1 "agones.dev/agones/pkg/apis/autoscaling/v1" + "agones.dev/agones/pkg/gameservers" + agtesting "agones.dev/agones/pkg/testing" + utilruntime "agones.dev/agones/pkg/util/runtime" ) const ( @@ -1950,8 +1951,8 @@ func TestApplyListPolicy(t *testing.T) { }}}}, }, want: expected{ - replicas: 0, - limited: false, + replicas: 1, + limited: true, wantErr: false, }, },