2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-06-01 17:01:19 +00:00
|
|
|
"net/http"
|
2024-04-08 20:57:45 +00:00
|
|
|
"sort"
|
2020-10-21 05:49:41 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-25 09:53:23 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-08-28 20:44:55 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2023-10-25 09:53:23 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-02-01 17:06:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
toolscache "k8s.io/client-go/tools/cache"
|
2024-02-01 13:08:54 +00:00
|
|
|
"k8s.io/utils/ptr"
|
2023-02-01 17:06:36 +00:00
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/cache/internal"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
|
|
|
)
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
var (
|
|
|
|
defaultSyncPeriod = 10 * time.Hour
|
|
|
|
)
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// InformerGetOptions defines the behavior of how informers are retrieved.
|
|
|
|
type InformerGetOptions internal.GetOptions
|
|
|
|
|
|
|
|
// InformerGetOption defines an option that alters the behavior of how informers are retrieved.
|
|
|
|
type InformerGetOption func(*InformerGetOptions)
|
|
|
|
|
|
|
|
// BlockUntilSynced determines whether a get request for an informer should block
|
|
|
|
// until the informer's cache has synced.
|
|
|
|
func BlockUntilSynced(shouldBlock bool) InformerGetOption {
|
|
|
|
return func(opts *InformerGetOptions) {
|
|
|
|
opts.BlockUntilSynced = &shouldBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Cache knows how to load Kubernetes objects, fetch informers to request
|
|
|
|
// to receive events for Kubernetes objects (at a low-level),
|
|
|
|
// and add indices to fields on the objects stored in the cache.
|
|
|
|
type Cache interface {
|
2023-08-28 20:44:55 +00:00
|
|
|
// Reader acts as a client to objects stored in the cache.
|
2020-10-21 05:49:41 +00:00
|
|
|
client.Reader
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Informers loads informers and adds field indices.
|
2020-10-21 05:49:41 +00:00
|
|
|
Informers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Informers knows how to create or fetch informers for different
|
|
|
|
// group-version-kinds, and add indices to those informers. It's safe to call
|
|
|
|
// GetInformer from multiple threads.
|
|
|
|
type Informers interface {
|
|
|
|
// GetInformer fetches or constructs an informer for the given object that corresponds to a single
|
|
|
|
// API kind and resource.
|
2023-08-28 20:44:55 +00:00
|
|
|
GetInformer(ctx context.Context, obj client.Object, opts ...InformerGetOption) (Informer, error)
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// GetInformerForKind is similar to GetInformer, except that it takes a group-version-kind, instead
|
|
|
|
// of the underlying object.
|
2023-08-28 20:44:55 +00:00
|
|
|
GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...InformerGetOption) (Informer, error)
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2024-02-01 13:08:54 +00:00
|
|
|
// RemoveInformer removes an informer entry and stops it if it was running.
|
|
|
|
RemoveInformer(ctx context.Context, obj client.Object) error
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Start runs all the informers known to this cache until the context is closed.
|
2020-10-21 05:49:41 +00:00
|
|
|
// It blocks.
|
2021-06-25 05:02:01 +00:00
|
|
|
Start(ctx context.Context) error
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// WaitForCacheSync waits for all the caches to sync. Returns false if it could not sync a cache.
|
2021-06-25 05:02:01 +00:00
|
|
|
WaitForCacheSync(ctx context.Context) bool
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// FieldIndexer adds indices to the managed informers.
|
2020-10-21 05:49:41 +00:00
|
|
|
client.FieldIndexer
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Informer allows you to interact with the underlying informer.
|
2020-10-21 05:49:41 +00:00
|
|
|
type Informer interface {
|
|
|
|
// AddEventHandler adds an event handler to the shared informer using the shared informer's resync
|
2023-08-28 20:44:55 +00:00
|
|
|
// period. Events to a single handler are delivered sequentially, but there is no coordination
|
2020-10-21 05:49:41 +00:00
|
|
|
// between different handlers.
|
2023-02-01 17:06:36 +00:00
|
|
|
// It returns a registration handle for the handler that can be used to remove
|
2023-08-28 20:44:55 +00:00
|
|
|
// the handler again and an error if the handler cannot be added.
|
2023-02-01 17:06:36 +00:00
|
|
|
AddEventHandler(handler toolscache.ResourceEventHandler) (toolscache.ResourceEventHandlerRegistration, error)
|
2023-08-28 20:44:55 +00:00
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// AddEventHandlerWithResyncPeriod adds an event handler to the shared informer using the
|
2023-08-28 20:44:55 +00:00
|
|
|
// specified resync period. Events to a single handler are delivered sequentially, but there is
|
2020-10-21 05:49:41 +00:00
|
|
|
// no coordination between different handlers.
|
2023-02-01 17:06:36 +00:00
|
|
|
// It returns a registration handle for the handler that can be used to remove
|
|
|
|
// the handler again and an error if the handler cannot be added.
|
|
|
|
AddEventHandlerWithResyncPeriod(handler toolscache.ResourceEventHandler, resyncPeriod time.Duration) (toolscache.ResourceEventHandlerRegistration, error)
|
2023-08-28 20:44:55 +00:00
|
|
|
|
|
|
|
// RemoveEventHandler removes a previously added event handler given by
|
2023-02-01 17:06:36 +00:00
|
|
|
// its registration handle.
|
2023-08-28 20:44:55 +00:00
|
|
|
// This function is guaranteed to be idempotent and thread-safe.
|
2023-02-01 17:06:36 +00:00
|
|
|
RemoveEventHandler(handle toolscache.ResourceEventHandlerRegistration) error
|
2023-08-28 20:44:55 +00:00
|
|
|
|
2024-08-19 08:02:11 +00:00
|
|
|
// AddIndexers adds indexers to this store. It is valid to add indexers
|
|
|
|
// after an informer was started.
|
2020-10-21 05:49:41 +00:00
|
|
|
AddIndexers(indexers toolscache.Indexers) error
|
2023-08-28 20:44:55 +00:00
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// HasSynced return true if the informers underlying store has synced.
|
2020-10-21 05:49:41 +00:00
|
|
|
HasSynced() bool
|
2024-02-01 13:08:54 +00:00
|
|
|
// IsStopped returns true if the informer has been stopped.
|
|
|
|
IsStopped() bool
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 09:53:23 +00:00
|
|
|
// AllNamespaces should be used as the map key to deliminate namespace settings
|
|
|
|
// that apply to all namespaces that themselves do not have explicit settings.
|
|
|
|
const AllNamespaces = metav1.NamespaceAll
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Options are the optional arguments for creating a new Cache object.
|
2020-10-21 05:49:41 +00:00
|
|
|
type Options struct {
|
2023-06-01 17:01:19 +00:00
|
|
|
// HTTPClient is the http client to use for the REST client
|
|
|
|
HTTPClient *http.Client
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
|
|
|
|
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
|
|
|
|
Mapper meta.RESTMapper
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// SyncPeriod determines the minimum frequency at which watched resources are
|
|
|
|
// reconciled. A lower period will correct entropy more quickly, but reduce
|
|
|
|
// responsiveness to change if there are many watched resources. Change this
|
|
|
|
// value only if you know what you are doing. Defaults to 10 hours if unset.
|
|
|
|
// there will a 10 percent jitter between the SyncPeriod of all controllers
|
|
|
|
// so that all controllers will not send list requests simultaneously.
|
|
|
|
//
|
|
|
|
// This applies to all controllers.
|
|
|
|
//
|
|
|
|
// A period sync happens for two reasons:
|
|
|
|
// 1. To insure against a bug in the controller that causes an object to not
|
|
|
|
// be requeued, when it otherwise should be requeued.
|
|
|
|
// 2. To insure against an unknown bug in controller-runtime, or its dependencies,
|
|
|
|
// that causes an object to not be requeued, when it otherwise should be
|
|
|
|
// requeued, or to be removed from the queue, when it otherwise should not
|
|
|
|
// be removed.
|
|
|
|
//
|
|
|
|
// If you want
|
|
|
|
// 1. to insure against missed watch events, or
|
|
|
|
// 2. to poll services that cannot be watched,
|
|
|
|
// then we recommend that, instead of changing the default period, the
|
|
|
|
// controller requeue, with a constant duration `t`, whenever the controller
|
|
|
|
// is "done" with an object, and would otherwise not requeue it, i.e., we
|
|
|
|
// recommend the `Reconcile` function return `reconcile.Result{RequeueAfter: t}`,
|
|
|
|
// instead of `reconcile.Result{}`.
|
|
|
|
SyncPeriod *time.Duration
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// ReaderFailOnMissingInformer configures the cache to return a ErrResourceNotCached error when a user
|
|
|
|
// requests, using Get() and List(), a resource the cache does not already have an informer for.
|
|
|
|
//
|
|
|
|
// This error is distinct from an errors.NotFound.
|
|
|
|
//
|
|
|
|
// Defaults to false, which means that the cache will start a new informer
|
|
|
|
// for every new requested resource.
|
|
|
|
ReaderFailOnMissingInformer bool
|
|
|
|
|
|
|
|
// DefaultNamespaces maps namespace names to cache configs. If set, only
|
|
|
|
// the namespaces in here will be watched and it will by used to default
|
|
|
|
// ByObject.Namespaces for all objects if that is nil.
|
|
|
|
//
|
2023-10-25 09:53:23 +00:00
|
|
|
// It is possible to have specific Config for just some namespaces
|
|
|
|
// but cache all namespaces by using the AllNamespaces const as the map key.
|
|
|
|
// This will then include all namespaces that do not have a more specific
|
|
|
|
// setting.
|
|
|
|
//
|
2023-08-28 20:44:55 +00:00
|
|
|
// The options in the Config that are nil will be defaulted from
|
|
|
|
// the respective Default* settings.
|
|
|
|
DefaultNamespaces map[string]Config
|
2021-06-25 05:02:01 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// DefaultLabelSelector will be used as a label selector for all objects
|
|
|
|
// unless there is already one set in ByObject or DefaultNamespaces.
|
2023-06-01 17:01:19 +00:00
|
|
|
DefaultLabelSelector labels.Selector
|
2021-09-02 12:01:06 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// DefaultFieldSelector will be used as a field selector for all object types
|
|
|
|
// unless there is already one set in ByObject or DefaultNamespaces.
|
2023-06-01 17:01:19 +00:00
|
|
|
DefaultFieldSelector fields.Selector
|
2021-12-08 13:50:47 +00:00
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// DefaultTransform will be used as transform for all object types
|
2023-08-28 20:44:55 +00:00
|
|
|
// unless there is already one set in ByObject or DefaultNamespaces.
|
2024-05-13 20:57:03 +00:00
|
|
|
//
|
|
|
|
// A typical usecase for this is to use TransformStripManagedFields
|
|
|
|
// to reduce the caches memory usage.
|
2023-06-01 17:01:19 +00:00
|
|
|
DefaultTransform toolscache.TransformFunc
|
|
|
|
|
2024-02-01 13:08:54 +00:00
|
|
|
// DefaultWatchErrorHandler will be used to the WatchErrorHandler which is called
|
|
|
|
// whenever ListAndWatch drops the connection with an error.
|
|
|
|
//
|
|
|
|
// After calling this handler, the informer will backoff and retry.
|
|
|
|
DefaultWatchErrorHandler toolscache.WatchErrorHandler
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// DefaultUnsafeDisableDeepCopy is the default for UnsafeDisableDeepCopy
|
|
|
|
// for everything that doesn't specify this.
|
|
|
|
//
|
2021-09-02 12:01:06 +00:00
|
|
|
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
|
|
|
|
// otherwise you will mutate the object in the cache.
|
2023-06-01 17:01:19 +00:00
|
|
|
//
|
2023-08-28 20:44:55 +00:00
|
|
|
// This will be used for all object types, unless it is set in ByObject or
|
|
|
|
// DefaultNamespaces.
|
|
|
|
DefaultUnsafeDisableDeepCopy *bool
|
|
|
|
|
2024-11-25 20:35:21 +00:00
|
|
|
// DefaultEnableWatchBookmarks requests watch events with type "BOOKMARK".
|
|
|
|
// Servers that do not implement bookmarks may ignore this flag and
|
|
|
|
// bookmarks are sent at the server's discretion. Clients should not
|
|
|
|
// assume bookmarks are returned at any specific interval, nor may they
|
|
|
|
// assume the server will send any BOOKMARK event during a session.
|
|
|
|
//
|
|
|
|
// This will be used for all object types, unless it is set in ByObject or
|
|
|
|
// DefaultNamespaces.
|
|
|
|
//
|
|
|
|
// Defaults to false.
|
|
|
|
DefaultEnableWatchBookmarks *bool
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// ByObject restricts the cache's ListWatch to the desired fields per GVK at the specified object.
|
2024-05-13 20:57:03 +00:00
|
|
|
// If unset, this will fall through to the Default* settings.
|
2023-08-28 20:44:55 +00:00
|
|
|
ByObject map[client.Object]ByObject
|
|
|
|
|
|
|
|
// newInformer allows overriding of NewSharedIndexInformer for testing.
|
|
|
|
newInformer *func(toolscache.ListerWatcher, runtime.Object, time.Duration, toolscache.Indexers) toolscache.SharedIndexInformer
|
2023-06-01 17:01:19 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// ByObject offers more fine-grained control over the cache's ListWatch by object.
|
|
|
|
type ByObject struct {
|
2023-08-28 20:44:55 +00:00
|
|
|
// Namespaces maps a namespace name to cache configs. If set, only the
|
|
|
|
// namespaces in this map will be cached.
|
|
|
|
//
|
|
|
|
// Settings in the map value that are unset will be defaulted.
|
|
|
|
// Use an empty value for the specific setting to prevent that.
|
|
|
|
//
|
2023-10-25 09:53:23 +00:00
|
|
|
// It is possible to have specific Config for just some namespaces
|
|
|
|
// but cache all namespaces by using the AllNamespaces const as the map key.
|
|
|
|
// This will then include all namespaces that do not have a more specific
|
|
|
|
// setting.
|
|
|
|
//
|
2023-08-28 20:44:55 +00:00
|
|
|
// A nil map allows to default this to the cache's DefaultNamespaces setting.
|
|
|
|
// An empty map prevents this and means that all namespaces will be cached.
|
|
|
|
//
|
|
|
|
// The defaulting follows the following precedence order:
|
|
|
|
// 1. ByObject
|
|
|
|
// 2. DefaultNamespaces[namespace]
|
|
|
|
// 3. Default*
|
|
|
|
//
|
|
|
|
// This must be unset for cluster-scoped objects.
|
|
|
|
Namespaces map[string]Config
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Label represents a label selector for the object.
|
|
|
|
Label labels.Selector
|
|
|
|
|
|
|
|
// Field represents a field selector for the object.
|
|
|
|
Field fields.Selector
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Transform is a transformer function for the object which gets applied
|
|
|
|
// when objects of the transformation are about to be committed to the cache.
|
2023-02-01 17:06:36 +00:00
|
|
|
//
|
|
|
|
// This function is called both for new objects to enter the cache,
|
2023-06-01 17:01:19 +00:00
|
|
|
// and for updated objects.
|
|
|
|
Transform toolscache.TransformFunc
|
2023-02-01 17:06:36 +00:00
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// UnsafeDisableDeepCopy indicates not to deep copy objects during get or
|
|
|
|
// list objects per GVK at the specified object.
|
|
|
|
// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
|
|
|
|
// otherwise you will mutate the object in the cache.
|
|
|
|
UnsafeDisableDeepCopy *bool
|
2024-11-25 20:35:21 +00:00
|
|
|
|
|
|
|
// EnableWatchBookmarks requests watch events with type "BOOKMARK".
|
|
|
|
// Servers that do not implement bookmarks may ignore this flag and
|
|
|
|
// bookmarks are sent at the server's discretion. Clients should not
|
|
|
|
// assume bookmarks are returned at any specific interval, nor may they
|
|
|
|
// assume the server will send any BOOKMARK event during a session.
|
|
|
|
//
|
|
|
|
// Defaults to false.
|
|
|
|
EnableWatchBookmarks *bool
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Config describes all potential options for a given watch.
|
|
|
|
type Config struct {
|
|
|
|
// LabelSelector specifies a label selector. A nil value allows to
|
|
|
|
// default this.
|
|
|
|
//
|
|
|
|
// Set to labels.Everything() if you don't want this defaulted.
|
|
|
|
LabelSelector labels.Selector
|
|
|
|
|
|
|
|
// FieldSelector specifics a field selector. A nil value allows to
|
|
|
|
// default this.
|
|
|
|
//
|
|
|
|
// Set to fields.Everything() if you don't want this defaulted.
|
|
|
|
FieldSelector fields.Selector
|
|
|
|
|
|
|
|
// Transform specifies a transform func. A nil value allows to default
|
|
|
|
// this.
|
|
|
|
//
|
|
|
|
// Set to an empty func to prevent this:
|
|
|
|
// func(in interface{}) (interface{}, error) { return in, nil }
|
|
|
|
Transform toolscache.TransformFunc
|
|
|
|
|
|
|
|
// UnsafeDisableDeepCopy specifies if List and Get requests against the
|
|
|
|
// cache should not DeepCopy. A nil value allows to default this.
|
|
|
|
UnsafeDisableDeepCopy *bool
|
2024-11-25 20:35:21 +00:00
|
|
|
|
|
|
|
// EnableWatchBookmarks requests watch events with type "BOOKMARK".
|
|
|
|
// Servers that do not implement bookmarks may ignore this flag and
|
|
|
|
// bookmarks are sent at the server's discretion. Clients should not
|
|
|
|
// assume bookmarks are returned at any specific interval, nor may they
|
|
|
|
// assume the server will send any BOOKMARK event during a session.
|
|
|
|
//
|
|
|
|
// Defaults to false.
|
|
|
|
EnableWatchBookmarks *bool
|
2023-08-28 20:44:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// NewCacheFunc - Function for creating a new cache from the options and a rest config.
|
|
|
|
type NewCacheFunc func(config *rest.Config, opts Options) (Cache, error)
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
// New initializes and returns a new Cache.
|
2023-08-28 20:44:55 +00:00
|
|
|
func New(cfg *rest.Config, opts Options) (Cache, error) {
|
|
|
|
opts, err := defaultOpts(cfg, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newCacheFunc := newCache(cfg, opts)
|
|
|
|
|
|
|
|
var defaultCache Cache
|
|
|
|
if len(opts.DefaultNamespaces) > 0 {
|
|
|
|
defaultConfig := optionDefaultsToConfig(&opts)
|
|
|
|
defaultCache = newMultiNamespaceCache(newCacheFunc, opts.Scheme, opts.Mapper, opts.DefaultNamespaces, &defaultConfig)
|
|
|
|
} else {
|
|
|
|
defaultCache = newCacheFunc(optionDefaultsToConfig(&opts), corev1.NamespaceAll)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
2023-08-28 20:44:55 +00:00
|
|
|
|
|
|
|
if len(opts.ByObject) == 0 {
|
|
|
|
return defaultCache, nil
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
delegating := &delegatingByGVKCache{
|
|
|
|
scheme: opts.Scheme,
|
|
|
|
caches: make(map[schema.GroupVersionKind]Cache, len(opts.ByObject)),
|
|
|
|
defaultCache: defaultCache,
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
for obj, config := range opts.ByObject {
|
|
|
|
gvk, err := apiutil.GVKForObject(obj, opts.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get GVK for type %T: %w", obj, err)
|
|
|
|
}
|
|
|
|
var cache Cache
|
|
|
|
if len(config.Namespaces) > 0 {
|
|
|
|
cache = newMultiNamespaceCache(newCacheFunc, opts.Scheme, opts.Mapper, config.Namespaces, nil)
|
|
|
|
} else {
|
|
|
|
cache = newCacheFunc(byObjectToConfig(config), corev1.NamespaceAll)
|
|
|
|
}
|
|
|
|
delegating.caches[gvk] = cache
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
2023-08-28 20:44:55 +00:00
|
|
|
|
|
|
|
return delegating, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// TransformStripManagedFields strips the managed fields of an object before it is committed to the cache.
|
|
|
|
// If you are not explicitly accessing managedFields from your code, setting this as `DefaultTransform`
|
|
|
|
// on the cache can lead to a significant reduction in memory usage.
|
|
|
|
func TransformStripManagedFields() toolscache.TransformFunc {
|
|
|
|
return func(in any) (any, error) {
|
|
|
|
// Nilcheck managed fields to avoid hitting https://github.com/kubernetes/kubernetes/issues/124337
|
|
|
|
if obj, err := meta.Accessor(in); err == nil && obj.GetManagedFields() != nil {
|
|
|
|
obj.SetManagedFields(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
func optionDefaultsToConfig(opts *Options) Config {
|
|
|
|
return Config{
|
|
|
|
LabelSelector: opts.DefaultLabelSelector,
|
|
|
|
FieldSelector: opts.DefaultFieldSelector,
|
2023-06-01 17:01:19 +00:00
|
|
|
Transform: opts.DefaultTransform,
|
2023-08-28 20:44:55 +00:00
|
|
|
UnsafeDisableDeepCopy: opts.DefaultUnsafeDisableDeepCopy,
|
2024-11-25 20:35:21 +00:00
|
|
|
EnableWatchBookmarks: opts.DefaultEnableWatchBookmarks,
|
2023-06-01 17:01:19 +00:00
|
|
|
}
|
2023-08-28 20:44:55 +00:00
|
|
|
}
|
2023-06-01 17:01:19 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
func byObjectToConfig(byObject ByObject) Config {
|
|
|
|
return Config{
|
|
|
|
LabelSelector: byObject.Label,
|
|
|
|
FieldSelector: byObject.Field,
|
|
|
|
Transform: byObject.Transform,
|
|
|
|
UnsafeDisableDeepCopy: byObject.UnsafeDisableDeepCopy,
|
2024-11-25 20:35:21 +00:00
|
|
|
EnableWatchBookmarks: byObject.EnableWatchBookmarks,
|
2023-08-28 20:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type newCacheFunc func(config Config, namespace string) Cache
|
|
|
|
|
|
|
|
func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
|
|
|
|
return func(config Config, namespace string) Cache {
|
|
|
|
return &informerCache{
|
|
|
|
scheme: opts.Scheme,
|
|
|
|
Informers: internal.NewInformers(restConfig, &internal.InformersOpts{
|
|
|
|
HTTPClient: opts.HTTPClient,
|
|
|
|
Scheme: opts.Scheme,
|
|
|
|
Mapper: opts.Mapper,
|
|
|
|
ResyncPeriod: *opts.SyncPeriod,
|
|
|
|
Namespace: namespace,
|
|
|
|
Selector: internal.Selector{
|
|
|
|
Label: config.LabelSelector,
|
|
|
|
Field: config.FieldSelector,
|
|
|
|
},
|
|
|
|
Transform: config.Transform,
|
2024-02-01 13:08:54 +00:00
|
|
|
WatchErrorHandler: opts.DefaultWatchErrorHandler,
|
|
|
|
UnsafeDisableDeepCopy: ptr.Deref(config.UnsafeDisableDeepCopy, false),
|
2024-11-25 20:35:21 +00:00
|
|
|
EnableWatchBookmarks: ptr.Deref(config.EnableWatchBookmarks, false),
|
2023-08-28 20:44:55 +00:00
|
|
|
NewInformer: opts.newInformer,
|
|
|
|
}),
|
|
|
|
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
func defaultOpts(config *rest.Config, opts Options) (Options, error) {
|
2023-08-14 20:06:34 +00:00
|
|
|
config = rest.CopyConfig(config)
|
|
|
|
if config.UserAgent == "" {
|
|
|
|
config.UserAgent = rest.DefaultKubernetesUserAgent()
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Use the rest HTTP client for the provided config if unset
|
|
|
|
if opts.HTTPClient == nil {
|
|
|
|
var err error
|
|
|
|
opts.HTTPClient, err = rest.HTTPClientFor(config)
|
2023-02-01 17:06:36 +00:00
|
|
|
if err != nil {
|
2023-08-28 20:44:55 +00:00
|
|
|
return Options{}, fmt.Errorf("could not create HTTP client from config: %w", err)
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Use the default Kubernetes Scheme if unset
|
|
|
|
if opts.Scheme == nil {
|
|
|
|
opts.Scheme = scheme.Scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a new Mapper if unset
|
|
|
|
if opts.Mapper == nil {
|
|
|
|
var err error
|
2024-02-01 13:08:54 +00:00
|
|
|
opts.Mapper, err = apiutil.NewDynamicRESTMapper(config, opts.HTTPClient)
|
2020-10-21 05:49:41 +00:00
|
|
|
if err != nil {
|
2023-08-28 20:44:55 +00:00
|
|
|
return Options{}, fmt.Errorf("could not create RESTMapper from config: %w", err)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
for obj, byObject := range opts.ByObject {
|
|
|
|
isNamespaced, err := apiutil.IsObjectNamespaced(obj, opts.Scheme, opts.Mapper)
|
|
|
|
if err != nil {
|
|
|
|
return opts, fmt.Errorf("failed to determine if %T is namespaced: %w", obj, err)
|
|
|
|
}
|
|
|
|
if !isNamespaced && byObject.Namespaces != nil {
|
|
|
|
return opts, fmt.Errorf("type %T is not namespaced, but its ByObject.Namespaces setting is not nil", obj)
|
|
|
|
}
|
|
|
|
|
2024-04-08 20:57:45 +00:00
|
|
|
if isNamespaced && byObject.Namespaces == nil {
|
|
|
|
byObject.Namespaces = maps.Clone(opts.DefaultNamespaces)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default the namespace-level configs first, because they need to use the undefaulted type-level config
|
|
|
|
// to be able to potentially fall through to settings from DefaultNamespaces.
|
2023-08-28 20:44:55 +00:00
|
|
|
for namespace, config := range byObject.Namespaces {
|
|
|
|
// 1. Default from the undefaulted type-level config
|
|
|
|
config = defaultConfig(config, byObjectToConfig(byObject))
|
|
|
|
|
|
|
|
// 2. Default from the namespace-level config. This was defaulted from the global default config earlier, but
|
|
|
|
// might not have an entry for the current namespace.
|
|
|
|
if defaultNamespaceSettings, hasDefaultNamespace := opts.DefaultNamespaces[namespace]; hasDefaultNamespace {
|
|
|
|
config = defaultConfig(config, defaultNamespaceSettings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Default from the global defaults
|
|
|
|
config = defaultConfig(config, optionDefaultsToConfig(&opts))
|
|
|
|
|
2023-10-25 09:53:23 +00:00
|
|
|
if namespace == metav1.NamespaceAll {
|
|
|
|
config.FieldSelector = fields.AndSelectors(
|
|
|
|
appendIfNotNil(
|
|
|
|
namespaceAllSelector(maps.Keys(byObject.Namespaces)),
|
|
|
|
config.FieldSelector,
|
|
|
|
)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
byObject.Namespaces[namespace] = config
|
|
|
|
}
|
|
|
|
|
2024-04-08 20:57:45 +00:00
|
|
|
// Only default ByObject iself if it isn't namespaced or has no namespaces configured, as only
|
|
|
|
// then any of this will be honored.
|
|
|
|
if !isNamespaced || len(byObject.Namespaces) == 0 {
|
|
|
|
defaultedConfig := defaultConfig(byObjectToConfig(byObject), optionDefaultsToConfig(&opts))
|
|
|
|
byObject.Label = defaultedConfig.LabelSelector
|
|
|
|
byObject.Field = defaultedConfig.FieldSelector
|
|
|
|
byObject.Transform = defaultedConfig.Transform
|
|
|
|
byObject.UnsafeDisableDeepCopy = defaultedConfig.UnsafeDisableDeepCopy
|
2024-11-25 20:35:21 +00:00
|
|
|
byObject.EnableWatchBookmarks = defaultedConfig.EnableWatchBookmarks
|
2023-08-28 20:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.ByObject[obj] = byObject
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// Default namespaces after byObject has been defaulted, otherwise a namespace without selectors
|
|
|
|
// will get the `Default` selectors, then get copied to byObject and then not get defaulted from
|
|
|
|
// byObject, as it already has selectors.
|
|
|
|
for namespace, cfg := range opts.DefaultNamespaces {
|
|
|
|
cfg = defaultConfig(cfg, optionDefaultsToConfig(&opts))
|
|
|
|
if namespace == metav1.NamespaceAll {
|
|
|
|
cfg.FieldSelector = fields.AndSelectors(
|
|
|
|
appendIfNotNil(
|
|
|
|
namespaceAllSelector(maps.Keys(opts.DefaultNamespaces)),
|
|
|
|
cfg.FieldSelector,
|
|
|
|
)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
opts.DefaultNamespaces[namespace] = cfg
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Default the resync period to 10 hours if unset
|
2023-06-01 17:01:19 +00:00
|
|
|
if opts.SyncPeriod == nil {
|
|
|
|
opts.SyncPeriod = &defaultSyncPeriod
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
return opts, nil
|
|
|
|
}
|
2021-06-25 05:02:01 +00:00
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
func defaultConfig(toDefault, defaultFrom Config) Config {
|
|
|
|
if toDefault.LabelSelector == nil {
|
|
|
|
toDefault.LabelSelector = defaultFrom.LabelSelector
|
|
|
|
}
|
|
|
|
if toDefault.FieldSelector == nil {
|
|
|
|
toDefault.FieldSelector = defaultFrom.FieldSelector
|
|
|
|
}
|
|
|
|
if toDefault.Transform == nil {
|
|
|
|
toDefault.Transform = defaultFrom.Transform
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
2023-08-28 20:44:55 +00:00
|
|
|
if toDefault.UnsafeDisableDeepCopy == nil {
|
|
|
|
toDefault.UnsafeDisableDeepCopy = defaultFrom.UnsafeDisableDeepCopy
|
|
|
|
}
|
2024-11-25 20:35:21 +00:00
|
|
|
if toDefault.EnableWatchBookmarks == nil {
|
|
|
|
toDefault.EnableWatchBookmarks = defaultFrom.EnableWatchBookmarks
|
|
|
|
}
|
2023-08-28 20:44:55 +00:00
|
|
|
return toDefault
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
2023-10-25 09:53:23 +00:00
|
|
|
|
2024-04-08 20:57:45 +00:00
|
|
|
func namespaceAllSelector(namespaces []string) []fields.Selector {
|
2023-10-25 09:53:23 +00:00
|
|
|
selectors := make([]fields.Selector, 0, len(namespaces)-1)
|
2024-04-08 20:57:45 +00:00
|
|
|
sort.Strings(namespaces)
|
2023-10-25 09:53:23 +00:00
|
|
|
for _, namespace := range namespaces {
|
|
|
|
if namespace != metav1.NamespaceAll {
|
|
|
|
selectors = append(selectors, fields.OneTermNotEqualSelector("metadata.namespace", namespace))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 20:57:45 +00:00
|
|
|
return selectors
|
2023-10-25 09:53:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 20:57:45 +00:00
|
|
|
func appendIfNotNil[T comparable](a []T, b T) []T {
|
2023-10-25 09:53:23 +00:00
|
|
|
if b != *new(T) {
|
2024-04-08 20:57:45 +00:00
|
|
|
return append(a, b)
|
2023-10-25 09:53:23 +00:00
|
|
|
}
|
2024-04-08 20:57:45 +00:00
|
|
|
return a
|
2023-10-25 09:53:23 +00:00
|
|
|
}
|