Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Metric interfaces into the general autoscaling package. #4236

Merged
merged 3 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/reconciler/autoscaling/kpa/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
Expand Down
39 changes: 3 additions & 36 deletions pkg/reconciler/autoscaling/kpa/kpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion pkg/reconciler/autoscaling/kpa/resources/decider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}