mirror of
https://github.com/siderolabs/talos.git
synced 2025-11-02 01:11:11 +01:00
This commit adds support for API load balancer. Quick way to enable it is during cluster creation using new `api-server-balancer-port` flag (0 by default - disabled). When enabled all API request will be routed across cluster control plane endpoints. Closes #7191 Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/netip"
|
|
"reflect"
|
|
"sort"
|
|
|
|
"github.com/cosi-project/runtime/pkg/controller"
|
|
"github.com/cosi-project/runtime/pkg/safe"
|
|
"github.com/siderolabs/gen/channel"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/siderolabs/talos/pkg/machinery/config/machine"
|
|
"github.com/siderolabs/talos/pkg/machinery/resources/cluster"
|
|
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
|
|
)
|
|
|
|
// EndpointController looks up control plane endpoints.
|
|
type EndpointController struct{}
|
|
|
|
// Name implements controller.Controller interface.
|
|
func (ctrl *EndpointController) Name() string {
|
|
return "cluster.EndpointController"
|
|
}
|
|
|
|
// Inputs implements controller.Controller interface.
|
|
func (ctrl *EndpointController) Inputs() []controller.Input {
|
|
return []controller.Input{
|
|
{
|
|
Namespace: cluster.NamespaceName,
|
|
Type: cluster.MemberType,
|
|
Kind: controller.InputWeak,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Outputs implements controller.Controller interface.
|
|
func (ctrl *EndpointController) Outputs() []controller.Output {
|
|
return []controller.Output{
|
|
{
|
|
Type: k8s.EndpointType,
|
|
Kind: controller.OutputShared,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Run implements controller.Controller interface.
|
|
func (ctrl *EndpointController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
|
|
for {
|
|
if _, ok := channel.RecvWithContext(ctx, r.EventCh()); !ok && ctx.Err() != nil {
|
|
return nil //nolint:nilerr
|
|
}
|
|
|
|
memberList, err := safe.ReaderListAll[*cluster.Member](ctx, r)
|
|
if err != nil {
|
|
return fmt.Errorf("error listing members: %w", err)
|
|
}
|
|
|
|
var endpoints []netip.Addr
|
|
|
|
for it := safe.IteratorFromList(memberList); it.Next(); {
|
|
member := it.Value().TypedSpec()
|
|
|
|
if !(member.MachineType == machine.TypeControlPlane || member.MachineType == machine.TypeInit) {
|
|
continue
|
|
}
|
|
|
|
endpoints = append(endpoints, member.Addresses...)
|
|
}
|
|
|
|
sort.Slice(endpoints, func(i, j int) bool { return endpoints[i].Compare(endpoints[j]) < 0 })
|
|
|
|
if err := safe.WriterModify(
|
|
ctx,
|
|
r,
|
|
k8s.NewEndpoint(k8s.ControlPlaneNamespaceName, k8s.ControlPlaneDiscoveredEndpointsID),
|
|
func(r *k8s.Endpoint) error {
|
|
if !reflect.DeepEqual(r.TypedSpec().Addresses, endpoints) {
|
|
logger.Debug("updated controlplane endpoints", zap.Any("endpoints", endpoints))
|
|
}
|
|
|
|
r.TypedSpec().Addresses = endpoints
|
|
|
|
return nil
|
|
},
|
|
); err != nil {
|
|
return fmt.Errorf("error updating endpoints: %w", err)
|
|
}
|
|
|
|
r.ResetRestartBackoff()
|
|
}
|
|
}
|