Skip to content

Instantly share code, notes, and snippets.

@sttts
Created September 17, 2023 11:08
Show Gist options
  • Save sttts/923cb3f5b63e0a7f35f4587c3f59c8c4 to your computer and use it in GitHub Desktop.
Save sttts/923cb3f5b63e0a7f35f4587c3f59c8c4 to your computer and use it in GitHub Desktop.
diff --git a/examples/pkg/kcp/clients/informers/externalversions/example/interface.go b/examples/pkg/kcp/clients/informers/externalversions/example/interface.go
index 424e1f2..2c68a44 100644
--- a/examples/pkg/kcp/clients/informers/externalversions/example/interface.go
+++ b/examples/pkg/kcp/clients/informers/externalversions/example/interface.go
@@ -81,7 +81,7 @@ type Interface interface {
V2() v2.Interface
}
-type scopedGroup struct {
+type classicalGroup struct {
factory internalinterfaces.SharedScopedInformerFactory
tweakListOptions internalinterfaces.TweakListOptionsFunc
namespace string
@@ -89,6 +89,37 @@ type scopedGroup struct {
// New returns a new Interface.
func NewScoped(f internalinterfaces.SharedScopedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
+ return &classicalGroup{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
+}
+
+// V1 returns a new v1.ClusterInterface.
+func (g *classicalGroup) V1() v1.Interface {
+ return v1.NewScoped(g.factory, g.namespace, g.tweakListOptions)
+}
+
+// V1alpha1 returns a new v1alpha1.ClusterInterface.
+func (g *classicalGroup) V1alpha1() v1alpha1.Interface {
+ return v1alpha1.NewScoped(g.factory, g.namespace, g.tweakListOptions)
+}
+
+// V1beta1 returns a new v1beta1.ClusterInterface.
+func (g *classicalGroup) V1beta1() v1beta1.Interface {
+ return v1beta1.NewScoped(g.factory, g.namespace, g.tweakListOptions)
+}
+
+// V2 returns a new v2.ClusterInterface.
+func (g *classicalGroup) V2() v2.Interface {
+ return v2.NewScoped(g.factory, g.namespace, g.tweakListOptions)
+}
+
+type scopedGroup struct {
+ factory internalinterfaces.SharedInformerFactory
+ tweakListOptions internalinterfaces.TweakListOptionsFunc
+ namespace string
+}
+
+// New returns a new Interface.
+func NewScoped2(f internalinterfaces.SharedInformerFactory, namespace string, tweakListOptions internalinterfaces.TweakListOptionsFunc) Interface {
return &scopedGroup{factory: f, namespace: namespace, tweakListOptions: tweakListOptions}
}
@@ -110,4 +141,4 @@ func (g *scopedGroup) V1beta1() v1beta1.Interface {
// V2 returns a new v2.ClusterInterface.
func (g *scopedGroup) V2() v2.Interface {
return v2.NewScoped(g.factory, g.namespace, g.tweakListOptions)
-}
+}
\ No newline at end of file
diff --git a/examples/pkg/kcp/clients/informers/externalversions/example/v1/testtype.go b/examples/pkg/kcp/clients/informers/externalversions/example/v1/testtype.go
index d4341a5..26d3ae4 100644
--- a/examples/pkg/kcp/clients/informers/externalversions/example/v1/testtype.go
+++ b/examples/pkg/kcp/clients/informers/externalversions/example/v1/testtype.go
@@ -130,7 +130,7 @@ func (f *testTypeInformer) Lister() examplev1listers.TestTypeLister {
}
type testTypeScopedInformer struct {
- factory internalinterfaces.SharedScopedInformerFactory
+ factory internalinterfaces.SharedInformerFactory
tweakListOptions internalinterfaces.TweakListOptionsFunc
namespace string
}
@@ -175,8 +175,16 @@ func NewFilteredTestTypeInformer(client scopedclientset.Interface, resyncPeriod
)
}
-func (f *testTypeScopedInformer) defaultInformer(client scopedclientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
+func (f *testTypeScopedInformer) defaultInformer(client clientset.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
return NewFilteredTestTypeInformer(client, resyncPeriod, f.namespace, cache.Indexers{
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
}, f.tweakListOptions)
}
+
+func (f *testTypeScopedInformer) defaultInformer(client clientset.ClusterInterface, resyncPeriod time.Duration) cache.SharedIndexInformer {
+ return NewFilteredTestTypeClusterInformer(client, resyncPeriod, cache.Indexers{
+ kcpcache.ClusterIndexName: kcpcache.ClusterIndexFunc,
+ kcpcache.ClusterAndNamespaceIndexName: kcpcache.ClusterAndNamespaceIndexFunc},
+ f.tweakListOptions,
+ )
+}
diff --git a/examples/pkg/kcp/clients/informers/externalversions/factory.go b/examples/pkg/kcp/clients/informers/externalversions/factory.go
index 36763ad..25994d2 100644
--- a/examples/pkg/kcp/clients/informers/externalversions/factory.go
+++ b/examples/pkg/kcp/clients/informers/externalversions/factory.go
@@ -197,15 +197,6 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
return informer
}
-type ScopedDynamicSharedInformerFactory interface {
- // ForResource gives generic access to a shared informer of the matching type.
- ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
-
- // Start initializes all requested informers. They are handled in goroutines
- // which run until the stop channel gets closed.
- Start(stopCh <-chan struct{})
-}
-
// SharedInformerFactory provides shared informers for resources in all known
// API group versions.
//
@@ -233,7 +224,7 @@ type ScopedDynamicSharedInformerFactory interface {
type SharedInformerFactory interface {
internalinterfaces.SharedInformerFactory
- Cluster(logicalcluster.Name) ScopedDynamicSharedInformerFactory
+ Cluster(logicalcluster.Name) SharedScopedInformerFactory
// Start initializes all requested informers. They are handled in goroutines
// which run until the stop channel gets closed.
@@ -283,19 +274,19 @@ func (f *sharedInformerFactory) Secondexample() secondexampleinformers.ClusterIn
return secondexampleinformers.New(f, f.tweakListOptions)
}
-func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) ScopedDynamicSharedInformerFactory {
- return &scopedDynamicSharedInformerFactory{
+func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) SharedScopedInformerFactory {
+ return &scopedSharedInformerFactory{
sharedInformerFactory: f,
clusterName: clusterName,
}
}
-type scopedDynamicSharedInformerFactory struct {
+type scopedSharedInformerFactory struct {
*sharedInformerFactory
clusterName logicalcluster.Name
}
-func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVersionResource) (GenericInformer, error) {
+func (f *scopedSharedInformerFactory) ForResource(resource schema.GroupVersionResource) (GenericInformer, error) {
clusterInformer, err := f.sharedInformerFactory.ForResource(resource)
if err != nil {
return nil, err
@@ -303,10 +294,31 @@ func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVe
return clusterInformer.Cluster(f.clusterName), nil
}
-func (f *scopedDynamicSharedInformerFactory) Start(stopCh <-chan struct{}) {
+// InformerFor returns the SharedIndexInformer for obj.
+func (f *scopedSharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
+ return f.sharedInformerFactory.InformerFor(obj, newFunc).Cluster(f.clusterName)
+}
+
+func (f *scopedSharedInformerFactory) Start(stopCh <-chan struct{}) {
f.sharedInformerFactory.Start(stopCh)
}
+func (f *scopedSharedInformerFactory) Example() exampleinformers.Interface {
+ return exampleinformers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Example3() example3informers.Interface {
+ return example3informers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Existinginterfaces() existinginterfacesinformers.Interface {
+ return existinginterfacesinformers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Secondexample() secondexampleinformers.Interface {
+ return secondexampleinformers.NewScoped(f, f.tweakListOptions)
+}
+
// WithNamespace limits the SharedInformerFactory to the specified namespace.
func WithNamespace(namespace string) SharedInformerOption {
return func(opts *SharedInformerOptions) *SharedInformerOptions {
@@ -315,7 +327,7 @@ func WithNamespace(namespace string) SharedInformerOption {
}
}
-type sharedScopedInformerFactory struct {
+type classicalSharedInformerFactory struct {
client scopedclientset.Interface
namespace string
tweakListOptions internalinterfaces.TweakListOptionsFunc
@@ -336,7 +348,7 @@ func NewSharedScopedInformerFactory(client scopedclientset.Interface, defaultRes
// NewSharedScopedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
func NewSharedScopedInformerFactoryWithOptions(client scopedclientset.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedScopedInformerFactory {
- factory := &sharedScopedInformerFactory{
+ factory := &classicalSharedInformerFactory{
client: client,
defaultResync: defaultResync,
informers: make(map[reflect.Type]cache.SharedIndexInformer),
@@ -362,7 +374,7 @@ func NewSharedScopedInformerFactoryWithOptions(client scopedclientset.Interface,
}
// Start initializes all requested informers.
-func (f *sharedScopedInformerFactory) Start(stopCh <-chan struct{}) {
+func (f *classicalSharedInformerFactory) Start(stopCh <-chan struct{}) {
f.lock.Lock()
defer f.lock.Unlock()
@@ -375,7 +387,7 @@ func (f *sharedScopedInformerFactory) Start(stopCh <-chan struct{}) {
}
// WaitForCacheSync waits for all started informers' cache were synced.
-func (f *sharedScopedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
+func (f *classicalSharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
informers := func() map[reflect.Type]cache.SharedIndexInformer {
f.lock.Lock()
defer f.lock.Unlock()
@@ -397,7 +409,7 @@ func (f *sharedScopedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) m
}
// InformerFor returns the SharedIndexInformer for obj.
-func (f *sharedScopedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
+func (f *classicalSharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
f.lock.Lock()
defer f.lock.Unlock()
@@ -431,18 +443,18 @@ type SharedScopedInformerFactory interface {
Secondexample() secondexampleinformers.Interface
}
-func (f *sharedScopedInformerFactory) Example() exampleinformers.Interface {
+func (f *classicalSharedInformerFactory) Example() exampleinformers.Interface {
return exampleinformers.NewScoped(f, f.namespace, f.tweakListOptions)
}
-func (f *sharedScopedInformerFactory) Example3() example3informers.Interface {
+func (f *classicalSharedInformerFactory) Example3() example3informers.Interface {
return example3informers.NewScoped(f, f.namespace, f.tweakListOptions)
}
-func (f *sharedScopedInformerFactory) Existinginterfaces() existinginterfacesinformers.Interface {
+func (f *classicalSharedInformerFactory) Existinginterfaces() existinginterfacesinformers.Interface {
return existinginterfacesinformers.NewScoped(f, f.namespace, f.tweakListOptions)
}
-func (f *sharedScopedInformerFactory) Secondexample() secondexampleinformers.Interface {
+func (f *classicalSharedInformerFactory) Secondexample() secondexampleinformers.Interface {
return secondexampleinformers.NewScoped(f, f.namespace, f.tweakListOptions)
}
diff --git a/examples/pkg/kcpexisting/clients/informers/externalversions/factory.go b/examples/pkg/kcpexisting/clients/informers/externalversions/factory.go
index 2589763..e8d11a9 100644
--- a/examples/pkg/kcpexisting/clients/informers/externalversions/factory.go
+++ b/examples/pkg/kcpexisting/clients/informers/externalversions/factory.go
@@ -196,15 +196,6 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
return informer
}
-type ScopedDynamicSharedInformerFactory interface {
- // ForResource gives generic access to a shared informer of the matching type.
- ForResource(resource schema.GroupVersionResource) (upstreaminformers.GenericInformer, error)
-
- // Start initializes all requested informers. They are handled in goroutines
- // which run until the stop channel gets closed.
- Start(stopCh <-chan struct{})
-}
-
// SharedInformerFactory provides shared informers for resources in all known
// API group versions.
//
@@ -232,7 +223,7 @@ type ScopedDynamicSharedInformerFactory interface {
type SharedInformerFactory interface {
internalinterfaces.SharedInformerFactory
- Cluster(logicalcluster.Name) ScopedDynamicSharedInformerFactory
+ Cluster(logicalcluster.Name) SharedScopedInformerFactory
// Start initializes all requested informers. They are handled in goroutines
// which run until the stop channel gets closed.
@@ -257,9 +248,6 @@ type SharedInformerFactory interface {
// or the stop channel gets closed.
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
- // InformerFor returns the SharedIndexInformer for obj.
- InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) kcpcache.ScopeableSharedIndexInformer
-
Example() exampleinformers.ClusterInterface
Example3() example3informers.ClusterInterface
Existinginterfaces() existinginterfacesinformers.ClusterInterface
@@ -282,19 +270,19 @@ func (f *sharedInformerFactory) Secondexample() secondexampleinformers.ClusterIn
return secondexampleinformers.New(f, f.tweakListOptions)
}
-func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) ScopedDynamicSharedInformerFactory {
- return &scopedDynamicSharedInformerFactory{
+func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) SharedScopedInformerFactory {
+ return &scopedSharedInformerFactory{
sharedInformerFactory: f,
clusterName: clusterName,
}
}
-type scopedDynamicSharedInformerFactory struct {
+type scopedSharedInformerFactory struct {
*sharedInformerFactory
clusterName logicalcluster.Name
}
-func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVersionResource) (upstreaminformers.GenericInformer, error) {
+func (f *scopedSharedInformerFactory) ForResource(resource schema.GroupVersionResource) (upstreaminformers.GenericInformer, error) {
clusterInformer, err := f.sharedInformerFactory.ForResource(resource)
if err != nil {
return nil, err
@@ -302,6 +290,22 @@ func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVe
return clusterInformer.Cluster(f.clusterName), nil
}
-func (f *scopedDynamicSharedInformerFactory) Start(stopCh <-chan struct{}) {
+func (f *scopedSharedInformerFactory) Start(stopCh <-chan struct{}) {
f.sharedInformerFactory.Start(stopCh)
}
+
+func (f *scopedSharedInformerFactory) Example() exampleinformers.Interface {
+ return exampleinformers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Example3() example3informers.Interface {
+ return example3informers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Existinginterfaces() existinginterfacesinformers.Interface {
+ return existinginterfacesinformers.NewScoped(f, f.tweakListOptions)
+}
+
+func (f *scopedSharedInformerFactory) Secondexample() secondexampleinformers.Interface {
+ return secondexampleinformers.NewScoped(f, f.tweakListOptions)
+}
diff --git a/pkg/internal/informergen/factory.go b/pkg/internal/informergen/factory.go
index a8c2eb0..ca095f9 100644
--- a/pkg/internal/informergen/factory.go
+++ b/pkg/internal/informergen/factory.go
@@ -254,15 +254,6 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
return informer
}
-type ScopedDynamicSharedInformerFactory interface {
- // ForResource gives generic access to a shared informer of the matching type.
- ForResource(resource schema.GroupVersionResource) ({{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer, error)
-
- // Start initializes all requested informers. They are handled in goroutines
- // which run until the stop channel gets closed.
- Start(stopCh <-chan struct{})
-}
-
// SharedInformerFactory provides shared informers for resources in all known
// API group versions.
//
@@ -290,7 +281,7 @@ type ScopedDynamicSharedInformerFactory interface {
type SharedInformerFactory interface {
internalinterfaces.SharedInformerFactory
- Cluster(logicalcluster.Name) ScopedDynamicSharedInformerFactory
+ Cluster(logicalcluster.Name) SharedScopedInformerFactory
// Start initializes all requested informers. They are handled in goroutines
// which run until the stop channel gets closed.
@@ -315,9 +306,6 @@ type SharedInformerFactory interface {
// or the stop channel gets closed.
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
- // InformerFor returns the SharedIndexInformer for obj.
- InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) kcpcache.ScopeableSharedIndexInformer
-
{{range .groups}} {{.GoName}}() {{.Group.PackageName}}informers.ClusterInterface
{{end -}}
}
@@ -328,19 +316,19 @@ func (f *sharedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.Cl
}
{{end}}
-func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) ScopedDynamicSharedInformerFactory {
- return &scopedDynamicSharedInformerFactory{
+func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) SharedScopedInformerFactory {
+ return &scopedSharedInformerFactory{
sharedInformerFactory: f,
clusterName: clusterName,
}
}
-type scopedDynamicSharedInformerFactory struct {
+type scopedSharedInformerFactory struct {
*sharedInformerFactory
clusterName logicalcluster.Name
}
-func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVersionResource) ({{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer, error) {
+func (f *scopedSharedInformerFactory) ForResource(resource schema.GroupVersionResource) ({{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer, error) {
clusterInformer, err := f.sharedInformerFactory.ForResource(resource)
if err != nil {
return nil, err
@@ -348,10 +336,21 @@ func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVe
return clusterInformer.Cluster(f.clusterName), nil
}
-func (f *scopedDynamicSharedInformerFactory) Start(stopCh <-chan struct{}) {
+// InformerFor returns the SharedIndexInformer for obj.
+func (f *scopedSharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
+ return f.sharedInformerFactory.InformerFor(obj, newFunc).Cluster(f.clusterName)
+}
+
+func (f *scopedSharedInformerFactory) Start(stopCh <-chan struct{}) {
f.sharedInformerFactory.Start(stopCh)
}
+{{range .groups}}
+func (f *scopedSharedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.Interface {
+ return {{.Group.PackageName}}informers.NewScoped(f, f.tweakListOptions)
+}
+{{end}}
+
{{if not .useUpstreamInterfaces -}}
// WithNamespace limits the SharedInformerFactory to the specified namespace.
func WithNamespace(namespace string) SharedInformerOption {
@@ -361,7 +360,7 @@ func WithNamespace(namespace string) SharedInformerOption {
}
}
-type sharedScopedInformerFactory struct {
+type classicalSharedInformerFactory struct {
client scopedclientset.Interface
namespace string
tweakListOptions internalinterfaces.TweakListOptionsFunc
@@ -382,7 +381,7 @@ func NewSharedScopedInformerFactory(client scopedclientset.Interface, defaultRes
// NewSharedScopedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
func NewSharedScopedInformerFactoryWithOptions(client scopedclientset.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedScopedInformerFactory {
- factory := &sharedScopedInformerFactory{
+ factory := &classicalSharedInformerFactory{
client: client,
defaultResync: defaultResync,
informers: make(map[reflect.Type]cache.SharedIndexInformer),
@@ -408,7 +407,7 @@ func NewSharedScopedInformerFactoryWithOptions(client scopedclientset.Interface,
}
// Start initializes all requested informers.
-func (f *sharedScopedInformerFactory) Start(stopCh <-chan struct{}) {
+func (f *classicalSharedInformerFactory) Start(stopCh <-chan struct{}) {
f.lock.Lock()
defer f.lock.Unlock()
@@ -421,7 +420,7 @@ func (f *sharedScopedInformerFactory) Start(stopCh <-chan struct{}) {
}
// WaitForCacheSync waits for all started informers' cache were synced.
-func (f *sharedScopedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
+func (f *classicalSharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
informers := func()map[reflect.Type]cache.SharedIndexInformer{
f.lock.Lock()
defer f.lock.Unlock()
@@ -443,7 +442,7 @@ func (f *sharedScopedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) m
}
// InformerFor returns the SharedIndexInformer for obj.
-func (f *sharedScopedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
+func (f *classicalSharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) cache.SharedIndexInformer {
f.lock.Lock()
defer f.lock.Unlock()
@@ -477,7 +476,7 @@ type SharedScopedInformerFactory interface {
{{range .groups}}
-func (f *sharedScopedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.Interface {
+func (f *classicalSharedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.Interface {
return {{.Group.PackageName}}informers.NewScoped(f, f.namespace, f.tweakListOptions)
}
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment