-
Notifications
You must be signed in to change notification settings - Fork 453
/
discovery_service.go
502 lines (405 loc) · 13.5 KB
/
discovery_service.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// 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"
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"net/netip"
"time"
"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/discovery-api/api/v1alpha1/client/pb"
discoveryclient "github.com/siderolabs/discovery-client/pkg/client"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/proto"
"github.com/siderolabs/talos/pkg/machinery/resources/cluster"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
"github.com/siderolabs/talos/pkg/machinery/resources/kubespan"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
"github.com/siderolabs/talos/pkg/machinery/version"
)
const defaultDiscoveryTTL = 30 * time.Minute
// DiscoveryServiceController pushes Affiliate resource to the Kubernetes registry.
type DiscoveryServiceController struct {
localAffiliateID resource.ID
discoveryConfigVersion resource.Version
}
// Name implements controller.Controller interface.
func (ctrl *DiscoveryServiceController) Name() string {
return "cluster.DiscoveryServiceController"
}
// Inputs implements controller.Controller interface.
func (ctrl *DiscoveryServiceController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: config.NamespaceName,
Type: cluster.ConfigType,
ID: optional.Some(cluster.ConfigID),
Kind: controller.InputWeak,
},
{
Namespace: cluster.NamespaceName,
Type: cluster.IdentityType,
ID: optional.Some(cluster.LocalIdentity),
Kind: controller.InputWeak,
},
{
Namespace: kubespan.NamespaceName,
Type: kubespan.EndpointType,
Kind: controller.InputWeak,
},
{
Namespace: runtime.NamespaceName,
Type: runtime.MachineResetSignalType,
ID: optional.Some(runtime.MachineResetSignalID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *DiscoveryServiceController) Outputs() []controller.Output {
return []controller.Output{
{
Type: cluster.AffiliateType,
Kind: controller.OutputShared,
},
{
Type: network.AddressStatusType,
Kind: controller.OutputShared,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *DiscoveryServiceController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
var (
client *discoveryclient.Client
clientCtxCancel context.CancelFunc
)
clientErrCh := make(chan error, 1)
defer func() {
if clientCtxCancel != nil {
clientCtxCancel()
<-clientErrCh
}
}()
notifyCh := make(chan struct{}, 1)
var (
prevLocalData *pb.Affiliate
prevLocalEndpoints []*pb.Endpoint
prevOtherEndpoints []discoveryclient.Endpoint
)
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
case <-notifyCh:
case err := <-clientErrCh:
if clientCtxCancel != nil {
clientCtxCancel()
}
clientCtxCancel = nil
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("error from discovery client: %w", err)
}
}
cleanupClient := func() {
if clientCtxCancel != nil {
clientCtxCancel()
<-clientErrCh
clientCtxCancel = nil
client = nil
prevLocalData = nil
prevLocalEndpoints = nil
prevOtherEndpoints = nil
}
}
discoveryConfig, err := safe.ReaderGetByID[*cluster.Config](ctx, r, cluster.ConfigID)
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting discovery config: %w", err)
}
continue
}
if !discoveryConfig.TypedSpec().RegistryServiceEnabled {
// if discovery is disabled cleanup existing resources
if err = cleanupAffiliates(ctx, ctrl, r, nil); err != nil {
return err
}
cleanupClient()
continue
}
if !discoveryConfig.Metadata().Version().Equal(ctrl.discoveryConfigVersion) {
// force reconnect on config change
cleanupClient()
}
identity, err := safe.ReaderGetByID[*cluster.Identity](ctx, r, cluster.LocalIdentity)
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting local identity: %w", err)
}
continue
}
localAffiliateID := identity.TypedSpec().NodeID
if ctrl.localAffiliateID != localAffiliateID {
ctrl.localAffiliateID = localAffiliateID
if err = r.UpdateInputs(append(ctrl.Inputs(),
controller.Input{
Namespace: cluster.NamespaceName,
Type: cluster.AffiliateType,
ID: optional.Some(ctrl.localAffiliateID),
Kind: controller.InputWeak,
},
)); err != nil {
return err
}
cleanupClient()
}
affiliate, err := safe.ReaderGetByID[*cluster.Affiliate](ctx, r, ctrl.localAffiliateID)
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting local affiliate: %w", err)
}
continue
}
affiliateSpec := affiliate.TypedSpec()
otherEndpointsList, err := safe.ReaderListAll[*kubespan.Endpoint](ctx, r)
if err != nil {
return fmt.Errorf("error listing endpoints: %w", err)
}
machineResetSginal, err := safe.ReaderGetByID[*runtime.MachineResetSignal](ctx, r, runtime.MachineResetSignalID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting machine reset signal: %w", err)
}
if client == nil {
var cipherBlock cipher.Block
cipherBlock, err = aes.NewCipher(discoveryConfig.TypedSpec().ServiceEncryptionKey)
if err != nil {
return fmt.Errorf("error initializing AES cipher: %w", err)
}
client, err = discoveryclient.NewClient(discoveryclient.Options{
Cipher: cipherBlock,
Endpoint: discoveryConfig.TypedSpec().ServiceEndpoint,
ClusterID: discoveryConfig.TypedSpec().ServiceClusterID,
AffiliateID: localAffiliateID,
TTL: defaultDiscoveryTTL,
Insecure: discoveryConfig.TypedSpec().ServiceEndpointInsecure,
ClientVersion: version.Tag,
})
if err != nil {
return fmt.Errorf("error initializing discovery client: %w", err)
}
var clientCtx context.Context
clientCtx, clientCtxCancel = context.WithCancel(ctx) //nolint:govet
ctrl.discoveryConfigVersion = discoveryConfig.Metadata().Version()
go func() {
clientErrCh <- client.Run(clientCtx, logger, notifyCh)
}()
}
// delete/update local affiliate
//
// if the node enters final resetting stage, cleanup the local affiliate
// otherwise, update local affiliate data
if machineResetSginal != nil {
client.DeleteLocalAffiliate()
} else {
localData := pbAffiliate(affiliateSpec)
localEndpoints := pbEndpoints(affiliateSpec)
otherEndpoints := pbOtherEndpoints(otherEndpointsList)
// don't send updates on localData if it hasn't changed: this introduces positive feedback loop,
// as the watch loop will notify on self update
if !proto.Equal(localData, prevLocalData) || !equalEndpoints(localEndpoints, prevLocalEndpoints) || !equalOtherEndpoints(otherEndpoints, prevOtherEndpoints) {
if err = client.SetLocalData(&discoveryclient.Affiliate{
Affiliate: localData,
Endpoints: localEndpoints,
}, otherEndpoints); err != nil {
return fmt.Errorf("error setting local affiliate data: %w", err)
}
prevLocalData = localData
prevLocalEndpoints = localEndpoints
prevOtherEndpoints = otherEndpoints
}
}
// discover public IP
if publicIP := client.GetPublicIP(); len(publicIP) > 0 {
if err = safe.WriterModify(ctx, r, network.NewAddressStatus(cluster.NamespaceName, "service"), func(address *network.AddressStatus) error {
var addr netip.Addr
if err = addr.UnmarshalBinary(publicIP); err != nil {
return fmt.Errorf("error unmarshaling public IP: %w", err)
}
address.TypedSpec().Address = netip.PrefixFrom(addr, addr.BitLen())
return nil
}); err != nil {
return err //nolint:govet
}
}
// discover other nodes (affiliates)
touchedIDs := make(map[resource.ID]struct{})
for _, discoveredAffiliate := range client.GetAffiliates() {
id := fmt.Sprintf("service/%s", discoveredAffiliate.Affiliate.NodeId)
if err = safe.WriterModify(ctx, r, cluster.NewAffiliate(cluster.RawNamespaceName, id), func(res *cluster.Affiliate) error {
*res.TypedSpec() = specAffiliate(discoveredAffiliate.Affiliate, discoveredAffiliate.Endpoints)
return nil
}); err != nil {
return err
}
touchedIDs[id] = struct{}{}
}
if err := cleanupAffiliates(ctx, ctrl, r, touchedIDs); err != nil {
return err
}
r.ResetRestartBackoff()
}
}
func pbAffiliate(affiliate *cluster.AffiliateSpec) *pb.Affiliate {
addresses := xslices.Map(affiliate.Addresses, func(address netip.Addr) []byte {
return takeResult(address.MarshalBinary())
})
var kubeSpan *pb.KubeSpan
if affiliate.KubeSpan.PublicKey != "" {
kubeSpan = &pb.KubeSpan{
PublicKey: affiliate.KubeSpan.PublicKey,
Address: takeResult(affiliate.KubeSpan.Address.MarshalBinary()),
AdditionalAddresses: xslices.Map(affiliate.KubeSpan.AdditionalAddresses, func(address netip.Prefix) *pb.IPPrefix {
return &pb.IPPrefix{
Bits: uint32(address.Bits()),
Ip: takeResult(address.Addr().MarshalBinary()),
}
}),
}
}
return &pb.Affiliate{
NodeId: affiliate.NodeID,
Addresses: addresses,
Hostname: affiliate.Hostname,
Nodename: affiliate.Nodename,
MachineType: affiliate.MachineType.String(),
OperatingSystem: affiliate.OperatingSystem,
Kubespan: kubeSpan,
ControlPlane: toPlane(affiliate.ControlPlane),
}
}
func toPlane(data *cluster.ControlPlane) *pb.ControlPlane {
if data == nil {
return nil
}
return &pb.ControlPlane{ApiServerPort: uint32(data.APIServerPort)}
}
func pbEndpoints(affiliate *cluster.AffiliateSpec) []*pb.Endpoint {
if affiliate.KubeSpan.PublicKey == "" || len(affiliate.KubeSpan.Endpoints) == 0 {
return nil
}
return xslices.Map(affiliate.KubeSpan.Endpoints, func(endpoint netip.AddrPort) *pb.Endpoint {
return &pb.Endpoint{
Port: uint32(endpoint.Port()),
Ip: takeResult(endpoint.Addr().MarshalBinary()),
}
})
}
func pbOtherEndpoints(otherEndpointsList safe.List[*kubespan.Endpoint]) []discoveryclient.Endpoint {
if otherEndpointsList.Len() == 0 {
return nil
}
result := make([]discoveryclient.Endpoint, 0, otherEndpointsList.Len())
for it := otherEndpointsList.Iterator(); it.Next(); {
endpoint := it.Value().TypedSpec()
result = append(result, discoveryclient.Endpoint{
AffiliateID: endpoint.AffiliateID,
Endpoints: []*pb.Endpoint{
{
Port: uint32(endpoint.Endpoint.Port()),
Ip: takeResult(endpoint.Endpoint.Addr().MarshalBinary()),
},
},
})
}
return result
}
func equalEndpoints(a, b []*pb.Endpoint) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
if len(a) != len(b) {
return false
}
for i := range a {
if !proto.Equal(a[i], b[i]) {
return false
}
}
return true
}
func equalOtherEndpoints(a, b []discoveryclient.Endpoint) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].AffiliateID != b[i].AffiliateID {
return false
}
if !equalEndpoints(a[i].Endpoints, b[i].Endpoints) {
return false
}
}
return true
}
func specAffiliate(affiliate *pb.Affiliate, endpoints []*pb.Endpoint) cluster.AffiliateSpec {
result := cluster.AffiliateSpec{
NodeID: affiliate.NodeId,
Hostname: affiliate.Hostname,
Nodename: affiliate.Nodename,
OperatingSystem: affiliate.OperatingSystem,
MachineType: takeResult(machine.ParseType(affiliate.MachineType)), // ignore parse error (machine.TypeUnknown)
ControlPlane: fromControlPlane(affiliate.ControlPlane),
}
result.Addresses = make([]netip.Addr, 0, len(affiliate.Addresses))
for i := range affiliate.Addresses {
var ip netip.Addr
if err := ip.UnmarshalBinary(affiliate.Addresses[i]); err == nil {
result.Addresses = append(result.Addresses, ip)
}
}
if affiliate.Kubespan != nil {
result.KubeSpan.PublicKey = affiliate.Kubespan.PublicKey
result.KubeSpan.Address.UnmarshalBinary(affiliate.Kubespan.Address) //nolint:errcheck // ignore error, address will be zero
result.KubeSpan.AdditionalAddresses = make([]netip.Prefix, 0, len(affiliate.Kubespan.AdditionalAddresses))
for i := range affiliate.Kubespan.AdditionalAddresses {
var ip netip.Addr
if err := ip.UnmarshalBinary(affiliate.Kubespan.AdditionalAddresses[i].Ip); err == nil {
result.KubeSpan.AdditionalAddresses = append(result.KubeSpan.AdditionalAddresses, netip.PrefixFrom(ip, int(affiliate.Kubespan.AdditionalAddresses[i].Bits)))
}
}
result.KubeSpan.Endpoints = make([]netip.AddrPort, 0, len(endpoints))
for i := range endpoints {
var ip netip.Addr
if err := ip.UnmarshalBinary(endpoints[i].Ip); err == nil {
result.KubeSpan.Endpoints = append(result.KubeSpan.Endpoints, netip.AddrPortFrom(ip, uint16(endpoints[i].Port)))
}
}
}
return result
}
func fromControlPlane(plane *pb.ControlPlane) *cluster.ControlPlane {
if plane == nil {
return nil
}
return &cluster.ControlPlane{APIServerPort: int(plane.ApiServerPort)}
}
func takeResult[T any](arg1 T, _ error) T {
return arg1
}