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

Make autoscaling configuration available to the HPA reconciler. #4237

Merged
merged 1 commit 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
27 changes: 25 additions & 2 deletions pkg/reconciler/autoscaling/hpa/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ limitations under the License.
package hpa

import (
"context"

"github.com/knative/pkg/configmap"
"github.com/knative/pkg/controller"
"github.com/knative/serving/pkg/apis/autoscaling"
"github.com/knative/serving/pkg/autoscaler"
informers "github.com/knative/serving/pkg/client/informers/externalversions/autoscaling/v1alpha1"
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"
autoscalingv2beta1informers "k8s.io/client-go/informers/autoscaling/v2beta1"
"k8s.io/client-go/tools/cache"
)
Expand All @@ -30,6 +35,12 @@ const (
controllerAgentName = "hpa-class-podautoscaler-controller"
)

// configStore is a minimized interface of the actual configStore.
type configStore interface {
ToContext(ctx context.Context) context.Context
WatchConfigs(w configmap.Watcher)
}

// NewController returns a new HPA reconcile controller.
func NewController(
opts *reconciler.Options,
Expand All @@ -47,10 +58,11 @@ func NewController(

c.Logger.Info("Setting up hpa-class event handlers")
onlyHpaClass := reconciler.AnnotationFilterFunc(autoscaling.ClassAnnotationKey, autoscaling.HPA, false)
paInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
paHandler := cache.FilteringResourceEventHandler{
FilterFunc: onlyHpaClass,
Handler: controller.HandleAll(impl.Enqueue),
})
}
paInformer.Informer().AddEventHandler(paHandler)

hpaInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: onlyHpaClass,
Expand All @@ -61,5 +73,16 @@ func NewController(
FilterFunc: onlyHpaClass,
Handler: controller.HandleAll(impl.EnqueueControllerOf),
})

c.Logger.Info("Setting up ConfigMap receivers")
configsToResync := []interface{}{
&autoscaler.Config{},
}
resync := configmap.TypeFilter(configsToResync...)(func(string, interface{}) {
controller.SendGlobalUpdates(paInformer.Informer(), paHandler)
})
c.configStore = config.NewStore(c.Logger.Named("config-store"), resync)
c.configStore.WatchConfigs(opts.ConfigMapWatcher)

return impl
}
8 changes: 5 additions & 3 deletions pkg/reconciler/autoscaling/hpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ import (
type Reconciler struct {
*reconciler.Base

paLister listers.PodAutoscalerLister
sksLister nlisters.ServerlessServiceLister
hpaLister autoscalingv2beta1listers.HorizontalPodAutoscalerLister
paLister listers.PodAutoscalerLister
sksLister nlisters.ServerlessServiceLister
hpaLister autoscalingv2beta1listers.HorizontalPodAutoscalerLister
configStore configStore
}

var _ controller.Reconciler = (*Reconciler)(nil)
Expand All @@ -63,6 +64,7 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
return nil
}
logger := logging.FromContext(ctx)
ctx = c.configStore.ToContext(ctx)
logger.Debug("Reconcile hpa-class PodAutoscaler")

original, err := c.paLister.PodAutoscalers(namespace).Get(name)
Expand Down
39 changes: 35 additions & 4 deletions pkg/reconciler/autoscaling/hpa/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import (
"context"
"testing"

"github.com/knative/pkg/configmap"
"github.com/knative/pkg/controller"
logtesting "github.com/knative/pkg/logging/testing"
"github.com/knative/pkg/system"
autoscalingv1alpha1 "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1"
"github.com/knative/serving/pkg/apis/networking"
nv1a1 "github.com/knative/serving/pkg/apis/networking/v1alpha1"
"github.com/knative/serving/pkg/autoscaler"
fakeKna "github.com/knative/serving/pkg/client/clientset/versioned/fake"
informers "github.com/knative/serving/pkg/client/informers/externalversions"
"github.com/knative/serving/pkg/reconciler"
"github.com/knative/serving/pkg/reconciler/autoscaling/config"
"github.com/knative/serving/pkg/reconciler/autoscaling/hpa/resources"
aresources "github.com/knative/serving/pkg/reconciler/autoscaling/resources"

Expand Down Expand Up @@ -58,6 +62,13 @@ func TestControllerCanReconcile(t *testing.T) {
KubeClientSet: kubeClient,
ServingClientSet: servingClient,
Logger: logtesting.TestLogger(t),
ConfigMapWatcher: configmap.NewStaticWatcher(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace(),
Name: autoscaler.ConfigName,
},
Data: map[string]string{},
}),
}

