Push the event recorder stuff into knative/pkg (#443)

This commit is contained in:
Matt Moore 2019-06-06 07:35:40 -07:00 committed by Knative Prow Robot
parent 262650b2c0
commit c6f03fa600
3 changed files with 70 additions and 0 deletions

View File

@ -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)
}

View File

@ -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")
}
}

View File

@ -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{})
}