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]>
  • Loading branch information
brandond committed Aug 30, 2023
1 parent f365a9c commit 8d074ec
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"

"github.com/k3s-io/k3s/pkg/util"
controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
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 8d074ec

Please sign in to comment.