servingInformer := informers.NewSharedInformerFactory(servingClient, 0)
Expand Down Expand Up @@ -406,10 +417,11 @@ func TestReconcile(t *testing.T) {
defer logtesting.ClearAll()
table.Test(t, MakeFactory(func(listers *Listers, opt reconciler.Options) controller.Reconciler {
return &Reconciler{
Base: reconciler.NewBase(opt, controllerAgentName),
paLister: listers.GetPodAutoscalerLister(),
sksLister: listers.GetServerlessServiceLister(),
hpaLister: listers.GetHorizontalPodAutoscalerLister(),
Base: reconciler.NewBase(opt, controllerAgentName),
paLister: listers.GetPodAutoscalerLister(),
sksLister: listers.GetServerlessServiceLister(),
hpaLister: listers.GetHorizontalPodAutoscalerLister(),
configStore: &testConfigStore{config: defaultConfig()},
}
}))
}
Expand Down Expand Up @@ -486,3 +498,22 @@ func deploy(namespace, name string, opts ...deploymentOption) *appsv1.Deployment
}
return s
}

func defaultConfig() *config.Config {
autoscalerConfig, _ := autoscaler.NewConfigFromMap(nil)
return &config.Config{
Autoscaler: autoscalerConfig,
}
}

type testConfigStore struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just type testConfigStore *config.Config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in Slack: This gets kinda hairy as I need cannot use the pointer type as the aliased type. I'd end up with type testConfigStore config.Config. All of the functions will remain pointer receivers though.

In the ToContext method I'll need to cast the value so the compiler eats it. On the caller side I'll need to define extra variables to be able to get a pointer to this new type.

Here's what the diff would look like:

diff --git a/pkg/reconciler/autoscaling/hpa/hpa_test.go b/pkg/reconciler/autoscaling/hpa/hpa_test.go
index 0af6c524..74de5a22 100644
--- a/pkg/reconciler/autoscaling/hpa/hpa_test.go
+++ b/pkg/reconciler/autoscaling/hpa/hpa_test.go
@@ -415,13 +415,14 @@ func TestReconcile(t *testing.T) {
 	}}
 
 	defer logtesting.ClearAll()
+	configStore := testConfigStore(*defaultConfig())
 	table.Test(t, MakeFactory(func(listers *Listers, opt reconciler.Options) controller.Reconciler {
 		return &Reconciler{
 			Base:        reconciler.NewBase(opt, controllerAgentName),
 			paLister:    listers.GetPodAutoscalerLister(),
 			sksLister:   listers.GetServerlessServiceLister(),
 			hpaLister:   listers.GetHorizontalPodAutoscalerLister(),
-			configStore: &testConfigStore{config: defaultConfig()},
+			configStore: &configStore,
 		}
 	}))
 }
@@ -506,12 +507,10 @@ func defaultConfig() *config.Config {
 	}
 }
 
-type testConfigStore struct {
-	config *config.Config
-}
+type testConfigStore config.Config
 
 func (t *testConfigStore) ToContext(ctx context.Context) context.Context {
-	return config.ToContext(ctx, t.config)
+	return config.ToContext(ctx, (*config.Config)(t))
 }
 
 func (t *testConfigStore) WatchConfigs(w configmap.Watcher) {}

config *config.Config
}

func (t *testConfigStore) ToContext(ctx context.Context) context.Context {
return config.ToContext(ctx, t.config)
}

func (t *testConfigStore) WatchConfigs(w configmap.Watcher) {}

var _ configStore = (*testConfigStore)(nil)