Skip to content

Commit 3cb0094

Browse files
Merge pull request #54660 from munnerz/namespaced-informer-factory
Automatic merge from submit-queue (batch tested with PRs 55403, 54660, 55165). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. code-generator: add NewFilteredSharedInformerFactory function **What this PR does / why we need it**: Adds a `namespace` option to the SharedInformerFactory constructor. This is useful when building controllers that may need to scope themselves to a namespace due to RBAC constraints. Workarounds for this involve losing type safety if a user wants to use it for core APIs as well as a SharedInformerFactory type interface, as we have to deal with plain SharedIndexInformers (example here: https://github.com/jetstack-experimental/cert-manager/blob/master/pkg/util/kube/factory.go) **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # Fixes kubernetes/code-generator#9 **Special notes for your reviewer**: This will require updating all uses of SharedInformerFactory throughout the codebase. I'm going to follow up with later commits in this PR with these changes, but wanted to get this here to get some feedback on the way it's implemented. **Release note**: ```release-note NONE ``` /cc @sttts @nikhita @deads2k Kubernetes-commit: bab312dbcf2ea68c19bffa9f26362c86ef1987ec
2 parents 7fa2992 + 0be304d commit 3cb0094

File tree

98 files changed

+1407
-439
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1407
-439
lines changed

informers/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go_library(
3434
"//vendor/k8s.io/api/settings/v1alpha1:go_default_library",
3535
"//vendor/k8s.io/api/storage/v1:go_default_library",
3636
"//vendor/k8s.io/api/storage/v1beta1:go_default_library",
37+
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
3738
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
3839
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
3940
"//vendor/k8s.io/client-go/informers/admissionregistration:go_default_library",

informers/admissionregistration/interface.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ type Interface interface {
3030
}
3131

3232
type group struct {
33-
internalinterfaces.SharedInformerFactory
33+
factory internalinterfaces.SharedInformerFactory
34+
namespace string
35+
tweakListOptions internalinterfaces.TweakListOptionsFunc
3436
}
3537

3638
// New returns a new Interface.
37-
func New(f internalinterfaces.SharedInformerFactory) Interface {
38-
return &group{f}
39+
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
40+
return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
3941
}
4042

4143
// V1alpha1 returns a new v1alpha1.Interface.
4244
func (g *group) V1alpha1() v1alpha1.Interface {
43-
return v1alpha1.New(g.SharedInformerFactory)
45+
return v1alpha1.New(g.factory, g.namespace, g.tweakListOptions)
4446
}

informers/admissionregistration/v1alpha1/externaladmissionhookconfiguration.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,33 @@ type ExternalAdmissionHookConfigurationInformer interface {
3838
}
3939

4040
type externalAdmissionHookConfigurationInformer struct {
41-
factory internalinterfaces.SharedInformerFactory
41+
factory internalinterfaces.SharedInformerFactory
42+
tweakListOptions internalinterfaces.TweakListOptionsFunc
4243
}
4344

4445
// NewExternalAdmissionHookConfigurationInformer constructs a new informer for ExternalAdmissionHookConfiguration type.
4546
// Always prefer using an informer factory to get a shared informer instead of getting an independent
4647
// one. This reduces memory footprint and number of connections to the server.
4748
func NewExternalAdmissionHookConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
49+
return NewFilteredExternalAdmissionHookConfigurationInformer(client, resyncPeriod, indexers, nil)
50+
}
51+
52+
// NewFilteredExternalAdmissionHookConfigurationInformer constructs a new informer for ExternalAdmissionHookConfiguration type.
53+
// Always prefer using an informer factory to get a shared informer instead of getting an independent
54+
// one. This reduces memory footprint and number of connections to the server.
55+
func NewFilteredExternalAdmissionHookConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
4856
return cache.NewSharedIndexInformer(
4957
&cache.ListWatch{
5058
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
59+
if tweakListOptions != nil {
60+
tweakListOptions(&options)
61+
}
5162
return client.AdmissionregistrationV1alpha1().ExternalAdmissionHookConfigurations().List(options)
5263
},
5364
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
65+
if tweakListOptions != nil {
66+
tweakListOptions(&options)
67+
}
5468
return client.AdmissionregistrationV1alpha1().ExternalAdmissionHookConfigurations().Watch(options)
5569
},
5670
},
@@ -60,12 +74,12 @@ func NewExternalAdmissionHookConfigurationInformer(client kubernetes.Interface,
6074
)
6175
}
6276

