Skip to content

Commit

Permalink
Add RWMutex to address controller
Browse files Browse the repository at this point in the history
Fixes race condition when address map is updated by multiple goroutines

Signed-off-by: Brad Davidson <[email protected]>
(cherry picked from commit 0d23cfe)
Signed-off-by: Brad Davidson <[email protected]>
porting by
Signed-off-by: Deshi Xiao <[email protected]>
  • Loading branch information
xiaods committed Sep 2, 2023
1 parent b4c267e commit 91ffd3d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/cluster/address_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"context"
"sync"

controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/sirupsen/logrus"
Expand All @@ -26,6 +27,8 @@ func registerAddressHandlers(ctx context.Context, c *Cluster) {
}

type addressesHandler struct {
sync.RWMutex

nodeController controllerv1.NodeController
allowed map[string]bool
}
Expand All @@ -37,6 +40,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
return cns
}

a.RLock()
defer a.RUnlock()

filteredCNs := make([]string, 0, len(cns))
for _, cn := range cns {
if a.allowed[cn] {
Expand All @@ -52,6 +58,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
func (a *addressesHandler) sync(key string, node *v1.Node) (*v1.Node, error) {
if node != nil {
if node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "" {
a.Lock()
defer a.Unlock()

for _, address := range node.Status.Addresses {
a.allowed[address.String()] = true
}
Expand Down

0 comments on commit 91ffd3d

Please sign in to comment.