Add RealFIFOOptions struct to provide structured configuration for RealFIFO

Signed-off-by: xigang <wangxigang2014@gmail.com>

Kubernetes-commit: 26bbea8c07131080f763c4ccc1eda5daa66803a6
This commit is contained in:
xigang
2025-06-12 08:19:21 +08:00
committed by Kubernetes Publisher
parent f49b36f94e
commit 00a491e0ce
3 changed files with 49 additions and 7 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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
}