Skip to content

Commit

Permalink
accept options in remote.NewClusterCacheTracker
Browse files Browse the repository at this point in the history
add helper functions to set the options used by the tracker

add SetLogger to provide a logger to the tracker
add SetClientDisableCacheFor to disable caching of certain objects
by the client
  • Loading branch information
Yuvaraj Kakaraparthi committed May 27, 2021
1 parent 0e1629b commit 49cce11
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 25 deletions.
73 changes: 59 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,68 @@ const (

// ClusterCacheTracker manages client caches for workload clusters.
type ClusterCacheTracker struct {
log logr.Logger
client client.Client
scheme *runtime.Scheme
log logr.Logger
clientDisableCacheFor []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 logr.Logger
ClientDisableCacheFor []client.Object
}

// Option is a configuration option supplied to NewClusterCacheTracker.
type Option func(*ClusterCacheTrackerOptions)

// SetLogger allows to override the default logger used by ClusterCacheTracker.
func SetLogger(log logr.Logger) Option {
return func(o *ClusterCacheTrackerOptions) {
o.Log = log
}
}

// SetClientDisableCacheFor allows to set the objects that should not be
// cached by the client.
func SetClientDisableCacheFor(objs []client.Object) Option {
return func(o *ClusterCacheTrackerOptions) {
o.ClientDisableCacheFor = objs
}
}

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 ...Option) (*ClusterCacheTracker, error) {
ccto := &ClusterCacheTrackerOptions{}
for _, o := range options {
o(ccto)
}

setDefaultOptions(ccto)

return &ClusterCacheTracker{
log: log,
client: manager.GetClient(),
scheme: manager.GetScheme(),
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
log: ccto.Log,
clientDisableCacheFor: ccto.ClientDisableCacheFor,
client: manager.GetClient(),
scheme: manager.GetScheme(),
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
}, nil
}

Expand Down Expand Up @@ -164,12 +212,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.clientDisableCacheFor,
})
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, SetLogger(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, SetLogger(log.NullLogger{}))
g.Expect(err).NotTo(HaveOccurred())

t.Log("Creating the ClusterCacheReconciler")
Expand Down
2 changes: 1 addition & 1 deletion controllers/remote/cluster_cache_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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, SetLogger(log.NullLogger{}))
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,
testEnv.Manager,
remote.SetLogger(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,
testEnv.Manager,
remote.SetLogger(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)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion exp/addons/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
fmt.Println("Creating new test environment")
testEnv = helpers.NewTestEnvironment()

trckr, err := remote.NewClusterCacheTracker(log.NullLogger{}, testEnv.Manager)
trckr, err := remote.NewClusterCacheTracker(testEnv.Manager, remote.SetLogger(log.NullLogger{}))
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.SetLogger(ctrl.Log.WithName("remote").WithName("ClusterCacheTracker")),
)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
Expand Down

0 comments on commit 49cce11

Please sign in to comment.