2023-08-01 00:51:46 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-30 02:34:32 +00:00
|
|
|
"sync"
|
2023-08-01 00:51:46 +00:00
|
|
|
|
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2024-05-06 16:42:27 +00:00
|
|
|
controllerv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
|
2023-08-01 00:51:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
2024-11-17 23:49:57 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2023-08-01 00:51:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func registerAddressHandlers(ctx context.Context, c *Cluster) {
|
|
|
|
nodes := c.config.Runtime.Core.Core().V1().Node()
|
|
|
|
a := &addressesHandler{
|
|
|
|
nodeController: nodes,
|
2024-11-17 23:49:57 +00:00
|
|
|
allowed: sets.New(c.config.SANs...),
|
2023-08-01 00:51:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 23:49:57 +00:00
|
|
|
logrus.Infof("Starting dynamiclistener CN filter node controller with SANs: %v", c.config.SANs)
|
2023-08-01 00:51:46 +00:00
|
|
|
nodes.OnChange(ctx, "server-cn-filter", a.sync)
|
|
|
|
c.cnFilterFunc = a.filterCN
|
|
|
|
}
|
|
|
|
|
|
|
|
type addressesHandler struct {
|
2023-08-30 02:34:32 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2023-08-01 00:51:46 +00:00
|
|
|
nodeController controllerv1.NodeController
|
2024-11-17 23:49:57 +00:00
|
|
|
allowed sets.Set[string]
|
2023-08-01 00:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-11-17 23:49:57 +00:00
|
|
|
if len(cns) == 0 || !a.nodeController.Informer().HasSynced() {
|
2023-08-01 00:51:46 +00:00
|
|
|
return cns
|
|
|
|
}
|
|
|
|
|
2023-08-30 02:34:32 +00:00
|
|
|
a.RLock()
|
|
|
|
defer a.RUnlock()
|
|
|
|
|
2024-11-17 23:49:57 +00:00
|
|
|
return a.allowed.Intersection(sets.New(cns...)).UnsortedList()
|
2023-08-01 00:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sync updates the allowed address list to include addresses for the node
|
|
|
|
func (a *addressesHandler) sync(key string, node *v1.Node) (*v1.Node, error) {
|
2024-11-17 23:49:57 +00:00
|
|
|
if node != nil && (node.Labels[util.ControlPlaneRoleLabelKey] != "" || node.Labels[util.ETCDRoleLabelKey] != "") {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
2023-08-30 02:34:32 +00:00
|
|
|
|
2024-11-17 23:49:57 +00:00
|
|
|
for _, address := range node.Status.Addresses {
|
|
|
|
a.allowed.Insert(address.String())
|
2023-08-01 00:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|