63-
func defaultExternalAdmissionHookConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
64-
return NewExternalAdmissionHookConfigurationInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
77+
func (f *externalAdmissionHookConfigurationInformer) defaultInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
78+
return NewFilteredExternalAdmissionHookConfigurationInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
6579
}
6680

6781
func (f *externalAdmissionHookConfigurationInformer) Informer() cache.SharedIndexInformer {
68-
return f.factory.InformerFor(&admissionregistration_v1alpha1.ExternalAdmissionHookConfiguration{}, defaultExternalAdmissionHookConfigurationInformer)
82+
return f.factory.InformerFor(&admissionregistration_v1alpha1.ExternalAdmissionHookConfiguration{}, f.defaultInformer)
6983
}
7084

7185
func (f *externalAdmissionHookConfigurationInformer) Lister() v1alpha1.ExternalAdmissionHookConfigurationLister {

informers/admissionregistration/v1alpha1/initializerconfiguration.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,33 @@ type InitializerConfigurationInformer interface {
3838
}
3939

4040
type initializerConfigurationInformer struct {
41-
factory internalinterfaces.SharedInformerFactory
41+
factory internalinterfaces.SharedInformerFactory
42+
tweakListOptions internalinterfaces.TweakListOptionsFunc
4243
}
4344

4445
// NewInitializerConfigurationInformer constructs a new informer for InitializerConfiguration type.
4546
// Always prefer using an informer factory to get a shared informer instead of getting an independent
4647
// one. This reduces memory footprint and number of connections to the server.
4748
func NewInitializerConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
49+
return NewFilteredInitializerConfigurationInformer(client, resyncPeriod, indexers, nil)
50+
}
51+
52+
// NewFilteredInitializerConfigurationInformer constructs a new informer for InitializerConfiguration type.
53+
// Always prefer using an informer factory to get a shared informer instead of getting an independent
54+
// one. This reduces memory footprint and number of connections to the server.
55+
func NewFilteredInitializerConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
4856
return cache.NewSharedIndexInformer(
4957
&cache.ListWatch{
5058
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
59+
if tweakListOptions != nil {
60+
tweakListOptions(&options)
61+
}
5162
return client.AdmissionregistrationV1alpha1().InitializerConfigurations().List(options)
5263
},
5364
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
65+
if tweakListOptions != nil {
66+
tweakListOptions(&options)
67+
}
5468
return client.AdmissionregistrationV1alpha1().InitializerConfigurations().Watch(options)
5569
},
5670
},
@@ -60,12 +74,12 @@ func NewInitializerConfigurationInformer(client kubernetes.Interface, resyncPeri
6074
)
6175
}
6276

63-
func defaultInitializerConfigurationInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
64-
return NewInitializerConfigurationInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
77+
func (f *initializerConfigurationInformer) defaultInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
78+
return NewFilteredInitializerConfigurationInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
6579
}
6680

6781
func (f *initializerConfigurationInformer) Informer() cache.SharedIndexInformer {
68-
return f.factory.InformerFor(&admissionregistration_v1alpha1.InitializerConfiguration{}, defaultInitializerConfigurationInformer)
82+
return f.factory.InformerFor(&admissionregistration_v1alpha1.InitializerConfiguration{}, f.defaultInformer)
6983
}
7084

