diff --git a/controllers/machinehealthcheck_controller.go b/controllers/machinehealthcheck_controller.go index 299609713f51..b631fa01c3b3 100644 --- a/controllers/machinehealthcheck_controller.go +++ b/controllers/machinehealthcheck_controller.go @@ -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" @@ -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 { @@ -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 } @@ -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 } @@ -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 } diff --git a/go.mod b/go.mod index 84c2b8201eeb..36ae6b686382 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 937a999ed913..ce54af0f44b8 100644 --- a/go.sum +++ b/go.sum @@ -506,8 +506,8 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk= modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k= 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= diff --git a/test/infrastructure/docker/go.mod b/test/infrastructure/docker/go.mod index 503b072ae84a..5f7bb46909e0 100644 --- a/test/infrastructure/docker/go.mod +++ b/test/infrastructure/docker/go.mod @@ -15,7 +15,7 @@ require ( k8s.io/klog v1.0.0 sigs.k8s.io/cluster-api v0.2.6-0.20200213153035-a0cdb3b05cda sigs.k8s.io/cluster-api/test/framework v0.0.0-20200212174651-13d44c484542 - sigs.k8s.io/controller-runtime v0.5.0 + sigs.k8s.io/controller-runtime v0.5.1-0.20200221182432-e00985b30c68 sigs.k8s.io/kind v0.7.0 ) diff --git a/test/infrastructure/docker/go.sum b/test/infrastructure/docker/go.sum index 9268d56ec9be..44db7ab6f64a 100644 --- a/test/infrastructure/docker/go.sum +++ b/test/infrastructure/docker/go.sum @@ -481,6 +481,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/kind v0.7.0 h1:7y7a8EYtGHM+auHmsvzuK5o84SrxPYGidlvfql7j/k4= sigs.k8s.io/kind v0.7.0/go.mod h1:An/AbWHT6pA/Lm0Og8j3ukGhfJP3RiVN/IBU6Lo3zl8= sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=