Skip to content

Commit

Permalink
Point HPA to segment resource, when ingress versioning is set. (#585)
Browse files Browse the repository at this point in the history
* Point HPA to segment resource, when ingress versioning is set.

Signed-off-by: Rodrigo Reis <[email protected]>

* Give proper parameter name.

Signed-off-by: Rodrigo Reis <[email protected]>

---------

Signed-off-by: Rodrigo Reis <[email protected]>
  • Loading branch information
gargravarr authored Feb 14, 2024
1 parent 895744c commit 12fcad6
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
11 changes: 8 additions & 3 deletions pkg/core/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion pkg/core/stack_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
118 changes: 118 additions & 0 deletions pkg/core/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -1392,6 +1509,7 @@ func TestGenerateHPA(t *testing.T) {
},
},
},
Resources: tc.resources,
}

hpa, err := autoscalerContainer.GenerateHPA()
Expand Down

0 comments on commit 12fcad6

Please sign in to comment.