-
Notifications
You must be signed in to change notification settings - Fork 2
/
peer_spec.go
209 lines (167 loc) · 5.86 KB
/
peer_spec.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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 kubespan
import (
"context"
"fmt"
"slices"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"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"
"go4.org/netipx"
"github.com/siderolabs/talos/pkg/machinery/resources/cluster"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
"github.com/siderolabs/talos/pkg/machinery/resources/kubespan"
)
// PeerSpecController watches cluster.Affiliates updates PeerSpec.
type PeerSpecController struct{}
// Name implements controller.Controller interface.
func (ctrl *PeerSpecController) Name() string {
return "kubespan.PeerSpecController"
}
// Inputs implements controller.Controller interface.
func (ctrl *PeerSpecController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: config.NamespaceName,
Type: kubespan.ConfigType,
ID: optional.Some(kubespan.ConfigID),
Kind: controller.InputWeak,
},
{
Namespace: cluster.NamespaceName,
Type: cluster.AffiliateType,
Kind: controller.InputWeak,
},
{
Namespace: cluster.NamespaceName,
Type: cluster.IdentityType,
ID: optional.Some(cluster.LocalIdentity),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *PeerSpecController) Outputs() []controller.Output {
return []controller.Output{
{
Type: kubespan.PeerSpecType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *PeerSpecController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
cfg, err := safe.ReaderGetByID[*kubespan.Config](ctx, r, kubespan.ConfigID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting kubespan configuration: %w", err)
}
localIdentity, err := safe.ReaderGetByID[*cluster.Identity](ctx, r, cluster.LocalIdentity)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting first MAC address: %w", err)
}
affiliates, err := safe.ReaderListAll[*cluster.Affiliate](ctx, r)
if err != nil {
return fmt.Errorf("error listing cluster affiliates: %w", err)
}
touchedIDs := map[resource.ID]struct{}{}
if cfg != nil && localIdentity != nil && cfg.TypedSpec().Enabled {
localAffiliateID := localIdentity.TypedSpec().NodeID
peerIPSets := make(map[string]*netipx.IPSet, affiliates.Len())
affiliateLoop:
for it := affiliates.Iterator(); it.Next(); {
affiliate := it.Value()
if affiliate.Metadata().ID() == localAffiliateID {
// skip local affiliate, it's not a peer
continue
}
spec := affiliate.TypedSpec()
if spec.KubeSpan.PublicKey == "" {
// no kubespan information, skip it
continue
}
var builder netipx.IPSetBuilder
for _, ipPrefix := range spec.KubeSpan.AdditionalAddresses {
builder.AddPrefix(ipPrefix)
}
for _, ip := range spec.Addresses {
builder.Add(ip)
}
builder.Add(spec.KubeSpan.Address)
var ipSet *netipx.IPSet
ipSet, err = builder.IPSet()
if err != nil {
logger.Warn("failed building list of IP ranges for the peer", zap.String("ignored_peer", spec.KubeSpan.PublicKey), zap.String("label", spec.Nodename), zap.Error(err))
continue
}
for otherPublicKey, otherIPSet := range peerIPSets {
if otherIPSet.Overlaps(ipSet) {
logger.Warn("peer address overlap", zap.String("this_peer", spec.KubeSpan.PublicKey), zap.String("other_peer", otherPublicKey),
zap.Strings("this_ips", dumpSet(ipSet)), zap.Strings("other_ips", dumpSet(otherIPSet)))
// exclude overlapping IPs from the ipSet
var bldr netipx.IPSetBuilder
// ipSet = ipSet & ~otherIPSet
bldr.AddSet(otherIPSet)
bldr.Complement()
bldr.Intersect(ipSet)
ipSet, err = bldr.IPSet()
if err != nil {
logger.Warn("failed building list of IP ranges for the peer", zap.String("ignored_peer", spec.KubeSpan.PublicKey), zap.String("label", spec.Nodename), zap.Error(err))
continue affiliateLoop
}
if len(ipSet.Ranges()) == 0 {
logger.Warn("conflict resolution removed all ranges", zap.String("this_peer", spec.KubeSpan.PublicKey), zap.String("other_peer", otherPublicKey))
}
}
}
peerIPSets[spec.KubeSpan.PublicKey] = ipSet
if err = safe.WriterModify(ctx, r, kubespan.NewPeerSpec(kubespan.NamespaceName, spec.KubeSpan.PublicKey), func(res *kubespan.PeerSpec) error {
*res.TypedSpec() = kubespan.PeerSpecSpec{
Address: spec.KubeSpan.Address,
AllowedIPs: ipSet.Prefixes(),
Endpoints: slices.Clone(spec.KubeSpan.Endpoints),
Label: spec.Nodename,
}
return nil
}); err != nil {
return err
}
touchedIDs[spec.KubeSpan.PublicKey] = struct{}{}
}
}
// list keys for cleanup
list, err := safe.ReaderListAll[*kubespan.PeerSpec](ctx, r)
if err != nil {
return fmt.Errorf("error listing resources: %w", err)
}
for it := list.Iterator(); it.Next(); {
res := it.Value()
if res.Metadata().Owner() != ctrl.Name() {
continue
}
if _, ok := touchedIDs[res.Metadata().ID()]; !ok {
if err = r.Destroy(ctx, res.Metadata()); err != nil {
return fmt.Errorf("error cleaning up specs: %w", err)
}
}
}
r.ResetRestartBackoff()
}
}
// dumpSet converts IPSet to a form suitable for logging.
func dumpSet(set *netipx.IPSet) []string {
return xslices.Map(set.Ranges(), netipx.IPRange.String)
}