Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions controllers/remote/cluster_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion controllers/remote/cluster_cache_healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion controllers/remote/cluster_cache_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions controllers/remote/cluster_cache_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
5 changes: 1 addition & 4 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions exp/addons/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down