Skip to content

Commit 8d074ec

Browse files
committed
Add RWMutex to address controller
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]>
1 parent f365a9c commit 8d074ec

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/cluster/address_controller.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cluster
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/k3s-io/k3s/pkg/util"
78
controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
@@ -26,6 +27,8 @@ func registerAddressHandlers(ctx context.Context, c *Cluster) {
2627
}
2728

2829
type addressesHandler struct {
30+
sync.RWMutex
31+
2932
nodeController controllerv1.NodeController
3033
allowed map[string]bool
3134
}
@@ -37,6 +40,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
3740
return cns
3841
}
3942

43+
a.RLock()
44+
defer a.RUnlock()
45+
4046
filteredCNs := make([]string, 0, len(cns))
4147
for _, cn := range cns {
4248
if a.allowed[cn] {
@@ -52,6 +58,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
5258
func (a *addressesHandler) sync(key string, node *v1.Node) (*v1.Node, error) {
5359
if node != nil {
5460
if node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "" {
61+
a.Lock()
62+
defer a.Unlock()
63+
5564
for _, address := range node.Status.Addresses {
5665
a.allowed[address.String()] = true
5766
}

0 commit comments

Comments
 (0)