From 8b1155123ddb61483f802c9c67d57271daf50b54 Mon Sep 17 00:00:00 2001 From: Andy Librian Date: Thu, 11 Aug 2022 10:06:24 +0700 Subject: [PATCH] use min replicas set by autoscaler in ScaleFromZero if autoscaler is specified Without this, the canary replicas are updated twice: to 1 replica then after a few seconds to the value of HPA minReplicas. In some cases, when updated to 1 replica (before updated by HPA controller to the minReplicas), it's considered ready: 1 of 1 (readyThreshold 100%), and the canary weight is advanced to receive traffic with less capacity than expected. Co-Authored-By: Joshua Gibeon Co-authored-by: Sanskar Jaiswal Signed-off-by: Andy Librian --- pkg/canary/deployment_controller.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/canary/deployment_controller.go b/pkg/canary/deployment_controller.go index 0d3fdd75..5d176d4a 100644 --- a/pkg/canary/deployment_controller.go +++ b/pkg/canary/deployment_controller.go @@ -197,6 +197,30 @@ func (c *DeploymentController) ScaleFromZero(cd *flaggerv1.Canary) error { if primary.Spec.Replicas != nil && *primary.Spec.Replicas > 0 { replicas = primary.Spec.Replicas } + } else if cd.Spec.AutoscalerRef != nil { + if cd.Spec.AutoscalerRef.Kind == "HorizontalPodAutoscaler" { + hpa, err := c.kubeClient.AutoscalingV2().HorizontalPodAutoscalers(cd.Namespace).Get(context.TODO(), cd.Spec.AutoscalerRef.Name, metav1.GetOptions{}) + if err == nil { + if hpa.Spec.MinReplicas != nil && *hpa.Spec.MinReplicas > 1 { + replicas = hpa.Spec.MinReplicas + } + } else { + // fallback to v2beta2 + hpa, err := c.kubeClient.AutoscalingV2beta2().HorizontalPodAutoscalers(cd.Namespace).Get(context.TODO(), cd.Spec.AutoscalerRef.Name, metav1.GetOptions{}) + if err == nil { + if hpa.Spec.MinReplicas != nil && *hpa.Spec.MinReplicas > 1 { + replicas = hpa.Spec.MinReplicas + } + } + } + } else if cd.Spec.AutoscalerRef.Kind == "ScaledObject" { + so, err := c.flaggerClient.KedaV1alpha1().ScaledObjects(cd.Namespace).Get(context.TODO(), cd.Spec.AutoscalerRef.Name, metav1.GetOptions{}) + if err == nil { + if so.Spec.MinReplicaCount != nil && *so.Spec.MinReplicaCount > 1 { + replicas = so.Spec.MinReplicaCount + } + } + } } depCopy := dep.DeepCopy() depCopy.Spec.Replicas = replicas