From 495f843f28246349704e0adcbaf895bbc909a9c0 Mon Sep 17 00:00:00 2001 From: Vaibhav Khurana Date: Wed, 15 Apr 2020 04:00:21 +0530 Subject: [PATCH] Adds delay in scale down interval --- main.go | 5 +++-- pkg/scaler/scaler.go | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 31551a2..ccdaaf3 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ type options struct { kubeconfig string syncInterval time.Duration scaleInterval time.Duration + scaleDownDelay time.Duration statsD string statsDInterval time.Duration } @@ -58,7 +59,7 @@ func main() { kingpin.Flag("kubeconfig", "Path to kubeconfig.").StringVar(&opts.kubeconfig) kingpin.Flag("sync-interval", "Interval to periodically refresh Scaler objects from Kubernetes.").Default("1m").DurationVar(&opts.syncInterval) kingpin.Flag("scale-interval", "Interval to check queue sizes and scale deployments.").Default("1m").DurationVar(&opts.scaleInterval) - + kingpin.Flag("scaledown-delay", "Delay in scaling down the pods once the scale down threshold is met.").Default("1s").DurationVar(&opts.scaleDownDelay) kingpin.Flag("statsd", "UDP address to publish StatsD metrics. e.g. 127.0.0.1:8125").Default("").StringVar(&opts.statsD) kingpin.Flag("statsd-interval", "Interval to publish to StatsD").Default("10s").DurationVar(&opts.statsDInterval) @@ -109,7 +110,7 @@ func main() { cache := crd.NewCache(sc, opts.syncInterval) go cache.Run(ctx) - s := scaler.New(cs, cache.Store, opts.scaleInterval) + s := scaler.New(cs, cache.Store, opts.scaleInterval, opts.scaleDownDelay) go s.Run(ctx) <-stopChan diff --git a/pkg/scaler/scaler.go b/pkg/scaler/scaler.go index c2ca081..e572f1e 100644 --- a/pkg/scaler/scaler.go +++ b/pkg/scaler/scaler.go @@ -21,10 +21,11 @@ type Scaler struct { client *kubernetes.Clientset store cache.Store interval time.Duration + delay time.Duration } -func New(client *kubernetes.Clientset, store cache.Store, interval time.Duration) *Scaler { - return &Scaler{client, store, interval} +func New(client *kubernetes.Clientset, store cache.Store, interval time.Duration, delay time.Duration) *Scaler { + return &Scaler{client, store, interval, delay} } func (s Scaler) Run(ctx context.Context) error { @@ -70,6 +71,7 @@ func (s Scaler) targetReplicas(size int64, scale *crd.SqsAutoScaler, d *appsv1.D desired := replicas + scale.Spec.ScaleUp.Amount return min(desired, scale.Spec.MaxPods), nil } else if size <= scale.Spec.ScaleDown.Threshold { + time.Sleep(s.delay * time.Second) desired := replicas - scale.Spec.ScaleDown.Amount return max(desired, scale.Spec.MinPods), nil }