Merge pull request #118455 from linxiulei/managedFields

Trim managedFields in controller-manager

Kubernetes-commit: 1acd489dca47a4be0301330cbfcf4e8f9d98f7c0
This commit is contained in:
Kubernetes Publisher
2023-08-15 15:17:12 -07:00
2 changed files with 42 additions and 0 deletions

View File

@@ -60,6 +60,7 @@ 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.
@@ -98,6 +99,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 kubernetes.Interface, defaultResync time.Duration) SharedInformerFactory {
return NewSharedInformerFactoryWithOptions(client, defaultResync)
@@ -202,6 +211,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

View File

@@ -21,6 +21,7 @@ import (
"sync"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -31,6 +32,17 @@ import (
"k8s.io/client-go/tools/cache"
)
// SharedInformerOption defines the functional option type for metadataSharedInformerFactory.
type SharedInformerOption func(*metadataSharedInformerFactory) *metadataSharedInformerFactory
// WithTransform sets a transform on all informers.
func WithTransform(transform cache.TransformFunc) SharedInformerOption {
return func(factory *metadataSharedInformerFactory) *metadataSharedInformerFactory {
factory.transform = transform
return factory
}
}
// NewSharedInformerFactory constructs a new instance of metadataSharedInformerFactory for all namespaces.
func NewSharedInformerFactory(client metadata.Interface, defaultResync time.Duration) SharedInformerFactory {
return NewFilteredSharedInformerFactory(client, defaultResync, metav1.NamespaceAll, nil)
@@ -49,10 +61,29 @@ func NewFilteredSharedInformerFactory(client metadata.Interface, defaultResync t
}
}
// NewSharedInformerFactoryWithOptions constructs a new instance of metadataSharedInformerFactory with additional options.
func NewSharedInformerFactoryWithOptions(client metadata.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
factory := &metadataSharedInformerFactory{
client: client,
namespace: v1.NamespaceAll,
defaultResync: defaultResync,
informers: map[schema.GroupVersionResource]informers.GenericInformer{},
startedInformers: make(map[schema.GroupVersionResource]bool),
}
// Apply all options
for _, opt := range options {
factory = opt(factory)
}
return factory
}
type metadataSharedInformerFactory struct {
client metadata.Interface
defaultResync time.Duration
namespace string
transform cache.TransformFunc
lock sync.Mutex
informers map[schema.GroupVersionResource]informers.GenericInformer
@@ -80,6 +111,7 @@ func (f *metadataSharedInformerFactory) ForResource(gvr schema.GroupVersionResou
}
informer = NewFilteredMetadataInformer(f.client, gvr, f.namespace, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
informer.Informer().SetTransform(f.transform)
f.informers[key] = informer
return informer