Skip to content

Commit

Permalink
Use fixed strings instead of knativeAutoscalingPrefix
Browse files Browse the repository at this point in the history
Use "autoscaling.knative.dev" instead of the const "knativeAutoscalingPrefix".

Signed-off-by: laminar <fangtian@kubesphere.io>
  • Loading branch information
tpiperatgod committed Jun 22, 2022
1 parent 96b63a4 commit ffb6846
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions pkg/core/serving/knative/servingrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
const (
knativeService = "serving.knative.dev/service"
componentName = "Knative/component"
knativeAutoscalingPrefix = "autoscaling.knative.dev"
)

type servingRun struct {
Expand Down Expand Up @@ -234,14 +233,19 @@ func (r *servingRun) createService(s *openfunction.Serving, cm map[string]string
labels = util.AppendLabels(s.Spec.Labels, labels)

// Handle the scale options, which have the following priority relationship:
// Annotations["autoscaling.knative.dev/max-scale" ("autoscaling.knative.dev/min-scale")] >
// ScaleOptions.Knative["max-scale" ("min-scale")] >
// ScaleOptions.Knative["autoscaling.knative.dev/max-scale" ("autoscaling.knative.dev/min-scale")] >
// ScaleOptions.maxScale (minScale) >
// Annotations["autoscaling.knative.dev/max-scale" ("autoscaling.knative.dev/min-scale")] >
// And in Knative Serving v1.1, the scale bounds' name were changed from "maxScale" ("minScale") to "max-scale" ("min-scale"),
// we need to support both of these layouts.
if s.Spec.ScaleOptions != nil {
maxScale := ""
minScale := ""

if s.Spec.Annotations == nil {
s.Spec.Annotations = map[string]string{}
}

if s.Spec.ScaleOptions.MaxReplicas != nil {
maxScale = strconv.Itoa(int(*s.Spec.ScaleOptions.MaxReplicas))
}
Expand All @@ -251,31 +255,22 @@ func (r *servingRun) createService(s *openfunction.Serving, cm map[string]string
if s.Spec.ScaleOptions.Knative != nil {
for k, v := range *s.Spec.ScaleOptions.Knative {
switch k {
case "max-scale", "maxScale":
case "autoscaling.knative.dev/max-scale", "autoscaling.knative.dev/maxScale":
maxScale = v
case "min-scale", "minScale":
case "autoscaling.knative.dev/min-scale", "autoscaling.knative.dev/minScale":
minScale = v
}
if _, exist := s.Spec.Annotations[fmt.Sprintf("%s/%s", knativeAutoscalingPrefix, k)]; !exist {
s.Spec.Annotations[k] = v
}
}
}
maxScaleAnnotationOld := fmt.Sprintf("%s/%s", knativeAutoscalingPrefix, "maxScale")
maxScaleAnnotation := fmt.Sprintf("%s/%s", knativeAutoscalingPrefix, "max-scale")
minScaleAnnotationOld := fmt.Sprintf("%s/%s", knativeAutoscalingPrefix, "minScale")
minScaleAnnotation := fmt.Sprintf("%s/%s", knativeAutoscalingPrefix, "min-scale")

if s.Spec.Annotations == nil {
s.Spec.Annotations = map[string]string{}
}
if _, exist := s.Spec.Annotations[maxScaleAnnotation]; !exist && maxScale != "" {
s.Spec.Annotations[maxScaleAnnotationOld] = maxScale
s.Spec.Annotations[maxScaleAnnotation] = maxScale
if maxScale != "" {
s.Spec.Annotations["autoscaling.knative.dev/maxScale"] = maxScale
s.Spec.Annotations["autoscaling.knative.dev/max-scale"] = maxScale
}
if _, exist := s.Spec.Annotations[minScaleAnnotation]; !exist && minScale != "" {
s.Spec.Annotations[minScaleAnnotationOld] = minScale
s.Spec.Annotations[minScaleAnnotation] = minScale

if minScale != "" {
s.Spec.Annotations["autoscaling.knative.dev/minScale"] = minScale
s.Spec.Annotations["autoscaling.knative.dev/min-scale"] = minScale
}
}

Expand Down

0 comments on commit ffb6846

Please sign in to comment.