mirror of
https://git.vectorsigma.ru/public/k3s.git
synced 2026-08-04 07:59:28 +00:00
Add RWMutex to address controller
Fixes race condition when address map is updated by multiple goroutines
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit 0d23cfe038)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
committed by
Brad Davidson
parent
f365a9cb98
commit
8d074ecb5a
@@ -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"
|
||||
@@ -26,6 +27,8 @@ func registerAddressHandlers(ctx context.Context, c *Cluster) {
|
||||
}
|
||||
|
||||
type addressesHandler struct {
|
||||
sync.RWMutex
|
||||
|
||||
nodeController controllerv1.NodeController
|
||||
allowed map[string]bool
|
||||
}
|
||||
@@ -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] {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user