mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
code-gen: allow specifying custom resync periods for certain informer types and switch to functional option pattern for SharedInformerFactory
This commit is contained in:
parent
aaed11fed7
commit
4f4798a445
@ -89,6 +89,7 @@ func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
"syncMutex": c.Universe.Type(syncMutex),
|
"syncMutex": c.Universe.Type(syncMutex),
|
||||||
"timeDuration": c.Universe.Type(timeDuration),
|
"timeDuration": c.Universe.Type(timeDuration),
|
||||||
"namespaceAll": c.Universe.Type(metav1NamespaceAll),
|
"namespaceAll": c.Universe.Type(metav1NamespaceAll),
|
||||||
|
"object": c.Universe.Type(metav1Object),
|
||||||
}
|
}
|
||||||
|
|
||||||
sw.Do(sharedInformerFactoryStruct, m)
|
sw.Do(sharedInformerFactoryStruct, m)
|
||||||
@ -98,12 +99,16 @@ func (g *factoryGenerator) GenerateType(c *generator.Context, t *types.Type, w i
|
|||||||
}
|
}
|
||||||
|
|
||||||
var sharedInformerFactoryStruct = `
|
var sharedInformerFactoryStruct = `
|
||||||
|
// SharedInformerOption defines the functional option type for SharedInformerFactory.
|
||||||
|
type SharedInformerOption func(*sharedInformerFactory) *sharedInformerFactory
|
||||||
|
|
||||||
type sharedInformerFactory struct {
|
type sharedInformerFactory struct {
|
||||||
client {{.clientSetInterface|raw}}
|
client {{.clientSetInterface|raw}}
|
||||||
namespace string
|
namespace string
|
||||||
tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}
|
tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}
|
||||||
lock {{.syncMutex|raw}}
|
lock {{.syncMutex|raw}}
|
||||||
defaultResync {{.timeDuration|raw}}
|
defaultResync {{.timeDuration|raw}}
|
||||||
|
customResync map[{{.reflectType|raw}}]{{.timeDuration|raw}}
|
||||||
|
|
||||||
informers map[{{.reflectType|raw}}]{{.cacheSharedIndexInformer|raw}}
|
informers map[{{.reflectType|raw}}]{{.cacheSharedIndexInformer|raw}}
|
||||||
// startedInformers is used for tracking which informers have been started.
|
// startedInformers is used for tracking which informers have been started.
|
||||||
@ -111,23 +116,62 @@ type sharedInformerFactory struct {
|
|||||||
startedInformers map[{{.reflectType|raw}}]bool
|
startedInformers map[{{.reflectType|raw}}]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory
|
// WithCustomResyncConfig sets a custom resync period for the specified informer types.
|
||||||
|
func WithCustomResyncConfig(resyncConfig map[{{.object|raw}}]{{.timeDuration|raw}}) SharedInformerOption {
|
||||||
|
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||||
|
for k, v := range resyncConfig {
|
||||||
|
factory.customResync[reflect.TypeOf(k)] = v
|
||||||
|
}
|
||||||
|
return factory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.
|
||||||
|
func WithTweakListOptions(tweakListOptions internalinterfaces.TweakListOptionsFunc) SharedInformerOption {
|
||||||
|
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||||
|
factory.tweakListOptions = tweakListOptions
|
||||||
|
return factory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNamespace limits the SharedInformerFactory to the specified namespace.
|
||||||
|
func WithNamespace(namespace string) SharedInformerOption {
|
||||||
|
return func(factory *sharedInformerFactory) *sharedInformerFactory {
|
||||||
|
factory.namespace = namespace
|
||||||
|
return factory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
|
||||||
func NewSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}) SharedInformerFactory {
|
func NewSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}) SharedInformerFactory {
|
||||||
return NewFilteredSharedInformerFactory(client, defaultResync, {{.namespaceAll|raw}}, nil)
|
return NewSharedInformerFactoryWithOptions(client, defaultResync)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
|
// NewFilteredSharedInformerFactory constructs a new instance of sharedInformerFactory.
|
||||||
// Listers obtained via this SharedInformerFactory will be subject to the same filters
|
// Listers obtained via this SharedInformerFactory will be subject to the same filters
|
||||||
// as specified here.
|
// as specified here.
|
||||||
|
// Deprecated: Please use NewSharedInformerFactoryWithOptions instead
|
||||||
func NewFilteredSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}, namespace string, tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}) SharedInformerFactory {
|
func NewFilteredSharedInformerFactory(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}, namespace string, tweakListOptions {{.interfacesTweakListOptionsFunc|raw}}) SharedInformerFactory {
|
||||||
return &sharedInformerFactory{
|
return NewSharedInformerFactoryWithOptions(client, defaultResync, WithNamespace(namespace), WithTweakListOptions(tweakListOptions))
|
||||||
client: client,
|
}
|
||||||
namespace: namespace,
|
|
||||||
tweakListOptions: tweakListOptions,
|
// NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
|
||||||
defaultResync: defaultResync,
|
func NewSharedInformerFactoryWithOptions(client {{.clientSetInterface|raw}}, defaultResync {{.timeDuration|raw}}, options ...SharedInformerOption) SharedInformerFactory {
|
||||||
informers: make(map[{{.reflectType|raw}}]{{.cacheSharedIndexInformer|raw}}),
|
factory := &sharedInformerFactory{
|
||||||
startedInformers: make(map[{{.reflectType|raw}}]bool),
|
client: client,
|
||||||
}
|
namespace: v1.NamespaceAll,
|
||||||
|
defaultResync: defaultResync,
|
||||||
|
informers: make(map[{{.reflectType|raw}}]{{.cacheSharedIndexInformer|raw}}),
|
||||||
|
startedInformers: make(map[{{.reflectType|raw}}]bool),
|
||||||
|
customResync: make(map[{{.reflectType|raw}}]{{.timeDuration|raw}}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply all options
|
||||||
|
for _, opt := range options {
|
||||||
|
factory = opt(factory)
|
||||||
|
}
|
||||||
|
|
||||||
|
return factory
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start initializes all requested informers.
|
// Start initializes all requested informers.
|
||||||
@ -176,7 +220,13 @@ func (f *sharedInformerFactory) InformerFor(obj {{.runtimeObject|raw}}, newFunc
|
|||||||
if exists {
|
if exists {
|
||||||
return informer
|
return informer
|
||||||
}
|
}
|
||||||
informer = newFunc(f.client, f.defaultResync)
|
|
||||||
|
resyncPeriod, exists := f.customResync[informerType]
|
||||||
|
if !exists {
|
||||||
|
resyncPeriod = f.defaultResync
|
||||||
|
}
|
||||||
|
|
||||||
|
informer = newFunc(f.client, resyncPeriod)
|
||||||
f.informers[informerType] = informer
|
f.informers[informerType] = informer
|
||||||
|
|
||||||
return informer
|
return informer
|
||||||
|
@ -37,5 +37,6 @@ var (
|
|||||||
timeDuration = types.Name{Package: "time", Name: "Duration"}
|
timeDuration = types.Name{Package: "time", Name: "Duration"}
|
||||||
v1ListOptions = types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListOptions"}
|
v1ListOptions = types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ListOptions"}
|
||||||
metav1NamespaceAll = types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "NamespaceAll"}
|
metav1NamespaceAll = types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "NamespaceAll"}
|
||||||
|
metav1Object = types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "Object"}
|
||||||
watchInterface = types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}
|
watchInterface = types.Name{Package: "k8s.io/apimachinery/pkg/watch", Name: "Interface"}
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user