Skip to content

Commit

Permalink
Use cache for watching remote nodes over informers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Mar 2, 2020
1 parent 308f931 commit 4f4ef09
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
45 changes: 19 additions & 26 deletions controllers/machinehealthcheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/controllers/remote"
Expand All @@ -56,11 +54,11 @@ type MachineHealthCheckReconciler struct {
Client client.Client
Log logr.Logger

controller controller.Controller
recorder record.EventRecorder
scheme *runtime.Scheme
clusterNodeInformers map[client.ObjectKey]cache.Informer
clusterNodeInformersLock sync.RWMutex
controller controller.Controller
recorder record.EventRecorder
scheme *runtime.Scheme
clusterCaches map[client.ObjectKey]cache.Cache
clusterCachesLock sync.RWMutex
}

func (r *MachineHealthCheckReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
Expand Down Expand Up @@ -100,8 +98,7 @@ func (r *MachineHealthCheckReconciler) SetupWithManager(mgr ctrl.Manager, option
r.controller = controller
r.recorder = mgr.GetEventRecorderFor("machinehealthcheck-controller")
r.scheme = mgr.GetScheme()
r.clusterNodeInformers = make(map[client.ObjectKey]cache.Informer)
r.clusterNodeInformersLock = sync.RWMutex{}
r.clusterCaches = make(map[client.ObjectKey]cache.Cache)
return nil
}

Expand Down Expand Up @@ -333,20 +330,20 @@ func (r *MachineHealthCheckReconciler) watchClusterNodes(ctx context.Context, c
return r.createClusterNodeInformer(ctx, c, key)
}

func (r *MachineHealthCheckReconciler) getClusterNodeInformer(key client.ObjectKey) (cache.Informer, bool) {
r.clusterNodeInformersLock.RLock()
defer r.clusterNodeInformersLock.RUnlock()
func (r *MachineHealthCheckReconciler) getClusterNodeInformer(key client.ObjectKey) (cache.Cache, bool) {
r.clusterCachesLock.RLock()
defer r.clusterCachesLock.RUnlock()

informer, ok := r.clusterNodeInformers[key]
return informer, ok
c, ok := r.clusterCaches[key]
return c, ok
}

func (r *MachineHealthCheckReconciler) createClusterNodeInformer(ctx context.Context, c client.Client, key client.ObjectKey) error {
r.clusterNodeInformersLock.Lock()
defer r.clusterNodeInformersLock.Unlock()
r.clusterCachesLock.Lock()
defer r.clusterCachesLock.Unlock()

// Double check the key still doesn't exist under write lock
if _, ok := r.clusterNodeInformers[key]; ok {
if _, ok := r.clusterCaches[key]; ok {
// An informer was created while waiting for the lock
return nil
}
Expand All @@ -356,25 +353,21 @@ func (r *MachineHealthCheckReconciler) createClusterNodeInformer(ctx context.Con
return errors.Wrap(err, "error fetching remote cluster config")
}

k8sClient, err := kubernetes.NewForConfig(config)
clusterCache, err := cache.New(config, cache.Options{})
if err != nil {
return errors.Wrap(err, "error constructing remote cluster client")
return errors.Wrap(err, "error creating cache for remote cluster")
}

// TODO(JoelSpeed): See if we use the resync period from the manager instead of 0
factory := informers.NewSharedInformerFactory(k8sClient, 0)
nodeInformer := factory.Core().V1().Nodes().Informer()
go nodeInformer.Run(ctx.Done())
go clusterCache.Start(ctx.Done())

err = r.controller.Watch(
&source.Informer{Informer: nodeInformer},
source.NewKindWithCache(&corev1.Node{}, clusterCache),
&handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(r.nodeToMachineHealthCheck)},
)
if err != nil {
return errors.Wrap(err, "error watching nodes on target cluster")
}

r.clusterNodeInformers[key] = nodeInformer
r.clusterCaches[key] = clusterCache
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ require (
k8s.io/component-base v0.17.2
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f
sigs.k8s.io/controller-runtime v0.5.0
sigs.k8s.io/controller-runtime v0.5.1-0.20200221182432-e00985b30c68
sigs.k8s.io/yaml v1.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
sigs.k8s.io/controller-runtime v0.5.0 h1:CbqIy5fbUX+4E9bpnBFd204YAzRYlM9SWW77BbrcDQo=
sigs.k8s.io/controller-runtime v0.5.0/go.mod h1:REiJzC7Y00U+2YkMbT8wxgrsX5USpXKGhb2sCtAXiT8=
sigs.k8s.io/controller-runtime v0.5.1-0.20200221182432-e00985b30c68 h1:IuI2yBVvmkkq3gMGoJosMtY5oIC9L8zbgyuXQ+6eUfM=
sigs.k8s.io/controller-runtime v0.5.1-0.20200221182432-e00985b30c68/go.mod h1:REiJzC7Y00U+2YkMbT8wxgrsX5USpXKGhb2sCtAXiT8=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06/go.mod h1:/ULNhyfzRopfcjskuui0cTITekDduZ7ycKN3oUT9R18=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
Expand Down

0 comments on commit 4f4ef09

Please sign in to comment.