Skip to content

Commit

Permalink
add optional options argument to NewClusterCacheTracker function
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 afdcf89
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
66 changes: 52 additions & 14 deletions controllers/remote/cluster_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,62 @@ 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
}

type clusterCacheTrackerOptions struct {
log logr.Logger
clientDisableCacheFor []client.Object
}

type Option func(*clusterCacheTrackerOptions)

func SetLogger(log logr.Logger) Option {
return func(o *clusterCacheTrackerOptions) {
o.log = log
}
}

func SetClientDisableCacheFor(objs []client.Object) Option {
return func(o *clusterCacheTrackerOptions) {
o.clientDisableCacheFor = objs
}
}

func setDefaultOptions(opts *clusterCacheTrackerOptions) {
if opts.log == nil {
opts.log = ctrl.Log.WithName("remote").WithName("ClusterCacheTracker")
}

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 +205,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
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ func setupChecks(mgr ctrl.Manager) {
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,
)
tracker, err := remote.NewClusterCacheTracker(mgr)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
os.Exit(1)
Expand Down

0 comments on commit afdcf89

Please sign in to comment.