diff --git a/cmd/controller/main.go b/cmd/controller/main.go index d257e822821..d45483d8d9c 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -23,11 +23,13 @@ import ( "os" "github.com/tektoncd/pipeline/pkg/apis/pipeline" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun" "github.com/tektoncd/pipeline/pkg/reconciler/taskrun" "github.com/tektoncd/pipeline/pkg/version" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/rest" + filteredinformerfactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered" "knative.dev/pkg/controller" "knative.dev/pkg/injection" "knative.dev/pkg/injection/sharedmain" @@ -109,6 +111,7 @@ func main() { log.Fatal(http.ListenAndServe(":"+port, mux)) }() + ctx = filteredinformerfactory.WithSelectors(ctx, v1beta1.ManagedByLabelKey) sharedmain.MainWithConfig(ctx, ControllerLogKey, cfg, taskrun.NewController(*namespace, images), pipelinerun.NewController(*namespace, images), diff --git a/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go b/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go index cc532f7870d..3adc7e175a1 100644 --- a/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go +++ b/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go @@ -29,7 +29,7 @@ import ( var _ apis.Defaultable = (*TaskRun)(nil) -const managedByLabelKey = "app.kubernetes.io/managed-by" +const ManagedByLabelKey = "app.kubernetes.io/managed-by" func (tr *TaskRun) SetDefaults(ctx context.Context) { ctx = apis.WithinParent(ctx, tr.ObjectMeta) @@ -41,8 +41,8 @@ func (tr *TaskRun) SetDefaults(ctx context.Context) { if tr.ObjectMeta.Labels == nil { tr.ObjectMeta.Labels = map[string]string{} } - if _, found := tr.ObjectMeta.Labels[managedByLabelKey]; !found { - tr.ObjectMeta.Labels[managedByLabelKey] = cfg.Defaults.DefaultManagedByLabelValue + if _, found := tr.ObjectMeta.Labels[ManagedByLabelKey]; !found { + tr.ObjectMeta.Labels[ManagedByLabelKey] = cfg.Defaults.DefaultManagedByLabelValue } } diff --git a/pkg/apis/pipeline/v1beta1/taskrun_defaults.go b/pkg/apis/pipeline/v1beta1/taskrun_defaults.go index 7dae7ec2f6f..fbfde383e44 100644 --- a/pkg/apis/pipeline/v1beta1/taskrun_defaults.go +++ b/pkg/apis/pipeline/v1beta1/taskrun_defaults.go @@ -27,7 +27,7 @@ import ( var _ apis.Defaultable = (*TaskRun)(nil) -const managedByLabelKey = "app.kubernetes.io/managed-by" +const ManagedByLabelKey = "app.kubernetes.io/managed-by" func (tr *TaskRun) SetDefaults(ctx context.Context) { ctx = apis.WithinParent(ctx, tr.ObjectMeta) @@ -39,8 +39,8 @@ func (tr *TaskRun) SetDefaults(ctx context.Context) { if tr.ObjectMeta.Labels == nil { tr.ObjectMeta.Labels = map[string]string{} } - if _, found := tr.ObjectMeta.Labels[managedByLabelKey]; !found { - tr.ObjectMeta.Labels[managedByLabelKey] = cfg.Defaults.DefaultManagedByLabelValue + if _, found := tr.ObjectMeta.Labels[ManagedByLabelKey]; !found { + tr.ObjectMeta.Labels[ManagedByLabelKey] = cfg.Defaults.DefaultManagedByLabelValue } } diff --git a/pkg/reconciler/pipelinerun/metrics_test.go b/pkg/reconciler/pipelinerun/metrics_test.go index ef4763407f6..5363ab28f44 100644 --- a/pkg/reconciler/pipelinerun/metrics_test.go +++ b/pkg/reconciler/pipelinerun/metrics_test.go @@ -23,13 +23,13 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" fakepipelineruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/pipelinerun/fake" "github.com/tektoncd/pipeline/pkg/names" + ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" "knative.dev/pkg/metrics/metricstest" // Required to setup metrics env for testing _ "knative.dev/pkg/metrics/testing" - rtesting "knative.dev/pkg/reconciler/testing" ) var ( @@ -154,7 +154,7 @@ func TestRecordRunningPipelineRunsCount(t *testing.T) { } } - ctx, _ := rtesting.SetupFakeContext(t) + ctx, _ := ttesting.SetupFakeContext(t) informer := fakepipelineruninformer.Get(ctx) // Add N randomly-named PipelineRuns with differently-succeeded statuses. for _, tr := range []*v1beta1.PipelineRun{ diff --git a/pkg/reconciler/taskrun/controller.go b/pkg/reconciler/taskrun/controller.go index 1258cbc7212..cd5fe381952 100644 --- a/pkg/reconciler/taskrun/controller.go +++ b/pkg/reconciler/taskrun/controller.go @@ -35,7 +35,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/cache" kubeclient "knative.dev/pkg/client/injection/kube/client" - podinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod" + filteredpodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered" "knative.dev/pkg/configmap" "knative.dev/pkg/controller" "knative.dev/pkg/kmeta" @@ -52,7 +52,7 @@ func NewController(namespace string, images pipeline.Images) func(context.Contex taskRunInformer := taskruninformer.Get(ctx) taskInformer := taskinformer.Get(ctx) clusterTaskInformer := clustertaskinformer.Get(ctx) - podInformer := podinformer.Get(ctx) + podInformer := filteredpodinformer.Get(ctx, v1beta1.ManagedByLabelKey) resourceInformer := resourceinformer.Get(ctx) metrics, err := NewRecorder() if err != nil { diff --git a/pkg/reconciler/taskrun/metrics_test.go b/pkg/reconciler/taskrun/metrics_test.go index 09685af4485..292c9a4aa42 100644 --- a/pkg/reconciler/taskrun/metrics_test.go +++ b/pkg/reconciler/taskrun/metrics_test.go @@ -25,13 +25,13 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" faketaskruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/taskrun/fake" "github.com/tektoncd/pipeline/pkg/names" + ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" "knative.dev/pkg/metrics/metricstest" _ "knative.dev/pkg/metrics/testing" - rtesting "knative.dev/pkg/reconciler/testing" ) var ( @@ -243,7 +243,7 @@ func TestRecordRunningTaskRunsCount(t *testing.T) { } } - ctx, _ := rtesting.SetupFakeContext(t) + ctx, _ := ttesting.SetupFakeContext(t) informer := faketaskruninformer.Get(ctx) // Add N randomly-named TaskRuns with differently-succeeded statuses. for _, tr := range []*v1beta1.TaskRun{ diff --git a/pkg/reconciler/testing/logger.go b/pkg/reconciler/testing/logger.go index 53f35362d36..08ca1829e88 100644 --- a/pkg/reconciler/testing/logger.go +++ b/pkg/reconciler/testing/logger.go @@ -4,16 +4,26 @@ import ( "context" "testing" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/record" + filteredinformerfactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered" + "knative.dev/pkg/injection" + logtesting "knative.dev/pkg/logging/testing" + "github.com/tektoncd/pipeline/pkg/reconciler/events/cloudevent" "go.uber.org/zap" "go.uber.org/zap/zaptest" "knative.dev/pkg/controller" "knative.dev/pkg/logging" - rtesting "knative.dev/pkg/reconciler/testing" + + // Import for creating fake filtered factory in the test + _ "knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake" ) +// SetupFakeContext sets up the the Context and the fake filtered informers for the tests. func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) { - ctx, informer := rtesting.SetupFakeContext(t) + ctx, _, informer := SetupFakeContextWithLabelKey(t) cloudEventClientBehaviour := cloudevent.FakeClientBehaviour{ SendSuccessfully: true, } @@ -21,11 +31,23 @@ func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) { return WithLogger(ctx, t), informer } +// WithLogger returns the the Logger func WithLogger(ctx context.Context, t *testing.T) context.Context { return logging.WithLogger(ctx, TestLogger(t)) } +// TestLogger sets up the the Logger func TestLogger(t *testing.T) *zap.SugaredLogger { logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())) return logger.Sugar().Named(t.Name()) } + +// SetupFakeContextWithLabelKey sets up the the Context and the fake informers for the tests +// The provided context includes the FilteredInformerFactory LabelKey. +func SetupFakeContextWithLabelKey(t zaptest.TestingT) (context.Context, context.CancelFunc, []controller.Informer) { + ctx, c := context.WithCancel(logtesting.TestContextWithLogger(t)) + ctx = controller.WithEventRecorder(ctx, record.NewFakeRecorder(1000)) + ctx = filteredinformerfactory.WithSelectors(ctx, v1beta1.ManagedByLabelKey) + ctx, is := injection.Fake.SetupInformers(ctx, &rest.Config{}) + return ctx, c, is +} diff --git a/test/controller.go b/test/controller.go index 2ec9d2e5f16..fb97c51611c 100644 --- a/test/controller.go +++ b/test/controller.go @@ -54,7 +54,7 @@ import ( "k8s.io/client-go/tools/record" fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" fakeconfigmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/fake" - fakepodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake" + fakefilteredpodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake" fakeserviceaccountinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount/fake" "knative.dev/pkg/controller" ) @@ -174,7 +174,7 @@ func SeedTestData(t *testing.T, ctx context.Context, d Data) (Clients, Informers ClusterTask: fakeclustertaskinformer.Get(ctx), PipelineResource: fakeresourceinformer.Get(ctx), Condition: fakeconditioninformer.Get(ctx), - Pod: fakepodinformer.Get(ctx), + Pod: fakefilteredpodinformer.Get(ctx, v1beta1.ManagedByLabelKey), ConfigMap: fakeconfigmapinformer.Get(ctx), ServiceAccount: fakeserviceaccountinformer.Get(ctx), } diff --git a/test/v1alpha1/controller.go b/test/v1alpha1/controller.go index c1914c1ce4c..b9472e38669 100644 --- a/test/v1alpha1/controller.go +++ b/test/v1alpha1/controller.go @@ -40,7 +40,7 @@ import ( coreinformers "k8s.io/client-go/informers/core/v1" fakekubeclientset "k8s.io/client-go/kubernetes/fake" fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" - fakepodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake" + fakefilteredpodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake" "knative.dev/pkg/controller" ) @@ -101,7 +101,7 @@ func SeedTestData(t *testing.T, ctx context.Context, d Data) (Clients, Informers ClusterTask: fakeclustertaskinformer.Get(ctx), PipelineResource: fakeresourceinformer.Get(ctx), Condition: fakeconditioninformer.Get(ctx), - Pod: fakepodinformer.Get(ctx), + Pod: fakefilteredpodinformer.Get(ctx, v1alpha1.ManagedByLabelKey), } for _, pr := range d.PipelineRuns { diff --git a/test/v1alpha1/wait_test.go b/test/v1alpha1/wait_test.go index 2c289941716..cacd11431df 100644 --- a/test/v1alpha1/wait_test.go +++ b/test/v1alpha1/wait_test.go @@ -22,11 +22,11 @@ import ( "time" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" - rtesting "knative.dev/pkg/reconciler/testing" ) var ( @@ -101,7 +101,7 @@ func TestWaitForPipelineRunStateFailed(t *testing.T) { } func fakeClients(t *testing.T, d Data) (*clients, context.Context, func()) { - ctx, _ := rtesting.SetupFakeContext(t) + ctx, _ := ttesting.SetupFakeContext(t) ctx, cancel := context.WithCancel(ctx) fakeClients, _ := SeedTestData(t, ctx, d) return &clients{ diff --git a/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake/fake.go b/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake/fake.go deleted file mode 100644 index ae10b34555b..00000000000 --- a/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake/fake.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2020 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. -*/ - -// Code generated by injection-gen. DO NOT EDIT. - -package fake - -import ( - context "context" - - pod "knative.dev/pkg/client/injection/kube/informers/core/v1/pod" - fake "knative.dev/pkg/client/injection/kube/informers/factory/fake" - controller "knative.dev/pkg/controller" - injection "knative.dev/pkg/injection" -) - -var Get = pod.Get - -func init() { - injection.Fake.RegisterInformer(withInformer) -} - -func withInformer(ctx context.Context) (context.Context, controller.Informer) { - f := fake.Get(ctx) - inf := f.Core().V1().Pods() - return context.WithValue(ctx, pod.Key{}, inf), inf.Informer() -} diff --git a/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake/fake.go b/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake/fake.go new file mode 100644 index 00000000000..ccf429100e6 --- /dev/null +++ b/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake/fake.go @@ -0,0 +1,52 @@ +/* +Copyright 2020 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. +*/ + +// Code generated by injection-gen. DO NOT EDIT. + +package fake + +import ( + context "context" + + filtered "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered" + factoryfiltered "knative.dev/pkg/client/injection/kube/informers/factory/filtered" + controller "knative.dev/pkg/controller" + injection "knative.dev/pkg/injection" + logging "knative.dev/pkg/logging" +) + +var Get = filtered.Get + +func init() { + injection.Fake.RegisterFilteredInformers(withInformer) +} + +func withInformer(ctx context.Context) (context.Context, []controller.Informer) { + untyped := ctx.Value(factoryfiltered.LabelKey{}) + if untyped == nil { + logging.FromContext(ctx).Panic( + "Unable to fetch labelkey from context.") + } + labelSelectors := untyped.([]string) + infs := []controller.Informer{} + for _, selector := range labelSelectors { + f := factoryfiltered.Get(ctx, selector) + inf := f.Core().V1().Pods() + ctx = context.WithValue(ctx, filtered.Key{Selector: selector}, inf) + infs = append(infs, inf.Informer()) + } + return ctx, infs +} diff --git a/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/pod.go b/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/pod.go similarity index 55% rename from vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/pod.go rename to vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/pod.go index 8f8a6fffbc7..7668965bd7d 100644 --- a/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/pod.go +++ b/vendor/knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/pod.go @@ -16,37 +16,50 @@ limitations under the License. // Code generated by injection-gen. DO NOT EDIT. -package pod +package filtered import ( context "context" v1 "k8s.io/client-go/informers/core/v1" - factory "knative.dev/pkg/client/injection/kube/informers/factory" + filtered "knative.dev/pkg/client/injection/kube/informers/factory/filtered" controller "knative.dev/pkg/controller" injection "knative.dev/pkg/injection" logging "knative.dev/pkg/logging" ) func init() { - injection.Default.RegisterInformer(withInformer) + injection.Default.RegisterFilteredInformers(withInformer) } // Key is used for associating the Informer inside the context.Context. -type Key struct{} +type Key struct { + Selector string +} -func withInformer(ctx context.Context) (context.Context, controller.Informer) { - f := factory.Get(ctx) - inf := f.Core().V1().Pods() - return context.WithValue(ctx, Key{}, inf), inf.Informer() +func withInformer(ctx context.Context) (context.Context, []controller.Informer) { + untyped := ctx.Value(filtered.LabelKey{}) + if untyped == nil { + logging.FromContext(ctx).Panic( + "Unable to fetch labelkey from context.") + } + labelSelectors := untyped.([]string) + infs := []controller.Informer{} + for _, selector := range labelSelectors { + f := filtered.Get(ctx, selector) + inf := f.Core().V1().Pods() + ctx = context.WithValue(ctx, Key{Selector: selector}, inf) + infs = append(infs, inf.Informer()) + } + return ctx, infs } // Get extracts the typed informer from the context. -func Get(ctx context.Context) v1.PodInformer { - untyped := ctx.Value(Key{}) +func Get(ctx context.Context, selector string) v1.PodInformer { + untyped := ctx.Value(Key{Selector: selector}) if untyped == nil { - logging.FromContext(ctx).Panic( - "Unable to fetch k8s.io/client-go/informers/core/v1.PodInformer from context.") + logging.FromContext(ctx).Panicf( + "Unable to fetch k8s.io/client-go/informers/core/v1.PodInformer with selector %s from context.", selector) } return untyped.(v1.PodInformer) } diff --git a/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake/fake_filtered_factory.go b/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake/fake_filtered_factory.go new file mode 100644 index 00000000000..b22d4515258 --- /dev/null +++ b/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake/fake_filtered_factory.go @@ -0,0 +1,59 @@ +/* +Copyright 2020 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. +*/ + +// Code generated by injection-gen. DO NOT EDIT. + +package fakeFilteredFactory + +import ( + context "context" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + informers "k8s.io/client-go/informers" + fake "knative.dev/pkg/client/injection/kube/client/fake" + filtered "knative.dev/pkg/client/injection/kube/informers/factory/filtered" + controller "knative.dev/pkg/controller" + injection "knative.dev/pkg/injection" + logging "knative.dev/pkg/logging" +) + +var Get = filtered.Get + +func init() { + injection.Fake.RegisterInformerFactory(withInformerFactory) +} + +func withInformerFactory(ctx context.Context) context.Context { + c := fake.Get(ctx) + opts := []informers.SharedInformerOption{} + if injection.HasNamespaceScope(ctx) { + opts = append(opts, informers.WithNamespace(injection.GetNamespaceScope(ctx))) + } + untyped := ctx.Value(filtered.LabelKey{}) + if untyped == nil { + logging.FromContext(ctx).Panic( + "Unable to fetch labelkey from context.") + } + labelSelectors := untyped.([]string) + for _, selector := range labelSelectors { + thisOpts := append(opts, informers.WithTweakListOptions(func(l *v1.ListOptions) { + l.LabelSelector = selector + })) + ctx = context.WithValue(ctx, filtered.Key{Selector: selector}, + informers.NewSharedInformerFactoryWithOptions(c, controller.GetResyncPeriod(ctx), thisOpts...)) + } + return ctx +} diff --git a/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/filtered_factory.go b/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/filtered_factory.go new file mode 100644 index 00000000000..20a91867663 --- /dev/null +++ b/vendor/knative.dev/pkg/client/injection/kube/informers/factory/filtered/filtered_factory.go @@ -0,0 +1,77 @@ +/* +Copyright 2020 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. +*/ + +// Code generated by injection-gen. DO NOT EDIT. + +package filteredFactory + +import ( + context "context" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + informers "k8s.io/client-go/informers" + client "knative.dev/pkg/client/injection/kube/client" + controller "knative.dev/pkg/controller" + injection "knative.dev/pkg/injection" + logging "knative.dev/pkg/logging" +) + +func init() { + injection.Default.RegisterInformerFactory(withInformerFactory) +} + +// Key is used as the key for associating information with a context.Context. +type Key struct { + Selector string +} + +type LabelKey struct{} + +func WithSelectors(ctx context.Context, selector ...string) context.Context { + return context.WithValue(ctx, LabelKey{}, selector) +} + +func withInformerFactory(ctx context.Context) context.Context { + c := client.Get(ctx) + opts := []informers.SharedInformerOption{} + if injection.HasNamespaceScope(ctx) { + opts = append(opts, informers.WithNamespace(injection.GetNamespaceScope(ctx))) + } + untyped := ctx.Value(LabelKey{}) + if untyped == nil { + logging.FromContext(ctx).Panic( + "Unable to fetch labelkey from context.") + } + labelSelectors := untyped.([]string) + for _, selector := range labelSelectors { + thisOpts := append(opts, informers.WithTweakListOptions(func(l *v1.ListOptions) { + l.LabelSelector = selector + })) + ctx = context.WithValue(ctx, Key{Selector: selector}, + informers.NewSharedInformerFactoryWithOptions(c, controller.GetResyncPeriod(ctx), thisOpts...)) + } + return ctx +} + +// Get extracts the InformerFactory from the context. +func Get(ctx context.Context, selector string) informers.SharedInformerFactory { + untyped := ctx.Value(Key{Selector: selector}) + if untyped == nil { + logging.FromContext(ctx).Panicf( + "Unable to fetch k8s.io/client-go/informers.SharedInformerFactory with selector %s from context.", selector) + } + return untyped.(informers.SharedInformerFactory) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 4fe75aede7d..5cc184b7c2d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -978,12 +978,14 @@ knative.dev/pkg/client/injection/kube/informers/admissionregistration/v1/mutatin knative.dev/pkg/client/injection/kube/informers/admissionregistration/v1/validatingwebhookconfiguration knative.dev/pkg/client/injection/kube/informers/core/v1/configmap knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/fake -knative.dev/pkg/client/injection/kube/informers/core/v1/pod -knative.dev/pkg/client/injection/kube/informers/core/v1/pod/fake +knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered +knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount/fake knative.dev/pkg/client/injection/kube/informers/factory knative.dev/pkg/client/injection/kube/informers/factory/fake +knative.dev/pkg/client/injection/kube/informers/factory/filtered +knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake knative.dev/pkg/codegen/cmd/injection-gen knative.dev/pkg/codegen/cmd/injection-gen/args knative.dev/pkg/codegen/cmd/injection-gen/generators