diff --git a/_examples/apiserver/informers/externalversions/example/interface.go b/_examples/apiserver/informers/externalversions/example/interface.go index 2d0d7b31..74626cd1 100644 --- a/_examples/apiserver/informers/externalversions/example/interface.go +++ b/_examples/apiserver/informers/externalversions/example/interface.go @@ -30,15 +30,17 @@ type Interface interface { } type group struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &group{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // V1 returns a new v1.Interface. func (g *group) V1() v1.Interface { - return v1.New(g.SharedInformerFactory) + return v1.New(g.factory, g.namespace, g.tweakListOptions) } diff --git a/_examples/apiserver/informers/externalversions/example/v1/interface.go b/_examples/apiserver/informers/externalversions/example/v1/interface.go index 2ebac67f..613797ae 100644 --- a/_examples/apiserver/informers/externalversions/example/v1/interface.go +++ b/_examples/apiserver/informers/externalversions/example/v1/interface.go @@ -29,15 +29,17 @@ type Interface interface { } type version struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &version{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // TestTypes returns a TestTypeInformer. func (v *version) TestTypes() TestTypeInformer { - return &testTypeInformer{factory: v.SharedInformerFactory} + return &testTypeInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} } diff --git a/_examples/apiserver/informers/externalversions/example/v1/testtype.go b/_examples/apiserver/informers/externalversions/example/v1/testtype.go index 55782d81..e6c7fca3 100644 --- a/_examples/apiserver/informers/externalversions/example/v1/testtype.go +++ b/_examples/apiserver/informers/externalversions/example/v1/testtype.go @@ -38,19 +38,34 @@ type TestTypeInformer interface { } type testTypeInformer struct { - factory internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc + namespace string } // NewTestTypeInformer constructs a new informer for TestType type. // Always prefer using an informer factory to get a shared informer instead of getting an independent // one. This reduces memory footprint and number of connections to the server. func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, namespace, resyncPeriod, indexers, nil) +} + +// NewFilteredTestTypeInformer constructs a new informer for TestType type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFilteredTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { return cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.ExampleV1().TestTypes(namespace).List(options) }, WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.ExampleV1().TestTypes(namespace).Watch(options) }, }, @@ -60,12 +75,12 @@ func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPer ) } -func defaultTestTypeInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { - return NewTestTypeInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) +func (f *testTypeInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) } func (f *testTypeInformer) Informer() cache.SharedIndexInformer { - return f.factory.InformerFor(&example_v1.TestType{}, defaultTestTypeInformer) + return f.factory.InformerFor(&example_v1.TestType{}, f.defaultInformer) } func (f *testTypeInformer) Lister() v1.TestTypeLister { diff --git a/_examples/apiserver/informers/externalversions/factory.go b/_examples/apiserver/informers/externalversions/factory.go index cfa88f8e..9ff55645 100644 --- a/_examples/apiserver/informers/externalversions/factory.go +++ b/_examples/apiserver/informers/externalversions/factory.go @@ -19,6 +19,7 @@ limitations under the License. package externalversions import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" cache "k8s.io/client-go/tools/cache" @@ -31,9 +32,11 @@ import ( ) type sharedInformerFactory struct { - client versioned.Interface - lock sync.Mutex - defaultResync time.Duration + client versioned.Interface + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc + lock sync.Mutex + defaultResync time.Duration informers map[reflect.Type]cache.SharedIndexInformer // startedInformers is used for tracking which informers have been started. @@ -43,8 +46,17 @@ type sharedInformerFactory struct { // NewSharedInformerFactory constructs a new instance of sharedInformerFactory func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory { + return NewFilteredSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil) +} + +// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. +// Listers obtained via this SharedInformerFactory will be subject to the same filters +// as specified here. +func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { return &sharedInformerFactory{ client: client, + namespace: namespace, + tweakListOptions: tweakListOptions, defaultResync: defaultResync, informers: make(map[reflect.Type]cache.SharedIndexInformer), startedInformers: make(map[reflect.Type]bool), @@ -114,5 +126,5 @@ type SharedInformerFactory interface { } func (f *sharedInformerFactory) Example() example.Interface { - return example.New(f) + return example.New(f, f.namespace, f.tweakListOptions) } diff --git a/_examples/apiserver/informers/externalversions/internalinterfaces/factory_interfaces.go b/_examples/apiserver/informers/externalversions/internalinterfaces/factory_interfaces.go index 32b2fdd8..dd26ca97 100644 --- a/_examples/apiserver/informers/externalversions/internalinterfaces/factory_interfaces.go +++ b/_examples/apiserver/informers/externalversions/internalinterfaces/factory_interfaces.go @@ -19,6 +19,7 @@ limitations under the License. package internalinterfaces import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" cache "k8s.io/client-go/tools/cache" versioned "k8s.io/code-generator/_examples/apiserver/clientset/versioned" @@ -32,3 +33,5 @@ type SharedInformerFactory interface { Start(stopCh <-chan struct{}) InformerFor(obj runtime.Object, newFunc NewInformerFunc) cache.SharedIndexInformer } + +type TweakListOptionsFunc func(*v1.ListOptions) diff --git a/_examples/apiserver/informers/internalversion/example/interface.go b/_examples/apiserver/informers/internalversion/example/interface.go index 5fd9bf09..646527b5 100644 --- a/_examples/apiserver/informers/internalversion/example/interface.go +++ b/_examples/apiserver/informers/internalversion/example/interface.go @@ -30,15 +30,17 @@ type Interface interface { } type group struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &group{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // InternalVersion returns a new internalversion.Interface. func (g *group) InternalVersion() internalversion.Interface { - return internalversion.New(g.SharedInformerFactory) + return internalversion.New(g.factory, g.namespace, g.tweakListOptions) } diff --git a/_examples/apiserver/informers/internalversion/example/internalversion/interface.go b/_examples/apiserver/informers/internalversion/example/internalversion/interface.go index 44aefe00..5fb90301 100644 --- a/_examples/apiserver/informers/internalversion/example/internalversion/interface.go +++ b/_examples/apiserver/informers/internalversion/example/internalversion/interface.go @@ -29,15 +29,17 @@ type Interface interface { } type version struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &version{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // TestTypes returns a TestTypeInformer. func (v *version) TestTypes() TestTypeInformer { - return &testTypeInformer{factory: v.SharedInformerFactory} + return &testTypeInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} } diff --git a/_examples/apiserver/informers/internalversion/example/internalversion/testtype.go b/_examples/apiserver/informers/internalversion/example/internalversion/testtype.go index 7480db52..c25308cf 100644 --- a/_examples/apiserver/informers/internalversion/example/internalversion/testtype.go +++ b/_examples/apiserver/informers/internalversion/example/internalversion/testtype.go @@ -38,19 +38,34 @@ type TestTypeInformer interface { } type testTypeInformer struct { - factory internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc + namespace string } // NewTestTypeInformer constructs a new informer for TestType type. // Always prefer using an informer factory to get a shared informer instead of getting an independent // one. This reduces memory footprint and number of connections to the server. func NewTestTypeInformer(client clientset_internalversion.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, namespace, resyncPeriod, indexers, nil) +} + +// NewFilteredTestTypeInformer constructs a new informer for TestType type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFilteredTestTypeInformer(client clientset_internalversion.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { return cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options v1.ListOptions) (runtime.Object, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.Example().TestTypes(namespace).List(options) }, WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.Example().TestTypes(namespace).Watch(options) }, }, @@ -60,12 +75,12 @@ func NewTestTypeInformer(client clientset_internalversion.Interface, namespace s ) } -func defaultTestTypeInformer(client clientset_internalversion.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { - return NewTestTypeInformer(client, v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) +func (f *testTypeInformer) defaultInformer(client clientset_internalversion.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) } func (f *testTypeInformer) Informer() cache.SharedIndexInformer { - return f.factory.InformerFor(&example.TestType{}, defaultTestTypeInformer) + return f.factory.InformerFor(&example.TestType{}, f.defaultInformer) } func (f *testTypeInformer) Lister() internalversion.TestTypeLister { diff --git a/_examples/apiserver/informers/internalversion/factory.go b/_examples/apiserver/informers/internalversion/factory.go index f3dc2bc5..3f1aab3d 100644 --- a/_examples/apiserver/informers/internalversion/factory.go +++ b/_examples/apiserver/informers/internalversion/factory.go @@ -19,6 +19,7 @@ limitations under the License. package internalversion import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" cache "k8s.io/client-go/tools/cache" @@ -31,9 +32,11 @@ import ( ) type sharedInformerFactory struct { - client internalversion.Interface - lock sync.Mutex - defaultResync time.Duration + client internalversion.Interface + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc + lock sync.Mutex + defaultResync time.Duration informers map[reflect.Type]cache.SharedIndexInformer // startedInformers is used for tracking which informers have been started. @@ -43,8 +46,17 @@ type sharedInformerFactory struct { // NewSharedInformerFactory constructs a new instance of sharedInformerFactory func NewSharedInformerFactory(client internalversion.Interface, defaultResync time.Duration) SharedInformerFactory { + return NewFilteredSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil) +} + +// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. +// Listers obtained via this SharedInformerFactory will be subject to the same filters +// as specified here. +func NewFilteredSharedInformerFactory(client internalversion.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { return &sharedInformerFactory{ client: client, + namespace: namespace, + tweakListOptions: tweakListOptions, defaultResync: defaultResync, informers: make(map[reflect.Type]cache.SharedIndexInformer), startedInformers: make(map[reflect.Type]bool), @@ -114,5 +126,5 @@ type SharedInformerFactory interface { } func (f *sharedInformerFactory) Example() example.Interface { - return example.New(f) + return example.New(f, f.namespace, f.tweakListOptions) } diff --git a/_examples/apiserver/informers/internalversion/internalinterfaces/factory_interfaces.go b/_examples/apiserver/informers/internalversion/internalinterfaces/factory_interfaces.go index 8548eed6..52c5ba46 100644 --- a/_examples/apiserver/informers/internalversion/internalinterfaces/factory_interfaces.go +++ b/_examples/apiserver/informers/internalversion/internalinterfaces/factory_interfaces.go @@ -19,6 +19,7 @@ limitations under the License. package internalinterfaces import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" cache "k8s.io/client-go/tools/cache" internalversion "k8s.io/code-generator/_examples/apiserver/clientset/internalversion" @@ -32,3 +33,5 @@ type SharedInformerFactory interface { Start(stopCh <-chan struct{}) InformerFor(obj runtime.Object, newFunc NewInformerFunc) cache.SharedIndexInformer } + +type TweakListOptionsFunc func(*v1.ListOptions) diff --git a/_examples/crd/informers/externalversions/example/interface.go b/_examples/crd/informers/externalversions/example/interface.go index 71c8802c..26d9165d 100644 --- a/_examples/crd/informers/externalversions/example/interface.go +++ b/_examples/crd/informers/externalversions/example/interface.go @@ -30,15 +30,17 @@ type Interface interface { } type group struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &group{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // V1 returns a new v1.Interface. func (g *group) V1() v1.Interface { - return v1.New(g.SharedInformerFactory) + return v1.New(g.factory, g.namespace, g.tweakListOptions) } diff --git a/_examples/crd/informers/externalversions/example/v1/interface.go b/_examples/crd/informers/externalversions/example/v1/interface.go index 39bc0bcc..ff4e55ec 100644 --- a/_examples/crd/informers/externalversions/example/v1/interface.go +++ b/_examples/crd/informers/externalversions/example/v1/interface.go @@ -29,15 +29,17 @@ type Interface interface { } type version struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &version{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // TestTypes returns a TestTypeInformer. func (v *version) TestTypes() TestTypeInformer { - return &testTypeInformer{factory: v.SharedInformerFactory} + return &testTypeInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} } diff --git a/_examples/crd/informers/externalversions/example/v1/testtype.go b/_examples/crd/informers/externalversions/example/v1/testtype.go index a745adad..e8e305df 100644 --- a/_examples/crd/informers/externalversions/example/v1/testtype.go +++ b/_examples/crd/informers/externalversions/example/v1/testtype.go @@ -38,19 +38,34 @@ type TestTypeInformer interface { } type testTypeInformer struct { - factory internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc + namespace string } // NewTestTypeInformer constructs a new informer for TestType type. // Always prefer using an informer factory to get a shared informer instead of getting an independent // one. This reduces memory footprint and number of connections to the server. func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, namespace, resyncPeriod, indexers, nil) +} + +// NewFilteredTestTypeInformer constructs a new informer for TestType type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFilteredTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { return cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.ExampleV1().TestTypes(namespace).List(options) }, WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.ExampleV1().TestTypes(namespace).Watch(options) }, }, @@ -60,12 +75,12 @@ func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPer ) } -func defaultTestTypeInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { - return NewTestTypeInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) +func (f *testTypeInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) } func (f *testTypeInformer) Informer() cache.SharedIndexInformer { - return f.factory.InformerFor(&example_v1.TestType{}, defaultTestTypeInformer) + return f.factory.InformerFor(&example_v1.TestType{}, f.defaultInformer) } func (f *testTypeInformer) Lister() v1.TestTypeLister { diff --git a/_examples/crd/informers/externalversions/example2/interface.go b/_examples/crd/informers/externalversions/example2/interface.go index 8b349db7..eeace710 100644 --- a/_examples/crd/informers/externalversions/example2/interface.go +++ b/_examples/crd/informers/externalversions/example2/interface.go @@ -30,15 +30,17 @@ type Interface interface { } type group struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &group{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // V1 returns a new v1.Interface. func (g *group) V1() v1.Interface { - return v1.New(g.SharedInformerFactory) + return v1.New(g.factory, g.namespace, g.tweakListOptions) } diff --git a/_examples/crd/informers/externalversions/example2/v1/interface.go b/_examples/crd/informers/externalversions/example2/v1/interface.go index 39bc0bcc..ff4e55ec 100644 --- a/_examples/crd/informers/externalversions/example2/v1/interface.go +++ b/_examples/crd/informers/externalversions/example2/v1/interface.go @@ -29,15 +29,17 @@ type Interface interface { } type version struct { - internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc } // New returns a new Interface. -func New(f internalinterfaces.SharedInformerFactory) Interface { - return &version{f} +func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface { + return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } // TestTypes returns a TestTypeInformer. func (v *version) TestTypes() TestTypeInformer { - return &testTypeInformer{factory: v.SharedInformerFactory} + return &testTypeInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} } diff --git a/_examples/crd/informers/externalversions/example2/v1/testtype.go b/_examples/crd/informers/externalversions/example2/v1/testtype.go index dc4c430c..11ca2b9b 100644 --- a/_examples/crd/informers/externalversions/example2/v1/testtype.go +++ b/_examples/crd/informers/externalversions/example2/v1/testtype.go @@ -38,19 +38,34 @@ type TestTypeInformer interface { } type testTypeInformer struct { - factory internalinterfaces.SharedInformerFactory + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc + namespace string } // NewTestTypeInformer constructs a new informer for TestType type. // Always prefer using an informer factory to get a shared informer instead of getting an independent // one. This reduces memory footprint and number of connections to the server. func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, namespace, resyncPeriod, indexers, nil) +} + +// NewFilteredTestTypeInformer constructs a new informer for TestType type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFilteredTestTypeInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { return cache.NewSharedIndexInformer( &cache.ListWatch{ ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.SecondExampleV1().TestTypes(namespace).List(options) }, WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.SecondExampleV1().TestTypes(namespace).Watch(options) }, }, @@ -60,12 +75,12 @@ func NewTestTypeInformer(client versioned.Interface, namespace string, resyncPer ) } -func defaultTestTypeInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { - return NewTestTypeInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) +func (f *testTypeInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredTestTypeInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) } func (f *testTypeInformer) Informer() cache.SharedIndexInformer { - return f.factory.InformerFor(&example2_v1.TestType{}, defaultTestTypeInformer) + return f.factory.InformerFor(&example2_v1.TestType{}, f.defaultInformer) } func (f *testTypeInformer) Lister() v1.TestTypeLister { diff --git a/_examples/crd/informers/externalversions/factory.go b/_examples/crd/informers/externalversions/factory.go index 36a747ad..ff06d7c3 100644 --- a/_examples/crd/informers/externalversions/factory.go +++ b/_examples/crd/informers/externalversions/factory.go @@ -19,6 +19,7 @@ limitations under the License. package externalversions import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" schema "k8s.io/apimachinery/pkg/runtime/schema" cache "k8s.io/client-go/tools/cache" @@ -32,9 +33,11 @@ import ( ) type sharedInformerFactory struct { - client versioned.Interface - lock sync.Mutex - defaultResync time.Duration + client versioned.Interface + namespace string + tweakListOptions internalinterfaces.TweakListOptionsFunc + lock sync.Mutex + defaultResync time.Duration informers map[reflect.Type]cache.SharedIndexInformer // startedInformers is used for tracking which informers have been started. @@ -44,8 +47,17 @@ type sharedInformerFactory struct { // NewSharedInformerFactory constructs a new instance of sharedInformerFactory func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory { + return NewFilteredSharedInformerFactory(client, defaultResync, v1.NamespaceAll, nil) +} + +// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. +// Listers obtained via this SharedInformerFactory will be subject to the same filters +// as specified here. +func NewFilteredSharedInformerFactory(client versioned.Interface, defaultResync time.Duration, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerFactory { return &sharedInformerFactory{ client: client, + namespace: namespace, + tweakListOptions: tweakListOptions, defaultResync: defaultResync, informers: make(map[reflect.Type]cache.SharedIndexInformer), startedInformers: make(map[reflect.Type]bool), @@ -116,9 +128,9 @@ type SharedInformerFactory interface { } func (f *sharedInformerFactory) Example() example.Interface { - return example.New(f) + return example.New(f, f.namespace, f.tweakListOptions) } func (f *sharedInformerFactory) SecondExample() example2.Interface { - return example2.New(f) + return example2.New(f, f.namespace, f.tweakListOptions) } diff --git a/_examples/crd/informers/externalversions/internalinterfaces/factory_interfaces.go b/_examples/crd/informers/externalversions/internalinterfaces/factory_interfaces.go index 8a94fce3..18b364af 100644 --- a/_examples/crd/informers/externalversions/internalinterfaces/factory_interfaces.go +++ b/_examples/crd/informers/externalversions/internalinterfaces/factory_interfaces.go @@ -19,6 +19,7 @@ limitations under the License. package internalinterfaces import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" cache "k8s.io/client-go/tools/cache" versioned "k8s.io/code-generator/_examples/crd/clientset/versioned" @@ -32,3 +33,5 @@ type SharedInformerFactory interface { Start(stopCh <-chan struct{}) InformerFor(obj runtime.Object, newFunc NewInformerFunc) cache.SharedIndexInformer } + +type TweakListOptionsFunc func(*v1.ListOptions) diff --git a/cmd/informer-gen/generators/factory.go b/cmd/informer-gen/generators/factory.go index 593f5334..2a91aaa9 100644 --- a/cmd/informer-gen/generators/factory.go +++ b/cmd/informer-gen/generators/factory.go @@ -74,19 +74,21 @@ func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w i gvNewFuncs[groupPkgName] = c.Universe.Function(types.Name{Package: path.Join(g.outputPackage, groupPkgName), Name: "New"}) } m := map[string]interface{}{ - "cacheSharedIndexInformer": c.Universe.Type(cacheSharedIndexInformer), - "groupVersions": g.groupVersions, - "gvInterfaces": gvInterfaces, - "gvNewFuncs": gvNewFuncs, - "gvGoNames": g.gvGoNames, - "interfacesNewInformerFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "NewInformerFunc"}), - "informerFactoryInterface": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}), - "clientSetInterface": c.Universe.Type(types.Name{Package: g.clientSetPackage, Name: "Interface"}), - "reflectType": c.Universe.Type(reflectType), - "runtimeObject": c.Universe.Type(runtimeObject), - "schemaGroupVersionResource": c.Universe.Type(schemaGroupVersionResource), - "syncMutex": c.Universe.Type(syncMutex), - "timeDuration": c.Universe.Type(timeDuration), + "cacheSharedIndexInformer": c.Universe.Type(cacheSharedIndexInformer), + "groupVersions": g.groupVersions, + "gvInterfaces": gvInterfaces, + "gvNewFuncs": gvNewFuncs, + "gvGoNames": g.gvGoNames, + "interfacesNewInformerFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "NewInformerFunc"}), + "interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}), + "informerFactoryInterface": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}), + "clientSetInterface": c.Universe.Type(types.Name{Package: g.clientSetPackage, Name: "Interface"}), + "reflectType": c.Universe.Type(reflectType), + "runtimeObject": c.Universe.Type(runtimeObject), + "schemaGroupVersionResource": c.Universe.Type(schemaGroupVersionResource), + "syncMutex": c.Universe.Type(syncMutex), + "timeDuration": c.Universe.Type(timeDuration), + "namespaceAll": c.Universe.Type(metav1NamespaceAll), } sw.Do(sharedInformerFactoryStruct, m) @@ -98,6 +100,8 @@ func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w i var sharedInformerFactoryStruct = ` type sharedInformerFactory struct { client {{.clientSetInterface|raw}} + namespace string + tweakListOptions {{.interfacesTweakListOptionsFunc|raw}} lock {{.syncMutex|raw}} defaultResync {{.timeDuration|raw}} @@ -109,8 +113,17 @@ type sharedInformerFactory struct { // NewSharedInformerFactory constructs a new instance of sharedInformerFactory func NewSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}) SharedInformerFactory { + return NewFilteredSharedInformerFactory(client, defaultResync, {{.namespaceAll|raw}}, nil) +} + +// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory. +// Listers obtained via this SharedInformerFactory will be subject to the same filters +// as specified here. +func NewFilteredSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}, namespace string, tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}) SharedInformerFactory { return &sharedInformerFactory{ - client: client, + client: client, + namespace: namespace, + tweakListOptions: tweakListOptions, defaultResync: defaultResync, informers: make(map[{{.reflectType|raw}}]{{.cacheSharedIndexInformer|raw}}), startedInformers: make(map[{{.reflectType|raw}}]bool), @@ -189,7 +202,7 @@ type SharedInformerFactory interface { {{$gvGoNames := .gvGoNames}} {{range $groupPkgName, $group := .groupVersions}} func (f *sharedInformerFactory) {{index $gvGoNames $groupPkgName}}() {{index $gvInterfaces $groupPkgName|raw}} { - return {{index $gvNewFuncs $groupPkgName|raw}}(f) + return {{index $gvNewFuncs $groupPkgName|raw}}(f, f.namespace, f.tweakListOptions) } {{end}} ` diff --git a/cmd/informer-gen/generators/factoryinterface.go b/cmd/informer-gen/generators/factoryinterface.go index 011fea20..c7818043 100644 --- a/cmd/informer-gen/generators/factoryinterface.go +++ b/cmd/informer-gen/generators/factoryinterface.go @@ -67,6 +67,7 @@ func (g *factoryInterfaceGenerator) GenerateType(c *generator.Context, t *types. "clientSetPackage": c.Universe.Type(types.Name{Package: g.clientSetPackage, Name: "Interface"}), "runtimeObject": c.Universe.Type(runtimeObject), "timeDuration": c.Universe.Type(timeDuration), + "v1ListOptions": c.Universe.Type(v1ListOptions), } sw.Do(externalSharedInformerFactoryInterface, m) @@ -82,4 +83,6 @@ type SharedInformerFactory interface { Start(stopCh <-chan struct{}) InformerFor(obj {{.runtimeObject|raw}}, newFunc NewInformerFunc) {{.cacheSharedIndexInformer|raw}} } + +type TweakListOptionsFunc func(*{{.v1ListOptions|raw}}) ` diff --git a/cmd/informer-gen/generators/groupinterface.go b/cmd/informer-gen/generators/groupinterface.go index bc6468ea..253b79f3 100644 --- a/cmd/informer-gen/generators/groupinterface.go +++ b/cmd/informer-gen/generators/groupinterface.go @@ -79,6 +79,7 @@ func (g *groupInterfaceGenerator) GenerateType(c *generator.Context, t *types.Ty }) } m := map[string]interface{}{ + "interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}), "interfacesSharedInformerFactory": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}), "versions": versions, } @@ -98,18 +99,20 @@ type Interface interface { } type group struct { - $.interfacesSharedInformerFactory|raw$ + factory $.interfacesSharedInformerFactory|raw$ + namespace string + tweakListOptions $.interfacesTweakListOptionsFunc|raw$ } // New returns a new Interface. -func New(f $.interfacesSharedInformerFactory|raw$) Interface { - return &group{f} +func New(f $.interfacesSharedInformerFactory|raw$, namespace string, tweakListOptions $.interfacesTweakListOptionsFunc|raw$) Interface { + return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } $range .versions$ // $.Name$ returns a new $.Interface|raw$. func (g *group) $.Name$() $.Interface|raw$ { - return $.New|raw$(g.SharedInformerFactory) + return $.New|raw$(g.factory, g.namespace, g.tweakListOptions) } $end$ ` diff --git a/cmd/informer-gen/generators/informer.go b/cmd/informer-gen/generators/informer.go index 57c4c63f..22b09fd5 100644 --- a/cmd/informer-gen/generators/informer.go +++ b/cmd/informer-gen/generators/informer.go @@ -88,6 +88,7 @@ func (g *informerGenerator) GenerateType(c *generator.Context, t *types.Type, w "clientSetInterface": clientSetInterface, "group": namer.IC(g.groupGoName), "informerFor": informerFor, + "interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}), "interfacesSharedInformerFactory": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}), "listOptions": c.Universe.Type(listOptions), "lister": c.Universe.Type(types.Name{Package: listerPackage, Name: t.Name.Name + "Lister"}), @@ -105,6 +106,7 @@ func (g *informerGenerator) GenerateType(c *generator.Context, t *types.Type, w sw.Do(typeInformerInterface, m) sw.Do(typeInformerStruct, m) sw.Do(typeInformerPublicConstructor, m) + sw.Do(typeFilteredInformerPublicConstructor, m) sw.Do(typeInformerConstructor, m) sw.Do(typeInformerInformer, m) sw.Do(typeInformerLister, m) @@ -124,6 +126,8 @@ type $.type|public$Informer interface { var typeInformerStruct = ` type $.type|private$Informer struct { factory $.interfacesSharedInformerFactory|raw$ + tweakListOptions $.interfacesTweakListOptionsFunc|raw$ + $if .namespaced$namespace string$end$ } ` @@ -132,12 +136,27 @@ var typeInformerPublicConstructor = ` // Always prefer using an informer factory to get a shared informer instead of getting an independent // one. This reduces memory footprint and number of connections to the server. func New$.type|public$Informer(client $.clientSetInterface|raw$$if .namespaced$, namespace string$end$, resyncPeriod $.timeDuration|raw$, indexers $.cacheIndexers|raw$) $.cacheSharedIndexInformer|raw$ { + return NewFiltered$.type|public$Informer(client$if .namespaced$, namespace$end$, resyncPeriod, indexers, nil) +} +` + +var typeFilteredInformerPublicConstructor = ` +// NewFiltered$.type|public$Informer constructs a new informer for $.type|public$ type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFiltered$.type|public$Informer(client $.clientSetInterface|raw$$if .namespaced$, namespace string$end$, resyncPeriod $.timeDuration|raw$, indexers $.cacheIndexers|raw$, tweakListOptions $.interfacesTweakListOptionsFunc|raw$) $.cacheSharedIndexInformer|raw$ { return $.cacheNewSharedIndexInformer|raw$( &$.cacheListWatch|raw${ ListFunc: func(options $.v1ListOptions|raw$) ($.runtimeObject|raw$, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.$.group$$.version$().$.type|publicPlural$($if .namespaced$namespace$end$).List(options) }, WatchFunc: func(options $.v1ListOptions|raw$) ($.watchInterface|raw$, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } return client.$.group$$.version$().$.type|publicPlural$($if .namespaced$namespace$end$).Watch(options) }, }, @@ -149,14 +168,14 @@ func New$.type|public$Informer(client $.clientSetInterface|raw$$if .namespaced$, ` var typeInformerConstructor = ` -func default$.type|public$Informer(client $.clientSetInterface|raw$, resyncPeriod $.timeDuration|raw$) $.cacheSharedIndexInformer|raw$ { - return New$.type|public$Informer(client, $if .namespaced$$.namespaceAll|raw$, $end$resyncPeriod, $.cacheIndexers|raw${$.cacheNamespaceIndex|raw$: $.cacheMetaNamespaceIndexFunc|raw$}) +func (f *$.type|private$Informer) defaultInformer(client $.clientSetInterface|raw$, resyncPeriod $.timeDuration|raw$) $.cacheSharedIndexInformer|raw$ { + return NewFiltered$.type|public$Informer(client$if .namespaced$, f.namespace$end$, resyncPeriod, $.cacheIndexers|raw${$.cacheNamespaceIndex|raw$: $.cacheMetaNamespaceIndexFunc|raw$}, f.tweakListOptions) } ` var typeInformerInformer = ` func (f *$.type|private$Informer) Informer() $.cacheSharedIndexInformer|raw$ { - return f.factory.$.informerFor$(&$.type|raw${}, default$.type|public$Informer) + return f.factory.$.informerFor$(&$.type|raw${}, f.defaultInformer) } ` diff --git a/cmd/informer-gen/generators/versioninterface.go b/cmd/informer-gen/generators/versioninterface.go index 23f7286f..1cd27d5c 100644 --- a/cmd/informer-gen/generators/versioninterface.go +++ b/cmd/informer-gen/generators/versioninterface.go @@ -22,6 +22,8 @@ import ( "k8s.io/gengo/generator" "k8s.io/gengo/namer" "k8s.io/gengo/types" + + "k8s.io/code-generator/cmd/client-gen/generators/util" ) // versionInterfaceGenerator generates the per-version interface file. @@ -59,11 +61,21 @@ func (g *versionInterfaceGenerator) GenerateType(c *generator.Context, t *types. sw := generator.NewSnippetWriter(w, c, "$", "$") m := map[string]interface{}{ + "interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}), "interfacesSharedInformerFactory": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}), "types": g.types, } sw.Do(versionTemplate, m) + for _, typeDef := range g.types { + tags, err := util.ParseClientGenTags(typeDef.SecondClosestCommentLines) + if err != nil { + return err + } + m["namespaced"] = !tags.NonNamespaced + m["type"] = typeDef + sw.Do(versionFuncTemplate, m) + } return sw.Error() } @@ -78,18 +90,20 @@ type Interface interface { } type version struct { - $.interfacesSharedInformerFactory|raw$ + factory $.interfacesSharedInformerFactory|raw$ + namespace string + tweakListOptions $.interfacesTweakListOptionsFunc|raw$ } // New returns a new Interface. -func New(f $.interfacesSharedInformerFactory|raw$) Interface { - return &version{f} +func New(f $.interfacesSharedInformerFactory|raw$, namespace string, tweakListOptions $.interfacesTweakListOptionsFunc|raw$) Interface { + return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } +` -$range .types$ -// $.|publicPlural$ returns a $.|public$Informer. -func (v *version) $.|publicPlural$() $.|public$Informer { - return &$.|private$Informer{factory: v.SharedInformerFactory} +var versionFuncTemplate = ` +// $.type|publicPlural$ returns a $.type|public$Informer. +func (v *version) $.type|publicPlural$() $.type|public$Informer { + return &$.type|private$Informer{factory: v.factory$if .namespaced$, namespace: v.namespace$end$, tweakListOptions: v.tweakListOptions} } -$end$ `