From 00a491e0ceb34af56b292c8e19f762d64a86658e Mon Sep 17 00:00:00 2001 From: xigang Date: Thu, 12 Jun 2025 08:19:21 +0800 Subject: [PATCH] Add RealFIFOOptions struct to provide structured configuration for RealFIFO Signed-off-by: xigang Kubernetes-commit: 26bbea8c07131080f763c4ccc1eda5daa66803a6 --- tools/cache/controller.go | 8 +++++-- tools/cache/shared_informer.go | 6 ++++- tools/cache/the_real_fifo.go | 42 ++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/tools/cache/controller.go b/tools/cache/controller.go index 5f983b6b6..e8c99aa6a 100644 --- a/tools/cache/controller.go +++ b/tools/cache/controller.go @@ -19,13 +19,13 @@ package cache import ( "context" "errors" - clientgofeaturegate "k8s.io/client-go/features" "sync" "time" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" + clientgofeaturegate "k8s.io/client-go/features" "k8s.io/utils/clock" ) @@ -598,7 +598,11 @@ func newInformer(clientState Store, options InformerOptions) Controller { var fifo Queue if clientgofeaturegate.FeatureGates().Enabled(clientgofeaturegate.InOrderInformers) { - fifo = NewRealFIFO(MetaNamespaceKeyFunc, clientState, options.Transform) + fifo = NewRealFIFOWithOptions(RealFIFOOptions{ + KeyFunction: MetaNamespaceKeyFunc, + KnownObjects: clientState, + Transformer: options.Transform, + }) } else { fifo = NewDeltaFIFOWithOptions(DeltaFIFOOptions{ KnownObjects: clientState, diff --git a/tools/cache/shared_informer.go b/tools/cache/shared_informer.go index 99e5fcd18..959fb19ba 100644 --- a/tools/cache/shared_informer.go +++ b/tools/cache/shared_informer.go @@ -541,7 +541,11 @@ func (s *sharedIndexInformer) RunWithContext(ctx context.Context) { var fifo Queue if clientgofeaturegate.FeatureGates().Enabled(clientgofeaturegate.InOrderInformers) { - fifo = NewRealFIFO(MetaNamespaceKeyFunc, s.indexer, s.transform) + fifo = NewRealFIFOWithOptions(RealFIFOOptions{ + KeyFunction: MetaNamespaceKeyFunc, + KnownObjects: s.indexer, + Transformer: s.transform, + }) } else { fifo = NewDeltaFIFOWithOptions(DeltaFIFOOptions{ KnownObjects: s.indexer, diff --git a/tools/cache/the_real_fifo.go b/tools/cache/the_real_fifo.go index ef322bea8..9295d3021 100644 --- a/tools/cache/the_real_fifo.go +++ b/tools/cache/the_real_fifo.go @@ -26,6 +26,25 @@ import ( utiltrace "k8s.io/utils/trace" ) +// RealFIFOOptions is the configuration parameters for RealFIFO. +type RealFIFOOptions struct { + // KeyFunction is used to figure out what key an object should have. (It's + // exposed in the returned RealFIFO's keyOf() method, with additional + // handling around deleted objects and queue state). + // Optional, the default is MetaNamespaceKeyFunc. + KeyFunction KeyFunc + + // KnownObjects is expected to return a list of keys that the consumer of + // this queue "knows about". It is used to decide which items are missing + // when Replace() is called; 'Deleted' deltas are produced for the missing items. + // KnownObjects is required. + KnownObjects KeyListerGetter + + // If set, will be called for objects before enqueueing them. Please + // see the comment on TransformFunc for details. + Transformer TransformFunc +} + // RealFIFO is a Queue in which every notification from the Reflector is passed // in order to the Queue via Pop. // This means that it @@ -398,16 +417,31 @@ func (f *RealFIFO) Transformer() TransformFunc { // NewRealFIFO returns a Store which can be used to queue up items to // process. func NewRealFIFO(keyFunc KeyFunc, knownObjects KeyListerGetter, transformer TransformFunc) *RealFIFO { - if knownObjects == nil { + return NewRealFIFOWithOptions(RealFIFOOptions{ + KeyFunction: keyFunc, + KnownObjects: knownObjects, + Transformer: transformer, + }) +} + +// NewRealFIFOWithOptions returns a Queue which can be used to process changes to +// items. See also the comment on RealFIFO. +func NewRealFIFOWithOptions(opts RealFIFOOptions) *RealFIFO { + if opts.KeyFunction == nil { + opts.KeyFunction = MetaNamespaceKeyFunc + } + + if opts.KnownObjects == nil { panic("coding error: knownObjects must be provided") } f := &RealFIFO{ items: make([]Delta, 0, 10), - keyFunc: keyFunc, - knownObjects: knownObjects, - transformer: transformer, + keyFunc: opts.KeyFunction, + knownObjects: opts.KnownObjects, + transformer: opts.Transformer, } + f.cond.L = &f.lock return f }