Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.31] Rework loadbalancer server selection logic #11457

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor filterCN to use a Set instead of map[string]bool
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit 95797c4)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Dec 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit fc3a2447a2dd9bde82b87f3a15c28577af4f27b4
35 changes: 11 additions & 24 deletions pkg/cluster/address_controller.go
Original file line number Diff line number Diff line change
@@ -8,20 +8,17 @@ import (
controllerv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
)

func registerAddressHandlers(ctx context.Context, c *Cluster) {
nodes := c.config.Runtime.Core.Core().V1().Node()
a := &addressesHandler{
nodeController: nodes,
allowed: map[string]bool{},
allowed: sets.New(c.config.SANs...),
}

for _, cn := range c.config.SANs {
a.allowed[cn] = true
}

logrus.Infof("Starting dynamiclistener CN filter node controller")
logrus.Infof("Starting dynamiclistener CN filter node controller with SANs: %v", c.config.SANs)
nodes.OnChange(ctx, "server-cn-filter", a.sync)
c.cnFilterFunc = a.filterCN
}
@@ -30,40 +27,30 @@ type addressesHandler struct {
sync.RWMutex

nodeController controllerv1.NodeController
allowed map[string]bool
allowed sets.Set[string]
}

// filterCN filters a list of potential server CNs (hostnames or IPs), removing any which do not correspond to
// valid cluster servers (control-plane or etcd), or an address explicitly added via the tls-san option.
func (a *addressesHandler) filterCN(cns ...string) []string {
if !a.nodeController.Informer().HasSynced() {
if len(cns) == 0 || !a.nodeController.Informer().HasSynced() {
return cns
}

a.RLock()
defer a.RUnlock()

filteredCNs := make([]string, 0, len(cns))
for _, cn := range cns {
if a.allowed[cn] {
filteredCNs = append(filteredCNs, cn)
} else {
logrus.Debugf("CN filter controller rejecting certificate CN: %s", cn)
}
}
return filteredCNs
return a.allowed.Intersection(sets.New(cns...)).UnsortedList()
}

// sync updates the allowed address list to include addresses for the node
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()
if node != nil && (node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "") {
a.Lock()
defer a.Unlock()

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