Skip to content

Commit

Permalink
Fix target name (#58)
Browse files Browse the repository at this point in the history
fix target name
  • Loading branch information
skonto authored Sep 23, 2024
1 parent fb9be59 commit 99b921e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
24 changes: 22 additions & 2 deletions pkg/reconciler/autoscaling/hpa/resources/keda.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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{}
Expand Down
5 changes: 5 additions & 0 deletions pkg/reconciler/autoscaling/hpa/resources/keda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion pkg/reconciler/autoscaling/hpa/resources/scaled_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}},
},
Expand Down

0 comments on commit 99b921e

Please sign in to comment.