diff --git a/controller/controller.go b/controller/controller.go index b714432f0..4471b9016 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" "github.com/knative/pkg/kmeta" @@ -452,3 +453,22 @@ func GetResyncPeriod(ctx context.Context) time.Duration { func GetTrackerLease(ctx context.Context) time.Duration { return 3 * GetResyncPeriod(ctx) } + +// erKey is used to associate record.EventRecorders with contexts. +type erKey struct{} + +// WithEventRecorder attaches the given record.EventRecorder to the provided context +// in the returned context. +func WithEventRecorder(ctx context.Context, er record.EventRecorder) context.Context { + return context.WithValue(ctx, erKey{}, er) +} + +// GetEventRecorder attempts to look up the record.EventRecorder on a given context. +// It may return null if none is found. +func GetEventRecorder(ctx context.Context) record.EventRecorder { + untyped := ctx.Value(erKey{}) + if untyped == nil { + return nil + } + return untyped.(record.EventRecorder) +} diff --git a/controller/controller_test.go b/controller/controller_test.go index 630040247..cb042c251 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -27,6 +27,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" . "github.com/knative/pkg/controller/testing" @@ -936,3 +937,17 @@ func TestGetResyncPeriod(t *testing.T) { t.Errorf("GetTrackerLease() = %v, wanted %v", got, want) } } + +func TestGetEventRecorder(t *testing.T) { + ctx := context.Background() + + if got := GetEventRecorder(ctx); got != nil { + t.Errorf("GetEventRecorder() = %v, wanted nil", got) + } + + ctx = WithEventRecorder(ctx, record.NewFakeRecorder(1000)) + + if got := GetEventRecorder(ctx); got == nil { + t.Error("GetEventRecorder() = nil, wanted non-nil") + } +} diff --git a/reconciler/testing/context.go b/reconciler/testing/context.go new file mode 100644 index 000000000..f9692c810 --- /dev/null +++ b/reconciler/testing/context.go @@ -0,0 +1,35 @@ +/* +Copyright 2019 The Knative Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "context" + "testing" + + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/record" + + "github.com/knative/pkg/controller" + "github.com/knative/pkg/injection" + logtesting "github.com/knative/pkg/logging/testing" +) + +func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) { + ctx := logtesting.TestContextWithLogger(t) + ctx = controller.WithEventRecorder(ctx, record.NewFakeRecorder(1000)) + return injection.Fake.SetupInformers(ctx, &rest.Config{}) +}