From 12fcad6185419763144bb8c3385da0b9d497db9c Mon Sep 17 00:00:00 2001 From: Rodrigo Reis Date: Wed, 14 Feb 2024 14:53:22 +0100 Subject: [PATCH] Point HPA to segment resource, when ingress versioning is set. (#585) * Point HPA to segment resource, when ingress versioning is set. Signed-off-by: Rodrigo Reis * Give proper parameter name. Signed-off-by: Rodrigo Reis --------- Signed-off-by: Rodrigo Reis --- pkg/core/autoscaler.go | 11 ++- pkg/core/stack_resources.go | 16 ++++- pkg/core/stack_resources_test.go | 118 +++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/pkg/core/autoscaler.go b/pkg/core/autoscaler.go index 645b16f4..125305f2 100644 --- a/pkg/core/autoscaler.go +++ b/pkg/core/autoscaler.go @@ -122,7 +122,12 @@ func (l autoscalerMetricsList) Less(i, j int) bool { return false } -func convertCustomMetrics(stacksetName, stackName, namespace string, metrics autoscalerMetricsList, trafficWeight float64) ([]autoscaling.MetricSpec, map[string]string, error) { +func convertCustomMetrics( + ingressResourceName, stackName, namespace string, + metrics autoscalerMetricsList, + trafficWeight float64, +) ([]autoscaling.MetricSpec, map[string]string, error) { + var resultMetrics []autoscaling.MetricSpec resultAnnotations := make(map[string]string) @@ -140,9 +145,9 @@ func convertCustomMetrics(stacksetName, stackName, namespace string, metrics aut case zv1.PodJSONAutoscalerMetric: generated, annotations, err = podJsonMetric(m) case zv1.IngressAutoscalerMetric: - generated, err = ingressMetric(m, stacksetName, stackName) + generated, err = ingressMetric(m, ingressResourceName, stackName) case zv1.RouteGroupAutoscalerMetric: - generated, err = routegroupMetric(m, stacksetName, stackName) + generated, err = routegroupMetric(m, ingressResourceName, stackName) case zv1.ZMONAutoscalerMetric: generated, annotations, err = zmonMetric(m, i, stackName, namespace) case zv1.ScalingScheduleMetric: diff --git a/pkg/core/stack_resources.go b/pkg/core/stack_resources.go index 8f8560ef..315ed7c6 100644 --- a/pkg/core/stack_resources.go +++ b/pkg/core/stack_resources.go @@ -253,7 +253,21 @@ func (sc *StackContainer) GenerateHPA() (*autoscaling.HorizontalPodAutoscaler, e result.Spec.MinReplicas = autoscalerSpec.MinReplicas result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas - metrics, annotations, err := convertCustomMetrics(sc.stacksetName, sc.Name(), sc.Namespace(), autoscalerMetricsList(autoscalerSpec.Metrics), trafficWeight) + ingressResourceName := sc.stacksetName + if sc.Resources.IngressSegment != nil { + ingressResourceName = sc.Resources.IngressSegment.Name + } + if sc.Resources.RouteGroupSegment != nil { + ingressResourceName = sc.Resources.RouteGroupSegment.Name + } + + metrics, annotations, err := convertCustomMetrics( + ingressResourceName, + sc.Name(), + sc.Namespace(), + autoscalerMetricsList(autoscalerSpec.Metrics), + trafficWeight, + ) if err != nil { return nil, err diff --git a/pkg/core/stack_resources_test.go b/pkg/core/stack_resources_test.go index aabad1d4..583d41a5 100644 --- a/pkg/core/stack_resources_test.go +++ b/pkg/core/stack_resources_test.go @@ -1330,6 +1330,7 @@ func TestGenerateHPA(t *testing.T) { for _, tc := range []struct { name string autoscaler *zv1.Autoscaler + resources StackResources expectedMinReplicas *int32 expectedMaxReplicas int32 expectedMetrics []autoscaling.MetricSpec @@ -1365,6 +1366,122 @@ func TestGenerateHPA(t *testing.T) { }, expectedBehavior: exampleBehavior, }, + { + name: "HPA in a Stack with ingress segment", + autoscaler: &zv1.Autoscaler{ + MinReplicas: &min, + MaxReplicas: max, + + Metrics: []zv1.AutoscalerMetrics{ + { + Type: zv1.CPUAutoscalerMetric, + AverageUtilization: &utilization, + }, + }, + Behavior: exampleBehavior, + }, + resources: StackResources{ + IngressSegment: &networking.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stack-traffic-segment", + }, + }, + }, + expectedMinReplicas: &min, + expectedMaxReplicas: max, + expectedMetrics: []autoscaling.MetricSpec{ + { + Type: autoscaling.ResourceMetricSourceType, + Resource: &autoscaling.ResourceMetricSource{ + Name: v1.ResourceCPU, + Target: autoscaling.MetricTarget{ + Type: autoscaling.UtilizationMetricType, + AverageUtilization: &utilization, + }, + }, + }, + }, + expectedBehavior: exampleBehavior, + }, + { + name: "HPA in a Stack with routegroup segment", + autoscaler: &zv1.Autoscaler{ + MinReplicas: &min, + MaxReplicas: max, + + Metrics: []zv1.AutoscalerMetrics{ + { + Type: zv1.CPUAutoscalerMetric, + AverageUtilization: &utilization, + }, + }, + Behavior: exampleBehavior, + }, + resources: StackResources{ + RouteGroupSegment: &rgv1.RouteGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stack-traffic-segment", + }, + }, + }, + expectedMinReplicas: &min, + expectedMaxReplicas: max, + expectedMetrics: []autoscaling.MetricSpec{ + { + Type: autoscaling.ResourceMetricSourceType, + Resource: &autoscaling.ResourceMetricSource{ + Name: v1.ResourceCPU, + Target: autoscaling.MetricTarget{ + Type: autoscaling.UtilizationMetricType, + AverageUtilization: &utilization, + }, + }, + }, + }, + expectedBehavior: exampleBehavior, + }, + { + name: "HPA in a Stack with both routegroup and ingress segment", + autoscaler: &zv1.Autoscaler{ + MinReplicas: &min, + MaxReplicas: max, + + Metrics: []zv1.AutoscalerMetrics{ + { + Type: zv1.CPUAutoscalerMetric, + AverageUtilization: &utilization, + }, + }, + Behavior: exampleBehavior, + }, + resources: StackResources{ + IngressSegment: &networking.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stack-traffic-segment", + }, + }, + RouteGroupSegment: &rgv1.RouteGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "stack-traffic-segment", + }, + }, + }, + expectedMinReplicas: &min, + expectedMaxReplicas: max, + expectedMetrics: []autoscaling.MetricSpec{ + { + Type: autoscaling.ResourceMetricSourceType, + Resource: &autoscaling.ResourceMetricSource{ + Name: v1.ResourceCPU, + Target: autoscaling.MetricTarget{ + Type: autoscaling.UtilizationMetricType, + AverageUtilization: &utilization, + }, + }, + }, + }, + expectedBehavior: exampleBehavior, + }, } { t.Run(tc.name, func(t *testing.T) { podTemplate := zv1.PodTemplateSpec{ @@ -1392,6 +1509,7 @@ func TestGenerateHPA(t *testing.T) { }, }, }, + Resources: tc.resources, } hpa, err := autoscalerContainer.GenerateHPA()