-
Notifications
You must be signed in to change notification settings - Fork 458
/
maintenance_config.go
129 lines (111 loc) · 3.96 KB
/
maintenance_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 runtime
import (
"context"
"fmt"
"net/netip"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
"github.com/siderolabs/talos/pkg/machinery/resources/siderolink"
)
// MaintenanceConfigController manages Maintenance Service config: which address it should listen on, etc.
type MaintenanceConfigController struct{}
// Name implements controller.Controller interface.
func (ctrl *MaintenanceConfigController) Name() string {
return "runtime.MaintenanceConfigController"
}
// Inputs implements controller.Controller interface.
func (ctrl *MaintenanceConfigController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: config.NamespaceName,
Type: siderolink.ConfigType,
ID: optional.Some(siderolink.ConfigID),
},
{
Namespace: network.NamespaceName,
Type: network.NodeAddressType,
ID: optional.Some(network.NodeAddressCurrentID),
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *MaintenanceConfigController) Outputs() []controller.Output {
return []controller.Output{
{
Type: runtime.MaintenanceServiceConfigType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *MaintenanceConfigController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
nodeAddresses, err := safe.ReaderGetByID[*network.NodeAddress](ctx, r, network.NodeAddressCurrentID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting node address: %w", err)
}
var (
listenAddress string
reachableAddresses []netip.Addr
)
if nodeAddresses != nil {
reachableAddresses = nodeAddresses.TypedSpec().IPs()
}
_, err = safe.ReaderGetByID[*siderolink.Config](ctx, r, siderolink.ConfigID)
// check if SideroLink config exists:
switch {
// * if it exists, find the SideroLink address and listen only on it
case err == nil:
if nodeAddresses != nil {
sideroLinkAddresses := xslices.Filter(nodeAddresses.TypedSpec().IPs(), func(addr netip.Addr) bool {
return network.IsULA(addr, network.ULASideroLink)
})
if len(sideroLinkAddresses) > 0 {
listenAddress = nethelpers.JoinHostPort(sideroLinkAddresses[0].String(), constants.ApidPort)
reachableAddresses = sideroLinkAddresses[:1]
}
}
// * if it doesn't exist, listen on '*'
case state.IsNotFoundError(err):
listenAddress = fmt.Sprintf(":%d", constants.ApidPort)
default:
return fmt.Errorf("error getting siderolink config: %w", err)
}
if listenAddress == "" {
// drop config
if err = r.Destroy(ctx, runtime.NewMaintenanceServiceConfig().Metadata()); err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error destroying maintenance config: %w", err)
}
} else {
// create/update config
if err = safe.WriterModify[*runtime.MaintenanceServiceConfig](ctx, r, runtime.NewMaintenanceServiceConfig(),
func(config *runtime.MaintenanceServiceConfig) error {
config.TypedSpec().ListenAddress = listenAddress
config.TypedSpec().ReachableAddresses = reachableAddresses
return nil
}); err != nil {
return fmt.Errorf("error updating maintenance config: %w", err)
}
}
r.ResetRestartBackoff()
}
}