Commit 0d23cfe0 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Add RWMutex to address controller

Fixes race condition when address map is updated by multiple goroutines Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent cba9f0d1
...@@ -2,6 +2,7 @@ package cluster ...@@ -2,6 +2,7 @@ package cluster
import ( import (
"context" "context"
"sync"
"github.com/k3s-io/k3s/pkg/util" "github.com/k3s-io/k3s/pkg/util"
controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1" controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
...@@ -26,6 +27,8 @@ func registerAddressHandlers(ctx context.Context, c *Cluster) { ...@@ -26,6 +27,8 @@ func registerAddressHandlers(ctx context.Context, c *Cluster) {
} }
type addressesHandler struct { type addressesHandler struct {
sync.RWMutex
nodeController controllerv1.NodeController nodeController controllerv1.NodeController
allowed map[string]bool allowed map[string]bool
} }
...@@ -37,6 +40,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string { ...@@ -37,6 +40,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
return cns return cns
} }
a.RLock()
defer a.RUnlock()
filteredCNs := make([]string, 0, len(cns)) filteredCNs := make([]string, 0, len(cns))
for _, cn := range cns { for _, cn := range cns {
if a.allowed[cn] { if a.allowed[cn] {
...@@ -52,6 +58,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string { ...@@ -52,6 +58,9 @@ func (a *addressesHandler) filterCN(cns ...string) []string {
func (a *addressesHandler) sync(key string, node *v1.Node) (*v1.Node, error) { func (a *addressesHandler) sync(key string, node *v1.Node) (*v1.Node, error) {
if node != nil { if node != nil {
if node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "" { if node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "" {
a.Lock()
defer a.Unlock()
for _, address := range node.Status.Addresses { for _, address := range node.Status.Addresses {
a.allowed[address.String()] = true a.allowed[address.String()] = true
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment