diff --git a/pkg/reconciler/autoscaling/hpa/resources/keda.go b/pkg/reconciler/autoscaling/hpa/resources/keda.go index 925ba501..c97463d6 100644 --- a/pkg/reconciler/autoscaling/hpa/resources/keda.go +++ b/pkg/reconciler/autoscaling/hpa/resources/keda.go @@ -50,6 +50,8 @@ const ( KedaAutoscalingAnnotationHPAScaleUpRules = autoscaling.GroupName + "/hpa-scale-up-rules" KedaAutoscalingAnnotationHPAScaleDownRules = autoscaling.GroupName + "/hpa-scale-down-rules" KedaAutoscaleAnnotationsScaledObjectOverride = autoscaling.GroupName + "/scaled-object-override" + + defaultCPUTarget = 70 ) // DesiredScaledObject creates an ScaledObject KEDA resource from a PA resource. @@ -90,7 +92,7 @@ func DesiredScaledObject(ctx context.Context, pa *autoscalingv1alpha1.PodAutosca sO.Spec.MinReplicaCount = ptr.Int32(1) } - if target, ok := pa.Target(); ok { + if target, ok := resolveTarget(pa); ok { mt, err := getMetricType(pa.Annotations, pa.Metric()) if err != nil { return nil, err @@ -151,6 +153,10 @@ func DesiredScaledObject(ctx context.Context, pa *autoscalingv1alpha1.PodAutosca sO.Spec.Triggers = append(sO.Spec.Triggers, extraPrometheusTriggers...) + if len(sO.Spec.Triggers) == 0 { + return nil, fmt.Errorf("no triggers were specified, make sure a metric target is specified or extra triggers are added") + } + if window, hasWindow := pa.Window(); hasWindow { windowSeconds := int32(window.Seconds()) sO.Spec.Advanced.HorizontalPodAutoscalerConfig.Behavior = &autoscalingv2.HorizontalPodAutoscalerBehavior{ @@ -188,6 +194,18 @@ func DesiredScaledObject(ctx context.Context, pa *autoscalingv1alpha1.PodAutosca return &sO, nil } +func resolveTarget(pa *autoscalingv1alpha1.PodAutoscaler) (float64, bool) { + if target, ok := pa.Target(); ok { + return target, true + } + // When user has not specified a target value, we default to 70% for CPU + // This improves the UX as Serving defaults to CPU if no metric is specified via an annotation. + if pa.Metric() == autoscaling.CPU { + return defaultCPUTarget, true + } + return 0, false +} + func getDefaultPrometheusTrigger(annotations map[string]string, address string, query string, threshold string, ns string, targetType autoscalingv2.MetricTargetType) (*v1alpha1.ScaleTriggers, error) { var name string @@ -303,7 +321,9 @@ func setScaledObjectDefaults(sO *v1alpha1.ScaledObject, max int32, pa *autoscali if sO.Spec.ScaleTargetRef == nil { sO.Spec.ScaleTargetRef = &v1alpha1.ScaleTarget{} } - sO.Spec.ScaleTargetRef.Name = pa.Name + "-deployment" + sO.Spec.ScaleTargetRef.Name = pa.Spec.ScaleTargetRef.Name + sO.Spec.ScaleTargetRef.Kind = pa.Spec.ScaleTargetRef.Kind + sO.Spec.ScaleTargetRef.APIVersion = pa.Spec.ScaleTargetRef.APIVersion if sO.Spec.Advanced == nil { sO.Spec.Advanced = &v1alpha1.AdvancedConfig{} sO.Spec.Advanced.HorizontalPodAutoscalerConfig = &v1alpha1.HorizontalPodAutoscalerConfig{} diff --git a/pkg/reconciler/autoscaling/hpa/resources/keda_test.go b/pkg/reconciler/autoscaling/hpa/resources/keda_test.go index 099108eb..7d89e064 100644 --- a/pkg/reconciler/autoscaling/hpa/resources/keda_test.go +++ b/pkg/reconciler/autoscaling/hpa/resources/keda_test.go @@ -319,6 +319,11 @@ func TestDesiredScaledObject(t *testing.T) { t.Run(tt.name, func(t *testing.T) { pa := helpers.PodAutoscaler(helpers.TestNamespace, helpers.TestRevision, WithHPAClass, helpers.WithAnnotations(tt.paAnnotations)) scaledObject, err := DesiredScaledObject(ctx, pa) + if tt.wantScaledObject != nil { + tt.wantScaledObject.Spec.ScaleTargetRef.Name = pa.Spec.ScaleTargetRef.Name + tt.wantScaledObject.Spec.ScaleTargetRef.Kind = pa.Spec.ScaleTargetRef.Kind + tt.wantScaledObject.Spec.ScaleTargetRef.APIVersion = pa.Spec.ScaleTargetRef.APIVersion + } if (err != nil) != tt.wantErr { t.Fatalf("Failed to create desiredScaledObject, error = %v, want: %v", err, tt.wantErr) } else if err == nil { diff --git a/pkg/reconciler/autoscaling/hpa/resources/scaled_object.go b/pkg/reconciler/autoscaling/hpa/resources/scaled_object.go index d487691e..1bc5993c 100644 --- a/pkg/reconciler/autoscaling/hpa/resources/scaled_object.go +++ b/pkg/reconciler/autoscaling/hpa/resources/scaled_object.go @@ -32,7 +32,6 @@ func ScaledObject(namespace, name string, options ...ScaledObjectOption) *kedav1 Namespace: namespace, }, Spec: kedav1alpha1.ScaledObjectSpec{ - ScaleTargetRef: &kedav1alpha1.ScaleTarget{Name: name + "-deployment"}, Advanced: &kedav1alpha1.AdvancedConfig{ HorizontalPodAutoscalerConfig: &kedav1alpha1.HorizontalPodAutoscalerConfig{}}, },