diff --git a/controllers/remote/cluster_cache.go b/controllers/remote/cluster_cache.go index 4c1f6ef3f9f7..82fe36a86998 100644 --- a/controllers/remote/cluster_cache.go +++ b/controllers/remote/cluster_cache.go @@ -38,6 +38,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/source" ) @@ -51,21 +52,51 @@ const ( // ClusterCacheTracker manages client caches for workload clusters. type ClusterCacheTracker struct { - log logr.Logger - client client.Client - scheme *runtime.Scheme + log logr.Logger + clientUncachedObjects []client.Object + client client.Client + scheme *runtime.Scheme lock sync.RWMutex clusterAccessors map[client.ObjectKey]*clusterAccessor } +// ClusterCacheTrackerOptions defines options to configure +// a ClusterCacheTracker. +type ClusterCacheTrackerOptions struct { + // Log is the logger used throughout the lifecycle of caches. + // Defaults to a no-op logger if it's not set. + Log logr.Logger + + // ClientDisableCacheFor instructs the Client to never cache the following objects, + // it'll instead query the API server directly. + // Defaults to never caching ConfigMap and Secret if not set. + ClientDisableCacheFor []client.Object +} + +func setDefaultOptions(opts *ClusterCacheTrackerOptions) { + if opts.Log == nil { + opts.Log = log.NullLogger{} + } + + if len(opts.ClientDisableCacheFor) == 0 { + opts.ClientDisableCacheFor = []client.Object{ + &corev1.ConfigMap{}, + &corev1.Secret{}, + } + } +} + // NewClusterCacheTracker creates a new ClusterCacheTracker. -func NewClusterCacheTracker(log logr.Logger, manager ctrl.Manager) (*ClusterCacheTracker, error) { +func NewClusterCacheTracker(manager ctrl.Manager, options ClusterCacheTrackerOptions) (*ClusterCacheTracker, error) { + setDefaultOptions(&options) + return &ClusterCacheTracker{ - log: log, - client: manager.GetClient(), - scheme: manager.GetScheme(), - clusterAccessors: make(map[client.ObjectKey]*clusterAccessor), + log: options.Log, + clientUncachedObjects: options.ClientDisableCacheFor, + client: manager.GetClient(), + scheme: manager.GetScheme(), + clusterAccessors: make(map[client.ObjectKey]*clusterAccessor), }, nil } @@ -164,12 +195,9 @@ func (t *ClusterCacheTracker) newClusterAccessor(ctx context.Context, cluster cl }) delegatingClient, err := client.NewDelegatingClient(client.NewDelegatingClientInput{ - CacheReader: cache, - Client: c, - UncachedObjects: []client.Object{ - &corev1.ConfigMap{}, - &corev1.Secret{}, - }, + CacheReader: cache, + Client: c, + UncachedObjects: t.clientUncachedObjects, }) if err != nil { return nil, err diff --git a/controllers/remote/cluster_cache_healthcheck_test.go b/controllers/remote/cluster_cache_healthcheck_test.go index d4189b5b549a..9bae93451555 100644 --- a/controllers/remote/cluster_cache_healthcheck_test.go +++ b/controllers/remote/cluster_cache_healthcheck_test.go @@ -71,7 +71,7 @@ func TestClusterCacheHealthCheck(t *testing.T) { k8sClient = mgr.GetClient() t.Log("Setting up a ClusterCacheTracker") - cct, err = NewClusterCacheTracker(klogr.New(), mgr) + cct, err = NewClusterCacheTracker(mgr, ClusterCacheTrackerOptions{Log: klogr.New()}) g.Expect(err).NotTo(HaveOccurred()) t.Log("Creating a namespace for the test") diff --git a/controllers/remote/cluster_cache_reconciler_test.go b/controllers/remote/cluster_cache_reconciler_test.go index 9a9f4d696981..055e945997b9 100644 --- a/controllers/remote/cluster_cache_reconciler_test.go +++ b/controllers/remote/cluster_cache_reconciler_test.go @@ -85,7 +85,7 @@ func TestClusterCacheReconciler(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) t.Log("Setting up a ClusterCacheTracker") - cct, err = NewClusterCacheTracker(log.NullLogger{}, mgr) + cct, err = NewClusterCacheTracker(mgr, ClusterCacheTrackerOptions{}) g.Expect(err).NotTo(HaveOccurred()) t.Log("Creating the ClusterCacheReconciler") diff --git a/controllers/remote/cluster_cache_tracker_test.go b/controllers/remote/cluster_cache_tracker_test.go index 44c6b1ca675e..aa343a25fd81 100644 --- a/controllers/remote/cluster_cache_tracker_test.go +++ b/controllers/remote/cluster_cache_tracker_test.go @@ -31,7 +31,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) @@ -86,7 +85,7 @@ func TestClusterCacheTracker(t *testing.T) { k8sClient = mgr.GetClient() t.Log("Setting up a ClusterCacheTracker") - cct, err = NewClusterCacheTracker(log.NullLogger{}, mgr) + cct, err = NewClusterCacheTracker(mgr, ClusterCacheTrackerOptions{}) g.Expect(err).NotTo(HaveOccurred()) t.Log("Creating a namespace for the test") diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 19d886fc23c6..54a15d4b03ee 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -52,8 +52,8 @@ func TestMain(m *testing.M) { // Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers // requiring a connection to a remote cluster tracker, err := remote.NewClusterCacheTracker( - log.Log, env.Manager, + remote.ClusterCacheTrackerOptions{Log: log.Log}, ) if err != nil { panic(fmt.Sprintf("unable to create cluster cache tracker: %v", err)) diff --git a/controlplane/kubeadm/internal/cluster_test.go b/controlplane/kubeadm/internal/cluster_test.go index 71168b196305..b6a0e9d37987 100644 --- a/controlplane/kubeadm/internal/cluster_test.go +++ b/controlplane/kubeadm/internal/cluster_test.go @@ -107,8 +107,8 @@ func TestGetWorkloadCluster(t *testing.T) { badCrtEtcdSecret := etcdSecret.DeepCopy() badCrtEtcdSecret.Data[secret.TLSCrtDataName] = []byte("bad cert") tracker, err := remote.NewClusterCacheTracker( - log.Log, env.Manager, + remote.ClusterCacheTrackerOptions{Log: log.Log}, ) g.Expect(err).ToNot(HaveOccurred()) diff --git a/controlplane/kubeadm/main.go b/controlplane/kubeadm/main.go index 18d5b16d0a22..514dff3cf117 100644 --- a/controlplane/kubeadm/main.go +++ b/controlplane/kubeadm/main.go @@ -185,10 +185,7 @@ func setupChecks(mgr ctrl.Manager) { func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { // Set up a ClusterCacheTracker to provide to controllers // requiring a connection to a remote cluster - tracker, err := remote.NewClusterCacheTracker( - ctrl.Log.WithName("remote").WithName("ClusterCacheTracker"), - mgr, - ) + tracker, err := remote.NewClusterCacheTracker(mgr, remote.ClusterCacheTrackerOptions{}) if err != nil { setupLog.Error(err, "unable to create cluster cache tracker") os.Exit(1) diff --git a/exp/addons/controllers/suite_test.go b/exp/addons/controllers/suite_test.go index 75545fb2d333..8af4d69685ac 100644 --- a/exp/addons/controllers/suite_test.go +++ b/exp/addons/controllers/suite_test.go @@ -25,7 +25,6 @@ import ( "sigs.k8s.io/cluster-api/internal/envtest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/log" // +kubebuilder:scaffold:imports ) @@ -38,7 +37,7 @@ func TestMain(m *testing.M) { fmt.Println("Creating new test environment") env = envtest.New() - trckr, err := remote.NewClusterCacheTracker(log.NullLogger{}, env.Manager) + trckr, err := remote.NewClusterCacheTracker(env.Manager, remote.ClusterCacheTrackerOptions{}) if err != nil { panic(fmt.Sprintf("Failed to create new cluster cache tracker: %v", err)) } diff --git a/main.go b/main.go index 1ed12e411692..df83149cf4c5 100644 --- a/main.go +++ b/main.go @@ -225,8 +225,8 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { // Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers // requiring a connection to a remote cluster tracker, err := remote.NewClusterCacheTracker( - ctrl.Log.WithName("remote").WithName("ClusterCacheTracker"), mgr, + remote.ClusterCacheTrackerOptions{Log: ctrl.Log.WithName("remote").WithName("ClusterCacheTracker")}, ) if err != nil { setupLog.Error(err, "unable to create cluster cache tracker")