7185
func (f *initializerConfigurationInformer) Lister() v1alpha1.InitializerConfigurationLister {

informers/admissionregistration/v1alpha1/interface.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ type Interface interface {
3131
}
3232

3333
type version struct {
34-
internalinterfaces.SharedInformerFactory
34+
factory internalinterfaces.SharedInformerFactory
35+
namespace string
36+
tweakListOptions internalinterfaces.TweakListOptionsFunc
3537
}
3638

3739
// New returns a new Interface.
38-
func New(f internalinterfaces.SharedInformerFactory) Interface {
39-
return &version{f}
40+
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
41+
return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
4042
}
4143

4244
// ExternalAdmissionHookConfigurations returns a ExternalAdmissionHookConfigurationInformer.
4345
func (v *version) ExternalAdmissionHookConfigurations() ExternalAdmissionHookConfigurationInformer {
44-
return &externalAdmissionHookConfigurationInformer{factory: v.SharedInformerFactory}
46+
return &externalAdmissionHookConfigurationInformer{factory: v.factory, tweakListOptions: v.tweakListOptions}
4547
}
4648

4749
// InitializerConfigurations returns a InitializerConfigurationInformer.
4850
func (v *version) InitializerConfigurations() InitializerConfigurationInformer {
49-
return &initializerConfigurationInformer{factory: v.SharedInformerFactory}
51+
return &initializerConfigurationInformer{factory: v.factory, tweakListOptions: v.tweakListOptions}
5052
}

informers/apps/interface.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,27 @@ type Interface interface {
3636
}
3737

3838
type group struct {
39-
internalinterfaces.SharedInformerFactory
39+
factory internalinterfaces.SharedInformerFactory
40+
namespace string
41+
tweakListOptions internalinterfaces.TweakListOptionsFunc
4042
}
4143

4244
// New returns a new Interface.
43-
func New(f internalinterfaces.SharedInformerFactory) Interface {
44-
return &group{f}
45+
func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
46+
return &group{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
4547
}
4648

4749
// V1 returns a new v1.Interface.
4850
func (g *group) V1() v1.Interface {
49-
return v1.New(g.SharedInformerFactory)
51+
return v1.New(g.factory, g.namespace, g.tweakListOptions)
5052
}
5153

5254
// V1beta1 returns a new v1beta1.Interface.
5355
func (g *group) V1beta1() v1beta1.Interface {
54-
return v1beta1.New(g.SharedInformerFactory)
56+
return v1beta1.New(g.factory, g.namespace, g.tweakListOptions)
5557
}
5658

5759
// V1beta2 returns a new v1beta2.Interface.
5860
func (g *group) V1beta2() v1beta2.Interface {
59-
return v1beta2.New(g.SharedInformerFactory)
61+
return v1beta2.New(g.factory, g.namespace, g.tweakListOptions)
6062
}

informers/apps/v1/controllerrevision.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,34 @@ type ControllerRevisionInformer interface {
3838
}
3939

4040
type controllerRevisionInformer struct {
41-
factory internalinterfaces.SharedInformerFactory
41+
factory internalinterfaces.SharedInformerFactory
42+
tweakListOptions internalinterfaces.TweakListOptionsFunc
43+
namespace string
4244
}
4345

4446
// NewControllerRevisionInformer constructs a new informer for ControllerRevision type.
4547
// Always prefer using an informer factory to get a shared informer instead of getting an independent
4648
// one. This reduces memory footprint and number of connections to the server.
4749
func NewControllerRevisionInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
50+
return NewFilteredControllerRevisionInformer(client, namespace, resyncPeriod, indexers, nil)
51+
}
52+
53+
// NewFilteredControllerRevisionInformer constructs a new informer for ControllerRevision type.
54+
// Always prefer using an informer factory to get a shared informer instead of getting an independent
55+
// one. This reduces memory footprint and number of connections to the server.
56+
func NewFilteredControllerRevisionInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
4857
return cache.NewSharedIndexInformer(
4958
&cache.ListWatch{
5059
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
60+
if tweakListOptions != nil {
61+
tweakListOptions(&options)
62+
}
5163
return client.AppsV1().ControllerRevisions(namespace).List(options)
5264
},
5365
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
66+
if tweakListOptions != nil {
67+
tweakListOptions(&options)
68+
}
5469
return client.AppsV1().ControllerRevisions(namespace).Watch(options)
5570
},
5671
},
@@ -60,12 +75,12 @@ func NewControllerRevisionInformer(client kubernetes.Interface, namespace string
6075
)
6176
}
6277

63-
func defaultControllerRevisionInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
64-
return NewControllerRevisionInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
78+
func (f *controllerRevisionInformer) defaultInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
79+
return NewFilteredControllerRevisionInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
6580
}
6681

6782
func (f *controllerRevisionInformer) Informer() cache.SharedIndexInformer {
68-
return f.factory.InformerFor(&apps_v1.ControllerRevision{}, defaultControllerRevisionInformer)
83+
return f.factory.InformerFor(&apps_v1.ControllerRevision{}, f.defaultInformer)
6984
}
7085

