mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-08 09:28:30 +02:00
Modernize code generation (#3003)
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
This commit is contained in:
@@ -25,13 +25,13 @@ SOFTWARE.
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
context "context"
|
||||
time "time"
|
||||
|
||||
acidzalandov1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||
apisacidzalandov1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||
versioned "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned"
|
||||
internalinterfaces "github.com/zalando/postgres-operator/pkg/generated/informers/externalversions/internalinterfaces"
|
||||
v1 "github.com/zalando/postgres-operator/pkg/generated/listers/acid.zalan.do/v1"
|
||||
acidzalandov1 "github.com/zalando/postgres-operator/pkg/generated/listers/acid.zalan.do/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
watch "k8s.io/apimachinery/pkg/watch"
|
||||
@@ -42,7 +42,7 @@ import (
|
||||
// Postgresqls.
|
||||
type PostgresqlInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() v1.PostgresqlLister
|
||||
Lister() acidzalandov1.PostgresqlLister
|
||||
}
|
||||
|
||||
type postgresqlInformer struct {
|
||||
@@ -77,7 +77,7 @@ func NewFilteredPostgresqlInformer(client versioned.Interface, namespace string,
|
||||
return client.AcidV1().Postgresqls(namespace).Watch(context.TODO(), options)
|
||||
},
|
||||
},
|
||||
&acidzalandov1.Postgresql{},
|
||||
&apisacidzalandov1.Postgresql{},
|
||||
resyncPeriod,
|
||||
indexers,
|
||||
)
|
||||
@@ -88,9 +88,9 @@ func (f *postgresqlInformer) defaultInformer(client versioned.Interface, resyncP
|
||||
}
|
||||
|
||||
func (f *postgresqlInformer) Informer() cache.SharedIndexInformer {
|
||||
return f.factory.InformerFor(&acidzalandov1.Postgresql{}, f.defaultInformer)
|
||||
return f.factory.InformerFor(&apisacidzalandov1.Postgresql{}, f.defaultInformer)
|
||||
}
|
||||
|
||||
func (f *postgresqlInformer) Lister() v1.PostgresqlLister {
|
||||
return v1.NewPostgresqlLister(f.Informer().GetIndexer())
|
||||
func (f *postgresqlInformer) Lister() acidzalandov1.PostgresqlLister {
|
||||
return acidzalandov1.NewPostgresqlLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
@@ -25,13 +25,13 @@ SOFTWARE.
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
context "context"
|
||||
time "time"
|
||||
|
||||
acidzalandov1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||
apisacidzalandov1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||
versioned "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned"
|
||||
internalinterfaces "github.com/zalando/postgres-operator/pkg/generated/informers/externalversions/internalinterfaces"
|
||||
v1 "github.com/zalando/postgres-operator/pkg/generated/listers/acid.zalan.do/v1"
|
||||
acidzalandov1 "github.com/zalando/postgres-operator/pkg/generated/listers/acid.zalan.do/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
watch "k8s.io/apimachinery/pkg/watch"
|
||||
@@ -42,7 +42,7 @@ import (
|
||||
// PostgresTeams.
|
||||
type PostgresTeamInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() v1.PostgresTeamLister
|
||||
Lister() acidzalandov1.PostgresTeamLister
|
||||
}
|
||||
|
||||
type postgresTeamInformer struct {
|
||||
@@ -77,7 +77,7 @@ func NewFilteredPostgresTeamInformer(client versioned.Interface, namespace strin
|
||||
return client.AcidV1().PostgresTeams(namespace).Watch(context.TODO(), options)
|
||||
},
|
||||
},
|
||||
&acidzalandov1.PostgresTeam{},
|
||||
&apisacidzalandov1.PostgresTeam{},
|
||||
resyncPeriod,
|
||||
indexers,
|
||||
)
|
||||
@@ -88,9 +88,9 @@ func (f *postgresTeamInformer) defaultInformer(client versioned.Interface, resyn
|
||||
}
|
||||
|
||||
func (f *postgresTeamInformer) Informer() cache.SharedIndexInformer {
|
||||
return f.factory.InformerFor(&acidzalandov1.PostgresTeam{}, f.defaultInformer)
|
||||
return f.factory.InformerFor(&apisacidzalandov1.PostgresTeam{}, f.defaultInformer)
|
||||
}
|
||||
|
||||
func (f *postgresTeamInformer) Lister() v1.PostgresTeamLister {
|
||||
return v1.NewPostgresTeamLister(f.Informer().GetIndexer())
|
||||
func (f *postgresTeamInformer) Lister() acidzalandov1.PostgresTeamLister {
|
||||
return acidzalandov1.NewPostgresTeamLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
@@ -49,11 +49,17 @@ type sharedInformerFactory struct {
|
||||
lock sync.Mutex
|
||||
defaultResync time.Duration
|
||||
customResync map[reflect.Type]time.Duration
|
||||
transform cache.TransformFunc
|
||||
|
||||
informers map[reflect.Type]cache.SharedIndexInformer
|
||||
// startedInformers is used for tracking which informers have been started.
|
||||
// This allows Start() to be called multiple times safely.
|
||||
startedInformers map[reflect.Type]bool
|
||||
// wg tracks how many goroutines were started.
|
||||
wg sync.WaitGroup
|
||||
// shuttingDown is true when Shutdown has been called. It may still be running
|
||||
// because it needs to wait for goroutines.
|
||||
shuttingDown bool
|
||||
}
|
||||
|
||||
// WithCustomResyncConfig sets a custom resync period for the specified informer types.
|
||||
@@ -82,6 +88,14 @@ func WithNamespace(namespace string) SharedInformerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithTransform sets a transform on all informers.
|
||||
func WithTransform(transform cache.TransformFunc) SharedInformerOption {
|
||||
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||
factory.transform = transform
|
||||
return factory
|
||||
}
|
||||
}
|
||||
|
||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
|
||||
func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
|
||||
return NewSharedInformerFactoryWithOptions(client, defaultResync)
|
||||
@@ -114,20 +128,39 @@ func NewSharedInformerFactoryWithOptions(client versioned.Interface, defaultResy
|
||||
return factory
|
||||
}
|
||||
|
||||
// Start initializes all requested informers.
|
||||
func (f *sharedInformerFactory) Start(stopCh <-chan struct{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.shuttingDown {
|
||||
return
|
||||
}
|
||||
|
||||
for informerType, informer := range f.informers {
|
||||
if !f.startedInformers[informerType] {
|
||||
go informer.Run(stopCh)
|
||||
f.wg.Add(1)
|
||||
// We need a new variable in each loop iteration,
|
||||
// otherwise the goroutine would use the loop variable
|
||||
// and that keeps changing.
|
||||
informer := informer
|
||||
go func() {
|
||||
defer f.wg.Done()
|
||||
informer.Run(stopCh)
|
||||
}()
|
||||
f.startedInformers[informerType] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WaitForCacheSync waits for all started informers' cache were synced.
|
||||
func (f *sharedInformerFactory) Shutdown() {
|
||||
f.lock.Lock()
|
||||
f.shuttingDown = true
|
||||
f.lock.Unlock()
|
||||
|
||||
// Will return immediately if there is nothing to wait for.
|
||||
f.wg.Wait()
|
||||
}
|
||||
|
||||
func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
|
||||
informers := func() map[reflect.Type]cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
@@ -149,7 +182,7 @@ func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[ref
|
||||
return res
|
||||
}
|
||||
|
||||
// InternalInformerFor returns the SharedIndexInformer for obj using an internal
|
||||
// InformerFor returns the SharedIndexInformer for obj using an internal
|
||||
// client.
|
||||
func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
|
||||
f.lock.Lock()
|
||||
@@ -167,6 +200,7 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
|
||||
}
|
||||
|
||||
informer = newFunc(f.client, resyncPeriod)
|
||||
informer.SetTransform(f.transform)
|
||||
f.informers[informerType] = informer
|
||||
|
||||
return informer
|
||||
@@ -174,11 +208,59 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
|
||||
|
||||
// SharedInformerFactory provides shared informers for resources in all known
|
||||
// API group versions.
|
||||
//
|
||||
// It is typically used like this:
|
||||
//
|
||||
// ctx, cancel := context.Background()
|
||||
// defer cancel()
|
||||
// factory := NewSharedInformerFactory(client, resyncPeriod)
|
||||
// defer factory.WaitForStop() // Returns immediately if nothing was started.
|
||||
// genericInformer := factory.ForResource(resource)
|
||||
// typedInformer := factory.SomeAPIGroup().V1().SomeType()
|
||||
// factory.Start(ctx.Done()) // Start processing these informers.
|
||||
// synced := factory.WaitForCacheSync(ctx.Done())
|
||||
// for v, ok := range synced {
|
||||
// if !ok {
|
||||
// fmt.Fprintf(os.Stderr, "caches failed to sync: %v", v)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Creating informers can also be created after Start, but then
|
||||
// // Start must be called again:
|
||||
// anotherGenericInformer := factory.ForResource(resource)
|
||||
// factory.Start(ctx.Done())
|
||||
type SharedInformerFactory interface {
|
||||
internalinterfaces.SharedInformerFactory
|
||||
ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
|
||||
|
||||
// Start initializes all requested informers. They are handled in goroutines
|
||||
// which run until the stop channel gets closed.
|
||||
// Warning: Start does not block. When run in a go-routine, it will race with a later WaitForCacheSync.
|
||||
Start(stopCh <-chan struct{})
|
||||
|
||||
// Shutdown marks a factory as shutting down. At that point no new
|
||||
// informers can be started anymore and Start will return without
|
||||
// doing anything.
|
||||
//
|
||||
// In addition, Shutdown blocks until all goroutines have terminated. For that
|
||||
// to happen, the close channel(s) that they were started with must be closed,
|
||||
// either before Shutdown gets called or while it is waiting.
|
||||
//
|
||||
// Shutdown may be called multiple times, even concurrently. All such calls will
|
||||
// block until all goroutines have terminated.
|
||||
Shutdown()
|
||||
|
||||
// WaitForCacheSync blocks until all started informers' caches were synced
|
||||
// or the stop channel gets closed.
|
||||
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
|
||||
|
||||
// ForResource gives generic access to a shared informer of the matching type.
|
||||
ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
|
||||
|
||||
// InformerFor returns the SharedIndexInformer for obj using an internal
|
||||
// client.
|
||||
InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer
|
||||
|
||||
Acid() acidzalando.Interface
|
||||
Zalando() zalandoorg.Interface
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ SOFTWARE.
|
||||
package externalversions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
fmt "fmt"
|
||||
|
||||
v1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||
zalandoorgv1 "github.com/zalando/postgres-operator/pkg/apis/zalando.org/v1"
|
||||
|
||||
@@ -25,13 +25,13 @@ SOFTWARE.
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
context "context"
|
||||
time "time"
|
||||
|
||||
zalandoorgv1 "github.com/zalando/postgres-operator/pkg/apis/zalando.org/v1"
|
||||
apiszalandoorgv1 "github.com/zalando/postgres-operator/pkg/apis/zalando.org/v1"
|
||||
versioned "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned"
|
||||
internalinterfaces "github.com/zalando/postgres-operator/pkg/generated/informers/externalversions/internalinterfaces"
|
||||
v1 "github.com/zalando/postgres-operator/pkg/generated/listers/zalando.org/v1"
|
||||
zalandoorgv1 "github.com/zalando/postgres-operator/pkg/generated/listers/zalando.org/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
watch "k8s.io/apimachinery/pkg/watch"
|
||||
@@ -42,7 +42,7 @@ import (
|
||||
// FabricEventStreams.
|
||||
type FabricEventStreamInformer interface {
|
||||
Informer() cache.SharedIndexInformer
|
||||
Lister() v1.FabricEventStreamLister
|
||||
Lister() zalandoorgv1.FabricEventStreamLister
|
||||
}
|
||||
|
||||
type fabricEventStreamInformer struct {
|
||||
@@ -77,7 +77,7 @@ func NewFilteredFabricEventStreamInformer(client versioned.Interface, namespace
|
||||
return client.ZalandoV1().FabricEventStreams(namespace).Watch(context.TODO(), options)
|
||||
},
|
||||
},
|
||||
&zalandoorgv1.FabricEventStream{},
|
||||
&apiszalandoorgv1.FabricEventStream{},
|
||||
resyncPeriod,
|
||||
indexers,
|
||||
)
|
||||
@@ -88,9 +88,9 @@ func (f *fabricEventStreamInformer) defaultInformer(client versioned.Interface,
|
||||
}
|
||||
|
||||
func (f *fabricEventStreamInformer) Informer() cache.SharedIndexInformer {
|
||||
return f.factory.InformerFor(&zalandoorgv1.FabricEventStream{}, f.defaultInformer)
|
||||
return f.factory.InformerFor(&apiszalandoorgv1.FabricEventStream{}, f.defaultInformer)
|
||||
}
|
||||
|
||||
func (f *fabricEventStreamInformer) Lister() v1.FabricEventStreamLister {
|
||||
return v1.NewFabricEventStreamLister(f.Informer().GetIndexer())
|
||||
func (f *fabricEventStreamInformer) Lister() zalandoorgv1.FabricEventStreamLister {
|
||||
return zalandoorgv1.NewFabricEventStreamLister(f.Informer().GetIndexer())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user