-
Notifications
You must be signed in to change notification settings - Fork 2
/
apid.go
80 lines (66 loc) · 1.98 KB
/
apid.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
// 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 controllers
import (
"context"
"net/netip"
"strings"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"go.uber.org/zap"
"github.com/siderolabs/talemu/internal/pkg/machine/services"
)
// APIDController interacts with SideroLink API and brings up the SideroLink Wireguard interface.
type APIDController struct {
APID *services.APID
address netip.Prefix
}
// Name implements controller.Controller interface.
func (ctrl *APIDController) Name() string {
return "APIDController"
}
// Inputs implements controller.Controller interface.
func (ctrl *APIDController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: network.NamespaceName,
Type: network.AddressStatusType,
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *APIDController) Outputs() []controller.Output {
return nil
}
// Run implements controller.Controller interface.
func (ctrl *APIDController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
addresses, err := safe.ReaderListAll[*network.AddressStatus](ctx, r)
if err != nil {
return err
}
siderolink, found := addresses.Find(func(address *network.AddressStatus) bool {
return strings.HasPrefix(address.TypedSpec().LinkName, constants.SideroLinkName)
})
if !found {
continue
}
address := siderolink.TypedSpec().Address
if ctrl.address == address {
continue
}
if err = ctrl.APID.Run(ctx, address, logger); err != nil {
return err
}
ctrl.address = address
}
}
}