-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from practo/resync-fix
Cooldown after last scale activity: scale-down-delay-after-last-scale-activity
- Loading branch information
Showing
6 changed files
with
239 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package controller | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/practo/klog/v2" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type ScaleOperation int | ||
|
||
const ( | ||
ScaleUp ScaleOperation = iota | ||
ScaleDown | ||
ScaleNoop | ||
) | ||
|
||
func GetScaleOperation( | ||
q string, | ||
desiredWorkers int32, | ||
currentWorkers int32, | ||
lastScaleTime *metav1.Time, | ||
scaleDownDelay time.Duration) ScaleOperation { | ||
|
||
if desiredWorkers > currentWorkers { | ||
return ScaleUp | ||
} | ||
|
||
if desiredWorkers == currentWorkers { | ||
return ScaleNoop | ||
} | ||
|
||
if canScaleDown( | ||
q, desiredWorkers, currentWorkers, lastScaleTime, scaleDownDelay) { | ||
return ScaleDown | ||
} | ||
|
||
return ScaleNoop | ||
} | ||
|
||
// canScaleDown checks the scaleDownDelay and the lastScaleTime to decide | ||
// if scaling is required. Checks coolOff! | ||
func canScaleDown( | ||
q string, | ||
desiredWorkers int32, | ||
currentWorkers int32, | ||
lastScaleTime *metav1.Time, scaleDownDelay time.Duration) bool { | ||
|
||
if lastScaleTime == nil { | ||
klog.V(2).Infof("%s scaleDownDelay ignored, lastScaleTime is nil", q) | ||
return true | ||
} | ||
|
||
nextScaleDownTime := metav1.NewTime( | ||
lastScaleTime.Time.Add(scaleDownDelay), | ||
) | ||
now := metav1.Now() | ||
|
||
if nextScaleDownTime.Before(&now) { | ||
klog.V(2).Infof("%s scaleDown is allowed, cooloff passed", q) | ||
return true | ||
} | ||
|
||
klog.V(2).Infof( | ||
"%s scaleDown forbidden, nextScaleDownTime: %v", | ||
q, | ||
nextScaleDownTime, | ||
) | ||
|
||
return false | ||
} | ||
|
||
func scaleOpString(op ScaleOperation) string { | ||
switch op { | ||
case ScaleUp: | ||
return "scale-up" | ||
case ScaleDown: | ||
return "scale-down" | ||
case ScaleNoop: | ||
return "no scaling operation" | ||
default: | ||
return "" | ||
} | ||
} |
Oops, something went wrong.