diff --git a/pkg/reconciler/autoscaling/kpa/controller.go b/pkg/reconciler/autoscaling/kpa/controller.go index 3697ac956b74..13ace3799473 100644 --- a/pkg/reconciler/autoscaling/kpa/controller.go +++ b/pkg/reconciler/autoscaling/kpa/controller.go @@ -27,6 +27,8 @@ import ( ninformers "github.com/knative/serving/pkg/client/informers/externalversions/networking/v1alpha1" "github.com/knative/serving/pkg/reconciler" "github.com/knative/serving/pkg/reconciler/autoscaling/config" + "github.com/knative/serving/pkg/reconciler/autoscaling/kpa/resources" + aresources "github.com/knative/serving/pkg/reconciler/autoscaling/resources" corev1informers "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/tools/cache" ) @@ -46,8 +48,8 @@ func NewController( sksInformer ninformers.ServerlessServiceInformer, serviceInformer corev1informers.ServiceInformer, endpointsInformer corev1informers.EndpointsInformer, - kpaDeciders Deciders, - metrics Metrics, + kpaDeciders resources.Deciders, + metrics aresources.Metrics, ) *controller.Impl { c := &Reconciler{ Base: reconciler.NewBase(*opts, controllerAgentName), diff --git a/pkg/reconciler/autoscaling/kpa/kpa.go b/pkg/reconciler/autoscaling/kpa/kpa.go index 2a04e5675670..61b72cd9e498 100644 --- a/pkg/reconciler/autoscaling/kpa/kpa.go +++ b/pkg/reconciler/autoscaling/kpa/kpa.go @@ -49,39 +49,6 @@ import ( "k8s.io/client-go/tools/cache" ) -// Deciders is an interface for notifying the presence or absence of KPAs. -type Deciders interface { - // Get accesses the Decider resource for this key, returning any errors. - Get(ctx context.Context, namespace, name string) (*autoscaler.Decider, error) - - // Create adds a Decider resource for a given key, returning any errors. - Create(ctx context.Context, decider *autoscaler.Decider) (*autoscaler.Decider, error) - - // Delete removes the Decider resource for a given key, returning any errors. - Delete(ctx context.Context, namespace, name string) error - - // Watch registers a function to call when Decider change. - Watch(watcher func(string)) - - // Update update the Decider resource, return the new Decider or any errors. - Update(ctx context.Context, decider *autoscaler.Decider) (*autoscaler.Decider, error) -} - -// Metrics is an interface for notifying the presence or absence of metric collection. -type Metrics interface { - // Get accesses the Metric resource for this key, returning any errors. - Get(ctx context.Context, namespace, name string) (*autoscaler.Metric, error) - - // Create adds a Metric resource for a given key, returning any errors. - Create(ctx context.Context, metric *autoscaler.Metric) (*autoscaler.Metric, error) - - // Delete removes the Metric resource for a given key, returning any errors. - Delete(ctx context.Context, namespace, name string) error - - // Update update the Metric resource, return the new Metric or any errors. - Update(ctx context.Context, metric *autoscaler.Metric) (*autoscaler.Metric, error) -} - // Reconciler tracks PAs and right sizes the ScaleTargetRef based on the // information from Deciders. type Reconciler struct { @@ -90,8 +57,8 @@ type Reconciler struct { serviceLister corev1listers.ServiceLister sksLister nlisters.ServerlessServiceLister endpointsLister corev1listers.EndpointsLister - kpaDeciders Deciders - metrics Metrics + kpaDeciders resources.Deciders + metrics aresources.Metrics scaler *scaler configStore configStore } @@ -329,7 +296,7 @@ func (c *Reconciler) reconcileMetricsService(ctx context.Context, pa *pav1alpha1 } func (c *Reconciler) reconcileMetric(ctx context.Context, pa *pav1alpha1.PodAutoscaler) error { - desiredMetric := resources.MakeMetric(ctx, pa, config.FromContext(ctx).Autoscaler) + desiredMetric := aresources.MakeMetric(ctx, pa, config.FromContext(ctx).Autoscaler) metric, err := c.metrics.Get(ctx, desiredMetric.Namespace, desiredMetric.Name) if errors.IsNotFound(err) { metric, err = c.metrics.Create(ctx, desiredMetric) diff --git a/pkg/reconciler/autoscaling/kpa/resources/decider.go b/pkg/reconciler/autoscaling/kpa/resources/decider.go index be27d4d2ba4a..a037053dd170 100644 --- a/pkg/reconciler/autoscaling/kpa/resources/decider.go +++ b/pkg/reconciler/autoscaling/kpa/resources/decider.go @@ -22,8 +22,27 @@ import ( "github.com/knative/pkg/logging" "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1" "github.com/knative/serving/pkg/autoscaler" + "github.com/knative/serving/pkg/reconciler/autoscaling/resources" ) +// Deciders is an interface for notifying the presence or absence of KPAs. +type Deciders interface { + // Get accesses the Decider resource for this key, returning any errors. + Get(ctx context.Context, namespace, name string) (*autoscaler.Decider, error) + + // Create adds a Decider resource for a given key, returning any errors. + Create(ctx context.Context, decider *autoscaler.Decider) (*autoscaler.Decider, error) + + // Delete removes the Decider resource for a given key, returning any errors. + Delete(ctx context.Context, namespace, name string) error + + // Watch registers a function to call when Decider change. + Watch(watcher func(string)) + + // Update update the Decider resource, return the new Decider or any errors. + Update(ctx context.Context, decider *autoscaler.Decider) (*autoscaler.Decider, error) +} + // MakeDecider constructs a Decider resource from a PodAutoscaler taking // into account the PA's ContainerConcurrency and the relevant // autoscaling annotation. @@ -51,7 +70,7 @@ func MakeDecider(ctx context.Context, pa *v1alpha1.PodAutoscaler, config *autosc } panicThreshold := target * panicThresholdPercentage / 100.0 // TODO: remove MetricSpec when the custom metrics adapter implements Metric. - metricSpec := MakeMetric(ctx, pa, config).Spec + metricSpec := resources.MakeMetric(ctx, pa, config).Spec return &autoscaler.Decider{ ObjectMeta: *pa.ObjectMeta.DeepCopy(), Spec: autoscaler.DeciderSpec{ diff --git a/pkg/reconciler/autoscaling/kpa/resources/metric.go b/pkg/reconciler/autoscaling/resources/metric.go similarity index 66% rename from pkg/reconciler/autoscaling/kpa/resources/metric.go rename to pkg/reconciler/autoscaling/resources/metric.go index ec90304577ba..3e13efe92969 100644 --- a/pkg/reconciler/autoscaling/kpa/resources/metric.go +++ b/pkg/reconciler/autoscaling/resources/metric.go @@ -24,6 +24,21 @@ import ( "github.com/knative/serving/pkg/autoscaler" ) +// Metrics is an interface for notifying the presence or absence of metric collection. +type Metrics interface { + // Get accesses the Metric resource for this key, returning any errors. + Get(ctx context.Context, namespace, name string) (*autoscaler.Metric, error) + + // Create adds a Metric resource for a given key, returning any errors. + Create(ctx context.Context, metric *autoscaler.Metric) (*autoscaler.Metric, error) + + // Delete removes the Metric resource for a given key, returning any errors. + Delete(ctx context.Context, namespace, name string) error + + // Update update the Metric resource, return the new Metric or any errors. + Update(ctx context.Context, metric *autoscaler.Metric) (*autoscaler.Metric, error) +} + // MakeMetric constructs a Metric resource from a PodAutoscaler func MakeMetric(ctx context.Context, pa *v1alpha1.PodAutoscaler, config *autoscaler.Config) *autoscaler.Metric { stableWindow, ok := pa.Window() diff --git a/pkg/reconciler/autoscaling/kpa/resources/metric_test.go b/pkg/reconciler/autoscaling/resources/metric_test.go similarity index 75% rename from pkg/reconciler/autoscaling/kpa/resources/metric_test.go rename to pkg/reconciler/autoscaling/resources/metric_test.go index eccc9198a0d9..c71aa9d6e58f 100644 --- a/pkg/reconciler/autoscaling/kpa/resources/metric_test.go +++ b/pkg/reconciler/autoscaling/resources/metric_test.go @@ -106,3 +106,36 @@ func metric(options ...MetricOption) *autoscaler.Metric { } return m } + +func pa(options ...PodAutoscalerOption) *v1alpha1.PodAutoscaler { + p := &v1alpha1.PodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test-namespace", + Name: "test-name", + Annotations: map[string]string{ + autoscaling.ClassAnnotationKey: autoscaling.KPA, + }, + }, + Spec: v1alpha1.PodAutoscalerSpec{ + ContainerConcurrency: 0, + }, + Status: v1alpha1.PodAutoscalerStatus{}, + } + for _, fn := range options { + fn(p) + } + return p +} + +var config = &autoscaler.Config{ + EnableScaleToZero: true, + ContainerConcurrencyTargetPercentage: 1.0, + ContainerConcurrencyTargetDefault: 100.0, + MaxScaleUpRate: 10.0, + StableWindow: 60 * time.Second, + PanicThresholdPercentage: 200, + PanicWindow: 6 * time.Second, + PanicWindowPercentage: 10, + TickInterval: 2 * time.Second, + ScaleToZeroGracePeriod: 30 * time.Second, +}