Dmitriy Matrenichev 8a02ecd4cb
chore: add endpoints balancer controller
This PR adds support for creating a list of API endpoints (each is pair of host and port).

It gets them from
- Machine config cluster endpoint.
- Localhost with LocalAPIServerPort if machine is control panel.
- netip.Addr[0] and port from affiliates if they are control panels.

For #7191

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
2023-06-05 20:47:52 -04:00

64 lines
1.8 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 kubernetes
import (
"context"
"fmt"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
"github.com/siderolabs/talos/pkg/machinery/config/container"
v1alpha1config "github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
)
// patchNodeConfig updates node configuration by means of patch function.
//
//nolint:gocyclo
func patchNodeConfig(ctx context.Context, cluster UpgradeProvider, node string, patchFunc func(config *v1alpha1config.Config) error) error {
c, err := cluster.Client()
if err != nil {
return fmt.Errorf("error building Talos API client: %w", err)
}
ctx = client.WithNode(ctx, node)
mc, err := safe.StateGetByID[*config.MachineConfig](ctx, c.COSI, config.V1Alpha1ID)
if err != nil {
return fmt.Errorf("error fetching config resource: %w", err)
}
cfg := mc.Container().RawV1Alpha1()
if cfg == nil {
return fmt.Errorf("config is not v1alpha1 config")
}
if !cfg.Persist() {
return fmt.Errorf("config persistence is disabled, patching is not supported")
}
if err = patchFunc(cfg); err != nil {
return fmt.Errorf("error patching config: %w", err)
}
cfgBytes, err := container.NewV1Alpha1(cfg).EncodeBytes()
if err != nil {
return fmt.Errorf("error serializing config: %w", err)
}
_, err = c.ApplyConfiguration(ctx, &machine.ApplyConfigurationRequest{
Data: cfgBytes,
Mode: machine.ApplyConfigurationRequest_NO_REBOOT,
})
if err != nil {
return fmt.Errorf("error applying config: %w", err)
}
return nil
}