Skip to content

Commit ee57999

Browse files
committed
simplify/cleanup informer-gen
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent 2e0837b commit ee57999

8 files changed

Lines changed: 216 additions & 244 deletions

File tree

cmd/cluster-informer-gen/args/args.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// Args is used by the gengo framework to pass args specific to this generator.
2626
type Args struct {
2727
OutputDir string // must be a directory path
28-
OutputPkg string // must be a Go import-path
28+
OutputPackage string // must be a Go import-path
2929
GoHeaderFile string
3030
VersionedClientSetPackage string // must be a Go import-path
3131
InternalClientSetPackage string // must be a Go import-path
@@ -55,7 +55,7 @@ func New() *Args {
5555
func (args *Args) AddFlags(fs *pflag.FlagSet) {
5656
fs.StringVar(&args.OutputDir, "output-dir", "",
5757
"the base directory under which to generate results")
58-
fs.StringVar(&args.OutputPkg, "output-pkg", args.OutputPkg,
58+
fs.StringVar(&args.OutputPackage, "output-pkg", args.OutputPackage,
5959
"the Go import-path of the generated results")
6060
fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
6161
"the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
@@ -82,7 +82,7 @@ func (args *Args) Validate() error {
8282
if len(args.OutputDir) == 0 {
8383
return fmt.Errorf("--output-dir must be specified")
8484
}
85-
if len(args.OutputPkg) == 0 {
85+
if len(args.OutputPackage) == 0 {
8686
return fmt.Errorf("--output-pkg must be specified")
8787
}
8888
if len(args.VersionedClientSetPackage) == 0 {

cmd/cluster-informer-gen/generators/factory.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ func (g *factoryGenerator) Namers(c *generator.Context) namer.NameSystems {
6161

6262
func (g *factoryGenerator) Imports(c *generator.Context) (imports []string) {
6363
imports = append(imports, g.imports.ImportLines()...)
64-
imports = append(imports,
65-
`"github.com/kcp-dev/logicalcluster/v3"`,
66-
`kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"`,
67-
)
6864
return
6965
}
7066

@@ -102,19 +98,24 @@ func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w i
10298
"gvNewScopedFuncs": gvNewScopedFuncs,
10399
"gvGoNames": g.gvGoNames,
104100
"interfacesNewInformerFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "NewInformerFunc"}),
101+
"interfacesNewScopedInformerFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "NewScopedInformerFunc"}),
105102
"interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}),
106103
"informerFactoryInterface": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}),
107104
"informerScopedFactoryInterface": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedScopedInformerFactory"}),
108105
"clientSetInterface": c.Universe.Type(types.Name{Package: g.singleClusterVersionedClientSetPackage, Name: "Interface"}),
109106
"clusterClientSetInterface": c.Universe.Type(types.Name{Package: g.clientSetPackage, Name: "ClusterInterface"}),
110107
"genericInformer": c.Universe.Type(types.Name{Package: genericInformerPkg, Name: "GenericInformer"}),
108+
"cacheWaitForCacheSync": c.Universe.Function(cacheWaitForCacheSync),
111109
"reflectType": c.Universe.Type(reflectType),
110+
"reflectTypeOf": c.Universe.Type(reflectTypeOf),
112111
"runtimeObject": c.Universe.Type(runtimeObject),
113112
"schemaGroupVersionResource": c.Universe.Type(schemaGroupVersionResource),
114113
"syncMutex": c.Universe.Type(syncMutex),
114+
"syncWaitGroup": c.Universe.Type(syncWaitGroup),
115115
"timeDuration": c.Universe.Type(timeDuration),
116116
"namespaceAll": c.Universe.Type(metav1NamespaceAll),
117117
"object": c.Universe.Type(metav1Object),
118+
"logicalclusterName": c.Universe.Type(logicalclusterName),
118119
}
119120

120121
sw.Do(sharedInformerFactoryStruct, m)
@@ -132,9 +133,9 @@ var sharedInformerFactoryStruct = `
132133
type SharedInformerOption func(*SharedInformerOptions) *SharedInformerOptions
133134
134135
type SharedInformerOptions struct {
135-
customResync map[reflect.Type]time.Duration
136-
tweakListOptions internalinterfaces.TweakListOptionsFunc
137-
transform cache.TransformFunc
136+
customResync map[{{.reflectType|raw}}]{{.timeDuration|raw}}
137+
tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}
138+
transform {{.cacheTransformFunc|raw}}
138139
namespace string
139140
}
140141
@@ -151,7 +152,7 @@ type sharedInformerFactory struct {
151152
// This allows Start() to be called multiple times safely.
152153
startedInformers map[{{.reflectType|raw}}]bool
153154
// wg tracks how many goroutines were started.
154-
wg sync.WaitGroup
155+
wg {{.syncWaitGroup|raw}}
155156
// shuttingDown is true when Shutdown has been called. It may still be running
156157
// because it needs to wait for goroutines.
157158
shuttingDown bool
@@ -161,14 +162,14 @@ type sharedInformerFactory struct {
161162
func WithCustomResyncConfig(resyncConfig map[{{.object|raw}}]{{.timeDuration|raw}}) SharedInformerOption {
162163
return func(opts *SharedInformerOptions) *SharedInformerOptions {
163164
for k, v := range resyncConfig {
164-
opts.customResync[reflect.TypeOf(k)] = v
165+
opts.customResync[{{.reflectTypeOf|raw}}(k)] = v
165166
}
166167
return opts
167168
}
168169
}
169170
170171
// WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
171-
func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
172+
func WithTweakListOptions(tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}) SharedInformerOption {
172173
return func(opts *SharedInformerOptions) *SharedInformerOptions {
173174
opts.tweakListOptions = tweakListOptions
174175
return opts
@@ -252,7 +253,6 @@ func (f *sharedInformerFactory) Shutdown() {
252253
f.shuttingDown = true
253254
f.lock.Unlock()
254255
255-
256256
// Will return immediately if there is nothing to wait for.
257257
f.wg.Wait()
258258
}
@@ -273,7 +273,7 @@ func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[{{.
273273
274274
res := map[{{.reflectType|raw}}]bool{}
275275
for informType, informer := range informers {
276-
res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
276+
res[informType] = {{.cacheWaitForCacheSync|raw}}(stopCh, informer.HasSynced)
277277
}
278278
279279
return res
@@ -284,7 +284,7 @@ func (f *sharedInformerFactory) InformerFor(obj {{.runtimeObject|raw}}, newFunc
284284
f.lock.Lock()
285285
defer f.lock.Unlock()
286286
287-
informerType := reflect.TypeOf(obj)
287+
informerType := {{.reflectTypeOf|raw}}(obj)
288288
informer, exists := f.informers[informerType]
289289
if exists {
290290
return informer
@@ -306,7 +306,7 @@ func (f *sharedInformerFactory) InformerFor(obj {{.runtimeObject|raw}}, newFunc
306306
var sharedInformerFactoryInterface = `
307307
type ScopedDynamicSharedInformerFactory interface {
308308
// ForResource gives generic access to a shared informer of the matching type.
309-
ForResource(resource schema.GroupVersionResource) ({{.genericInformer|raw}}, error)
309+
ForResource(resource {{.schemaGroupVersionResource|raw}}) ({{.genericInformer|raw}}, error)
310310
311311
// Start initializes all requested informers. They are handled in goroutines
312312
// which run until the stop channel gets closed.
@@ -340,7 +340,7 @@ type ScopedDynamicSharedInformerFactory interface {
340340
type SharedInformerFactory interface {
341341
{{.informerFactoryInterface|raw}}
342342
343-
Cluster(logicalcluster.Name) ScopedDynamicSharedInformerFactory
343+
Cluster({{.logicalclusterName|raw}}) ScopedDynamicSharedInformerFactory
344344
345345
// Start initializes all requested informers. They are handled in goroutines
346346
// which run until the stop channel gets closed.
@@ -361,7 +361,7 @@ type SharedInformerFactory interface {
361361
362362
// WaitForCacheSync blocks until all started informers' caches were synced
363363
// or the stop channel gets closed.
364-
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
364+
WaitForCacheSync(stopCh <-chan struct{}) map[{{.reflectType|raw}}]bool
365365
366366
// ForResource gives generic access to a shared informer of the matching type.
367367
ForResource(resource {{.schemaGroupVersionResource|raw}}) (GenericClusterInformer, error)
@@ -380,7 +380,7 @@ func (f *sharedInformerFactory) {{index $.gvGoNames $groupPkgName}}() {{index $.
380380
}
381381
{{end}}
382382
383-
func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) ScopedDynamicSharedInformerFactory {
383+
func (f *sharedInformerFactory) Cluster(clusterName {{.logicalclusterName|raw}}) ScopedDynamicSharedInformerFactory {
384384
return &scopedDynamicSharedInformerFactory{
385385
sharedInformerFactory: f,
386386
clusterName: clusterName,
@@ -389,10 +389,10 @@ func (f *sharedInformerFactory) Cluster(clusterName logicalcluster.Name) ScopedD
389389
390390
type scopedDynamicSharedInformerFactory struct {
391391
*sharedInformerFactory
392-
clusterName logicalcluster.Name
392+
clusterName {{.logicalclusterName|raw}}
393393
}
394394
395-
func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVersionResource) ({{.genericInformer|raw}}, error) {
395+
func (f *scopedDynamicSharedInformerFactory) ForResource(resource {{.schemaGroupVersionResource|raw}}) ({{.genericInformer|raw}}, error) {
396396
clusterInformer, err := f.sharedInformerFactory.ForResource(resource)
397397
if err != nil {
398398
return nil, err
@@ -491,17 +491,17 @@ func (f *sharedScopedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) m
491491
492492
res := map[{{.reflectType|raw}}]bool{}
493493
for informType, informer := range informers {
494-
res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
494+
res[informType] = {{.cacheWaitForCacheSync|raw}}(stopCh, informer.HasSynced)
495495
}
496496
return res
497497
}
498498
499499
// InformerFor returns the SharedIndexInformer for obj.
500-
func (f *sharedScopedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewScopedInformerFunc) {{.cacheSharedIndexInformer|raw}} {
500+
func (f *sharedScopedInformerFactory) InformerFor(obj {{.runtimeObject|raw}}, newFunc {{.interfacesNewScopedInformerFunc|raw}}) {{.cacheSharedIndexInformer|raw}} {
501501
f.lock.Lock()
502502
defer f.lock.Unlock()
503503
504-
informerType := reflect.TypeOf(obj)
504+
informerType := {{.reflectTypeOf|raw}}(obj)
505505
informer, exists := f.informers[informerType]
506506
if exists {
507507
return informer
@@ -523,7 +523,7 @@ func (f *sharedScopedInformerFactory) InformerFor(obj runtime.Object, newFunc in
523523
// API group versions, scoped to one workspace.
524524
type SharedScopedInformerFactory interface {
525525
{{.informerScopedFactoryInterface|raw}}
526-
ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
526+
ForResource(resource {{.schemaGroupVersionResource|raw}}) (GenericInformer, error)
527527
WaitForCacheSync(stopCh <-chan struct{}) map[{{.reflectType|raw}}]bool
528528
529529
{{range $groupName, $group := .groupVersions}}{{index $.gvGoNames $groupName}}() {{index $.gvInterfaces $groupName|raw}}

cmd/cluster-informer-gen/generators/factoryinterface.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (g *factoryInterfaceGenerator) GenerateType(c *generator.Context, t *types.
7070
"interface": c.Universe.Type(types.Name{Package: g.singleClusterVersionedClientSetPackage, Name: "Interface"}),
7171
"runtimeObject": c.Universe.Type(runtimeObject),
7272
"timeDuration": c.Universe.Type(timeDuration),
73-
"v1ListOptions": c.Universe.Type(v1ListOptions),
73+
"metav1ListOptions": c.Universe.Type(metav1ListOptions),
7474
}
7575

7676
sw.Do(externalSharedInformerFactoryInterface, m)
@@ -83,13 +83,13 @@ func (g *factoryInterfaceGenerator) GenerateType(c *generator.Context, t *types.
8383
}
8484

8585
var externalSharedInformerFactoryInterface = `
86-
// TweakListOptionsFunc is a function that transforms a $.v1ListOptions|raw$.
87-
type TweakListOptionsFunc func(*$.v1ListOptions|raw$)
86+
// TweakListOptionsFunc is a function that transforms a $.metav1ListOptions|raw$.
87+
type TweakListOptionsFunc func(*$.metav1ListOptions|raw$)
8888
8989
// NewInformerFunc takes $.clusterInterface|raw$ and $.timeDuration|raw$ to return a $.scopeableCacheSharedIndexInformer|raw$.
9090
type NewInformerFunc func($.clusterInterface|raw$, $.timeDuration|raw$) $.scopeableCacheSharedIndexInformer|raw$
9191
92-
// SharedInformerFactory a small interface to allow for adding an informer without an import cycle
92+
// SharedInformerFactory a small interface to allow for adding an informer without an import cycle.
9393
type SharedInformerFactory interface {
9494
Start(stopCh <-chan struct{})
9595
InformerFor(obj $.runtimeObject|raw$, newFunc NewInformerFunc) $.scopeableCacheSharedIndexInformer|raw$
@@ -98,9 +98,9 @@ type SharedInformerFactory interface {
9898

9999
var externalSharedScopedInformerFactoryInterface = `
100100
// NewScopedInformerFunc takes $.interface|raw$ and $.timeDuration|raw$ to return a SharedIndexInformer.
101-
type NewScopedInformerFunc func($.interface|raw$, time.Duration) $.cacheSharedIndexInformer|raw$
101+
type NewScopedInformerFunc func($.interface|raw$, $.timeDuration|raw$) $.cacheSharedIndexInformer|raw$
102102
103-
// SharedScopedInformerFactory a small interface to allow for adding an informer without an import cycle
103+
// SharedScopedInformerFactory a small interface to allow for adding an informer without an import cycle.
104104
type SharedScopedInformerFactory interface {
105105
Start(stopCh <-chan struct{})
106106
InformerFor(obj $.runtimeObject|raw$, newFunc NewScopedInformerFunc) $.cacheSharedIndexInformer|raw$

0 commit comments

Comments
 (0)