Add support for custom metrics for HPA (#2423)

* Add support for custom metrics for HPA
* Cleanup TargetCPUPercent references from possible places
* HPA v2beta has 80% default  cpu limit if not set

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sanket Sudake
2022-05-09 17:16:34 +05:30
committed by GitHub
parent 925f817e42
commit 39dd65b224
13 changed files with 496 additions and 77 deletions
@@ -29,7 +29,6 @@ import (
multierror "github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"go.uber.org/zap"
asv2beta2 "k8s.io/api/autoscaling/v2beta2"
apiv1 "k8s.io/api/core/v1"
k8sErrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -490,8 +489,7 @@ func (caaf *Container) updateFunction(ctx context.Context, oldFn *fv1.Function,
return err
}
if oldFn.Spec.InvokeStrategy != newFn.Spec.InvokeStrategy {
if !reflect.DeepEqual(oldFn.Spec.InvokeStrategy, newFn.Spec.InvokeStrategy) {
// to support backward compatibility, if the function was created in default ns, we fall back to creating the
// deployment of the function in fission-function ns, so cleaning up resources there
ns := caaf.namespace
@@ -524,10 +522,13 @@ func (caaf *Container) updateFunction(ctx context.Context, oldFn *fv1.Function,
hpaChanged = true
}
if newFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent != oldFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent {
targetCpupercent := int32(newFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent)
hpaMetric := hpautils.ConvertTargetCPUToCustomMetric(targetCpupercent)
hpa.Spec.Metrics = []asv2beta2.MetricSpec{hpaMetric}
if !reflect.DeepEqual(newFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics, oldFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics) {
hpa.Spec.Metrics = newFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics
hpaChanged = true
}
if !reflect.DeepEqual(newFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior, oldFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior) {
hpa.Spec.Behavior = newFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior
hpaChanged = true
}
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"sync"
@@ -29,7 +30,6 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
autoscalingv1 "k8s.io/api/autoscaling/v1"
asv2beta2 "k8s.io/api/autoscaling/v2beta2"
apiv1 "k8s.io/api/core/v1"
k8sErrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -532,7 +532,7 @@ func (deploy *NewDeploy) updateFunction(ctx context.Context, oldFn *fv1.Function
deployChanged := false
if oldFn.Spec.InvokeStrategy != newFn.Spec.InvokeStrategy {
if !reflect.DeepEqual(oldFn.Spec.InvokeStrategy, newFn.Spec.InvokeStrategy) {
// to support backward compatibility, if the function was created in default ns, we fall back to creating the
// deployment of the function in fission-function ns, so cleaning up resources there
@@ -566,10 +566,13 @@ func (deploy *NewDeploy) updateFunction(ctx context.Context, oldFn *fv1.Function
hpaChanged = true
}
if newFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent != oldFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent {
targetCpupercent := int32(newFn.Spec.InvokeStrategy.ExecutionStrategy.TargetCPUPercent)
hpaMetric := hpautils.ConvertTargetCPUToCustomMetric(targetCpupercent)
hpa.Spec.Metrics = []asv2beta2.MetricSpec{hpaMetric}
if !reflect.DeepEqual(newFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics, oldFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics) {
hpa.Spec.Metrics = newFn.Spec.InvokeStrategy.ExecutionStrategy.Metrics
hpaChanged = true
}
if !reflect.DeepEqual(newFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior, oldFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior) {
hpa.Spec.Behavior = newFn.Spec.InvokeStrategy.ExecutionStrategy.Behavior
hpaChanged = true
}
+7 -2
View File
@@ -88,12 +88,16 @@ func (hpaops *HpaOperations) CreateOrGetHpa(ctx context.Context, hpaName string,
if maxRepl == 0 {
maxRepl = minRepl
}
targetCPU := int32(execStrategy.TargetCPUPercent)
targetCPU := int32(execStrategy.TargetCPUPercent) // nolint: staticcheck
var hpaMetrics []asv2beta2.MetricSpec
if targetCPU > 0 {
if targetCPU > 0 && targetCPU < 100 {
hpaMetrics = append(hpaMetrics, ConvertTargetCPUToCustomMetric(targetCPU))
}
if execStrategy.Metrics != nil {
hpaMetrics = append(hpaMetrics, execStrategy.Metrics...)
}
hpa := &asv2beta2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: hpaName,
@@ -105,6 +109,7 @@ func (hpaops *HpaOperations) CreateOrGetHpa(ctx context.Context, hpaName string,
MinReplicas: &minRepl,
MaxReplicas: maxRepl,
Metrics: hpaMetrics,
Behavior: execStrategy.Behavior,
},
}