Skip to content

Commit f333539

Browse files
committed
Add synchronization for node reconciliation to prevent race conditions
1 parent 24ca89e commit f333539

File tree

1 file changed

+10
-2
lines changed
  • components/node-labeler/cmd

1 file changed

+10
-2
lines changed

components/node-labeler/cmd/run.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"strconv"
1414
"strings"
15+
"sync"
1516
"time"
1617

1718
"github.com/bombsimon/logrusr/v2"
@@ -287,8 +288,9 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
287288

288289
type NodeScaledownAnnotationController struct {
289290
client.Client
290-
nodesToReconcile chan string
291-
stopChan chan struct{}
291+
nodesToReconcile chan string
292+
stopChan chan struct{}
293+
nodeReconcileLock sync.Map
292294
}
293295

294296
func NewNodeScaledownAnnotationController(client client.Client) (*NodeScaledownAnnotationController, error) {
@@ -427,6 +429,12 @@ func (wc *NodeScaledownAnnotationController) reconcileAllNodes(ctx context.Conte
427429

428430
// reconcileNode counts the workspaces running on a node and updates the autoscaler annotation accordingly
429431
func (c *NodeScaledownAnnotationController) reconcileNode(ctx context.Context, nodeName string) error {
432+
mutexInterface, _ := c.nodeReconcileLock.LoadOrStore(nodeName, &sync.Mutex{})
433+
mutex := mutexInterface.(*sync.Mutex)
434+
435+
mutex.Lock()
436+
defer mutex.Unlock()
437+
430438
timer := prometheus.NewTimer(NodeScaledownAnnotationReconcileDuration.WithLabelValues("node"))
431439
defer timer.ObserveDuration()
432440

0 commit comments

Comments
 (0)