Skip to content

Commit

Permalink
Use sync.Map for clusterNodeInformers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Feb 24, 2020
1 parent 79cf094 commit 987bf92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/crd/bases/cluster.x-k8s.io_machinehealthchecks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ spec:
x-kubernetes-int-or-string: true
nodeStartupTimeout:
description: Machines older than this duration without a node will
be considered to have failed and will be remediated
be considered to have failed and will be remediated.
type: string
selector:
description: Label selector to match machines whose health will be
Expand Down
36 changes: 22 additions & 14 deletions controllers/machinehealthcheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ type MachineHealthCheckReconciler struct {
Client client.Client
Log logr.Logger

controller controller.Controller
recorder record.EventRecorder
scheme *runtime.Scheme
clusterNodeInformers map[types.NamespacedName]cache.Informer
clusterNodeInformersLock *sync.Mutex
controller controller.Controller
recorder record.EventRecorder
scheme *runtime.Scheme
clusterNodeInformers *sync.Map
}

func (r *MachineHealthCheckReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
Expand Down Expand Up @@ -101,8 +100,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[types.NamespacedName]cache.Informer)
r.clusterNodeInformersLock = &sync.Mutex{}
r.clusterNodeInformers = &sync.Map{}
return nil
}

Expand Down Expand Up @@ -345,11 +343,9 @@ func (r *MachineHealthCheckReconciler) getMachineFromNode(nodeName string) (*clu

func (r *MachineHealthCheckReconciler) watchClusterNodes(ctx context.Context, c client.Client, cluster *clusterv1.Cluster) error {
// Ensure that concurrent reconciles don't clash when setting up watches
r.clusterNodeInformersLock.Lock()
defer r.clusterNodeInformersLock.Unlock()

key := util.ObjectKey(cluster)
if _, ok := r.clusterNodeInformers[key]; ok {
if _, ok := r.loadClusterNodeInformer(key); ok {
// watch was already set up for this cluster
return nil
}
Expand Down Expand Up @@ -377,12 +373,24 @@ func (r *MachineHealthCheckReconciler) watchClusterNodes(ctx context.Context, c
return errors.Wrap(err, "error watching nodes on target cluster")
}

if r.clusterNodeInformers == nil {
r.clusterNodeInformers = make(map[types.NamespacedName]cache.Informer)
r.storeClusterNodeInformer(key, nodeInformer)
return nil
}

func (r *MachineHealthCheckReconciler) loadClusterNodeInformer(key client.ObjectKey) (cache.Informer, bool) {
val, ok := r.clusterNodeInformers.Load(key)
if !ok {
return nil, false
}
informer, ok := val.(cache.Informer)
if !ok {
return nil, false
}
return informer, true
}

r.clusterNodeInformers[key] = nodeInformer
return nil
func (r *MachineHealthCheckReconciler) storeClusterNodeInformer(key client.ObjectKey, nodeInformer cache.Informer) {
r.clusterNodeInformers.Store(key, nodeInformer)
}

func (r *MachineHealthCheckReconciler) indexMachineByNodeName(object runtime.Object) []string {
Expand Down

0 comments on commit 987bf92

Please sign in to comment.