7186
func (f *controllerRevisionInformer) Lister() v1.ControllerRevisionLister {

informers/apps/v1/daemonset.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,34 @@ type DaemonSetInformer interface {
3838
}
3939

4040
type daemonSetInformer struct {
41-
factory internalinterfaces.SharedInformerFactory
41+
factory internalinterfaces.SharedInformerFactory
42+
tweakListOptions internalinterfaces.TweakListOptionsFunc
43+
namespace string
4244
}
4345

4446
// NewDaemonSetInformer constructs a new informer for DaemonSet type.
4547
// Always prefer using an informer factory to get a shared informer instead of getting an independent
4648
// one. This reduces memory footprint and number of connections to the server.
4749
func NewDaemonSetInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
50+
return NewFilteredDaemonSetInformer(client, namespace, resyncPeriod, indexers, nil)
51+
}
52+
53+
// NewFilteredDaemonSetInformer constructs a new informer for DaemonSet type.
54+
// Always prefer using an informer factory to get a shared informer instead of getting an independent
55+
// one. This reduces memory footprint and number of connections to the server.
56+
func NewFilteredDaemonSetInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
4857
return cache.NewSharedIndexInformer(
4958
&cache.ListWatch{
5059
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
60+
if tweakListOptions != nil {
61+
tweakListOptions(&options)
62+
}
5163
return client.AppsV1().DaemonSets(namespace).List(options)
5264
},
5365
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
66+
if tweakListOptions != nil {
67+
tweakListOptions(&options)
68+
}
5469
return client.AppsV1().DaemonSets(namespace).Watch(options)
5570
},
5671
},
@@ -60,12 +75,12 @@ func NewDaemonSetInformer(client kubernetes.Interface, namespace string, resyncP
6075
)
6176
}
6277

63-
func defaultDaemonSetInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
64-
return NewDaemonSetInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
78+
func (f *daemonSetInformer) defaultInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
79+
return NewFilteredDaemonSetInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
6580
}
6681

6782
func (f *daemonSetInformer) Informer() cache.SharedIndexInformer {
68-
return f.factory.InformerFor(&apps_v1.DaemonSet{}, defaultDaemonSetInformer)
83+
return f.factory.InformerFor(&apps_v1.DaemonSet{}, f.defaultInformer)
6984
}
7085

7186
func (f *daemonSetInformer) Lister() v1.DaemonSetLister {

informers/apps/v1/deployment.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,34 @@ type DeploymentInformer interface {
3838
}
3939

4040
type deploymentInformer struct {
41-
factory internalinterfaces.SharedInformerFactory
41+
factory internalinterfaces.SharedInformerFactory
42+
tweakListOptions internalinterfaces.TweakListOptionsFunc
43+
namespace string
4244
}
4345

4446
// NewDeploymentInformer constructs a new informer for Deployment type.
4547
// Always prefer using an informer factory to get a shared informer instead of getting an independent
4648
// one. This reduces memory footprint and number of connections to the server.
4749
func NewDeploymentInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
50+
return NewFilteredDeploymentInformer(client, namespace, resyncPeriod, indexers, nil)
51+
}
52+
53+
// NewFilteredDeploymentInformer constructs a new informer for Deployment type.
54+
// Always prefer using an informer factory to get a shared informer instead of getting an independent
55+
// one. This reduces memory footprint and number of connections to the server.
56+
func NewFilteredDeploymentInformer(client kubernetes.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
4857
return cache.NewSharedIndexInformer(
4958
&cache.ListWatch{
5059
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
60+
if tweakListOptions != nil {
61+
tweakListOptions(&options)
62+
}
5163
return client.AppsV1().Deployments(namespace).List(options)
5264
},
5365
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
66+
if tweakListOptions != nil {
67+
tweakListOptions(&options)
68+
}
5469
return client.AppsV1().Deployments(namespace).Watch(options)
5570
},
5671
},
@@ -60,12 +75,12 @@ func NewDeploymentInformer(client kubernetes.Interface, namespace string, resync
6075
)
6176
}
6277

63-
func defaultDeploymentInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
64-
return NewDeploymentInformer(client, meta_v1.NamespaceAll, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
78+
func (f *deploymentInformer) defaultInformer(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
79+
return NewFilteredDeploymentInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
6580
}
6681

6782
func (f *deploymentInformer) Informer() cache.SharedIndexInformer {
68-
return f.factory.InformerFor(&apps_v1.Deployment{}, defaultDeploymentInformer)
83+
return f.factory.InformerFor(&apps_v1.Deployment{}, f.defaultInformer)
6984
}
7085

7186
func (f *deploymentInformer) Lister() v1.DeploymentLister {

0 commit comments

Comments
 (0)