-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
662 lines (587 loc) · 25.3 KB
/
main.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Copyright (c) 2022-2024 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build linux
// +build linux
package main
import (
"context"
"crypto/tls"
"net"
"net/url"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"google.golang.org/protobuf/proto"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext/ipcontext/ipaddress"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext/ipcontext/routes"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext/ipcontext/unnumbered"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext/mtu"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/loopback"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/memif"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/up"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/vrf"
"github.com/networkservicemesh/sdk/pkg/ipam/strictvl3ipam"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clientinfo"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/heal"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/null"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/onidle"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/retry"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/upstreamrefresh"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/mtu/vl3mtu"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
registryclientinfo "github.com/networkservicemesh/sdk/pkg/registry/common/clientinfo"
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
"github.com/networkservicemesh/sdk/pkg/tools/opentelemetry"
"github.com/networkservicemesh/sdk/pkg/tools/token"
"github.com/networkservicemesh/sdk/pkg/tools/tracing"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/genericsync"
"github.com/edwarnicke/grpcfd"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/networkservicemesh/vpphelper"
"github.com/networkservicemesh/api/pkg/api/ipam"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/payload"
registryapi "github.com/networkservicemesh/api/pkg/api/registry"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/tag"
kernelsdk "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/tools/debug"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
"github.com/networkservicemesh/sdk/pkg/tools/pprofutils"
"github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
)
// Config holds configuration parameters from environment variables
type Config struct {
Name string `default:"vL3-server" desc:"Name of vL3 Server"`
DialTimeout time.Duration `default:"5s" desc:"timeout to dial NSMgr" split_words:"true"`
RequestTimeout time.Duration `default:"15s" desc:"timeout to request NSE" split_words:"true"`
ListenOn string `default:"listen.on.sock" desc:"listen on socket" split_words:"true"`
ConnectTo url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to connect to" split_words:"true"`
MaxTokenLifetime time.Duration `default:"10m" desc:"maximum lifetime of tokens" split_words:"true"`
RegistryClientPolicies []string `default:"etc/nsm/opa/common/.*.rego,etc/nsm/opa/registry/.*.rego,etc/nsm/opa/client/.*.rego" desc:"paths to files and directories that contain registry client policies" split_words:"true"`
ServiceNames []string `default:"vL3" desc:"Name of providing service" split_words:"true"`
Labels map[string]string `default:"" desc:"Endpoint labels"`
RequestLabels map[string]string `default:"" desc:"Adds labels to request on requesting vl3 nse" split_words:"true"`
IdleTimeout time.Duration `default:"0" desc:"timeout for automatic shutdown when there were no requests for specified time. Set 0 to disable auto-shutdown." split_words:"true"`
RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"`
UnregisterItself bool `default:"true" desc:"if true then NSE unregister itself when it completes working" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" split_words:"true"`
PrefixServerURL []url.URL `default:"vl3-ipam:5006" desc:"URL to VL3 IPAM server(s)" split_words:"true"`
DNSTemplates []string `default:"{{ index .Labels \"podName\" }}.{{ .NetworkService }}." desc:"Represents domain naming templates in go-template format. It is using for generating the domain name for each nse/nsc in the vl3 network" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofListenOn string `default:"localhost:6060" desc:"pprof URL to ListenAndServe" split_words:"true"`
dnsServerAddr net.IP
dnsServerAddrCh chan net.IP
dnsConfigs genericsync.Map[string, []*networkservice.DNSConfig]
}
// Process prints and processes env to config
func (c *Config) Process() error {
if err := envconfig.Usage("nsm", c); err != nil {
return errors.Wrap(err, "cannot show usage of envconfig nse")
}
if err := envconfig.Process("nsm", c); err != nil {
return errors.Wrap(err, "cannot process envconfig nse")
}
return nil
}
func startListenPrefixes(ctx context.Context, c *Config, tlsClientConfig *tls.Config, subscriptions []chan *ipam.PrefixResponse) {
for i := range c.PrefixServerURL {
prefixServerURL := &c.PrefixServerURL[i]
go func(i int, prefixServerURL *url.URL) {
log.FromContext(ctx).Infof("Start listening prefix server %d: %s", i, prefixServerURL)
var previousResponse *ipam.PrefixResponse
var cc *grpc.ClientConn
var err error
for ctx.Err() == nil {
// Close the previous clientConn
if cc != nil {
_ = cc.Close()
}
dialCtx, dialCtxCancel := context.WithTimeout(ctx, time.Millisecond*200)
cc, err = grpc.DialContext(dialCtx,
grpcutils.URLToTarget(prefixServerURL),
grpc.WithBlock(),
grpc.WithTransportCredentials(
credentials.NewTLS(
tlsClientConfig,
),
),
)
// It is safe to cancel dial ctx after DialContext if WithBlock() option is used
dialCtxCancel()
if err != nil {
logrus.Error(err.Error())
continue
}
managePrefixClient, err := ipam.NewIPAMClient(cc).ManagePrefixes(ctx)
if err != nil {
logrus.Error(err.Error())
continue
}
request := &ipam.PrefixRequest{
Type: ipam.Type_ALLOCATE,
Prefix: previousResponse.GetPrefix(),
}
err = managePrefixClient.Send(request)
if err != nil {
continue
}
for resp, recvErr := managePrefixClient.Recv(); recvErr == nil; resp, recvErr = managePrefixClient.Recv() {
if !proto.Equal(previousResponse, resp) {
previousResponse = resp
subscriptions[i] <- resp
}
}
}
}(i, prefixServerURL)
}
}
func main() {
// ********************************************************************************
// setup context to catch signals
// ********************************************************************************
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// ********************************************************************************
// setup logging
// ********************************************************************************
ctx = log.WithLog(ctx, logruslogger.New(ctx, map[string]interface{}{"cmd": os.Args[0]}))
if err := debug.Self(); err != nil {
log.FromContext(ctx).Infof("%s", err)
}
// ********************************************************************************
// Configure open tracing
// ********************************************************************************
log.EnableTracing(true)
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 1: get config from environment")
// ********************************************************************************
config := new(Config)
if err := config.Process(); err != nil {
logrus.Fatal(err.Error())
}
level, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
logrus.Fatalf("invalid log level %s", config.LogLevel)
}
logrus.SetLevel(level)
logrus.SetFormatter(&nested.Formatter{})
logruslogger.SetupLevelChangeOnSignal(ctx, map[os.Signal]logrus.Level{
syscall.SIGUSR1: logrus.TraceLevel,
syscall.SIGUSR2: level,
})
config.dnsServerAddrCh = make(chan net.IP, 1)
// ********************************************************************************
// Configure Open Telemetry
// ********************************************************************************
if opentelemetry.IsEnabled() {
collectorAddress := config.OpenTelemetryEndpoint
spanExporter := opentelemetry.InitSpanExporter(ctx, collectorAddress)
metricExporter := opentelemetry.InitOPTLMetricExporter(ctx, collectorAddress, config.MetricsExportInterval)
o := opentelemetry.Init(ctx, spanExporter, metricExporter, config.Name)
defer func() {
if err = o.Close(); err != nil {
logrus.Error(err.Error())
}
}()
}
// ********************************************************************************
// Configure pprof
// ********************************************************************************
if config.PprofEnabled {
go pprofutils.ListenAndServe(ctx, config.PprofListenOn)
}
log.FromContext(ctx).Infof("Config: %#v", config)
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 2: run vpp and get a connection to it")
// ********************************************************************************
vppConn, vppErrCh := vpphelper.StartAndDialContext(ctx)
exitOnErr(ctx, cancel, vppErrCh)
defer func() {
cancel()
<-vppErrCh
}()
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 3: retrieving svid, check spire agent logs if this is the last line you see")
// ********************************************************************************
source, err := workloadapi.NewX509Source(ctx)
if err != nil {
logrus.Fatalf("error getting x509 source: %+v", err)
}
svid, err := source.GetX509SVID()
if err != nil {
logrus.Fatalf("error getting x509 svid: %+v", err)
}
log.FromContext(ctx).Infof("SVID: %q", svid.ID)
tlsClientConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeAny())
tlsClientConfig.MinVersion = tls.VersionTLS12
tlsServerConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeAny())
tlsServerConfig.MinVersion = tls.VersionTLS12
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 4: prepare shared between client and server resources")
// ********************************************************************************
vrfOptions := []vrf.Option{
vrf.WithSharedMap(vrf.NewMap()),
vrf.WithLoadInterface(loopback.Load),
}
loopOptions := []loopback.Option{
loopback.WithSharedMap(loopback.NewMap()),
}
signalCtx, cancelSignalCtx := notifyContext(ctx)
defer cancelSignalCtx()
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 5: get all vl3-nses from registry")
// ********************************************************************************
clientOptions := append(tracing.WithTracingDial(),
grpcfd.WithChainStreamInterceptor(),
grpcfd.WithChainUnaryInterceptor(),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
grpc.PerRPCCredentials(token.NewPerRPCCredentials(spiffejwt.TokenGeneratorFunc(source, config.MaxTokenLifetime))),
),
grpc.WithTransportCredentials(
grpcfd.TransportCredentials(
credentials.NewTLS(tlsClientConfig),
),
),
)
clientAdditionalFunctionality := []networkservice.NetworkServiceClient{
upstreamrefresh.NewClient(ctx, upstreamrefresh.WithLocalNotifications()),
vl3mtu.NewClient(),
}
nsmClient := retry.NewClient(
client.NewClient(ctx,
client.WithClientURL(&config.ConnectTo),
client.WithName(config.Name),
client.WithAuthorizeClient(authorize.NewClient()),
client.WithHealClient(heal.NewClient(ctx)),
client.WithAdditionalFunctionality(
append(
clientAdditionalFunctionality,
clientinfo.NewClient(),
kernelsdk.NewClient(),
sendfd.NewClient(),
)...,
),
client.WithDialTimeout(config.DialTimeout),
client.WithDialOptions(clientOptions...),
),
)
nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(
ctx,
registryclient.WithClientURL(&config.ConnectTo),
registryclient.WithDialOptions(clientOptions...),
registryclient.WithNSEAdditionalFunctionality(
registryclientinfo.NewNetworkServiceEndpointRegistryClient(),
registrysendfd.NewNetworkServiceEndpointRegistryClient(),
),
registryclient.WithAuthorizeNSERegistryClient(registryauthorize.NewNetworkServiceEndpointRegistryClient(
registryauthorize.WithPolicies(config.RegistryClientPolicies...),
)),
)
if config.RegisterService {
for _, serviceName := range config.ServiceNames {
nsRegistryClient := registryclient.NewNetworkServiceRegistryClient(ctx,
registryclient.WithClientURL(&config.ConnectTo),
registryclient.WithDialOptions(clientOptions...),
registryclient.WithAuthorizeNSRegistryClient(registryauthorize.NewNetworkServiceRegistryClient(
registryauthorize.WithPolicies(config.RegistryClientPolicies...),
)))
_, err = nsRegistryClient.Register(ctx, ®istryapi.NetworkService{
Name: serviceName,
Payload: payload.IP,
})
if err != nil {
log.FromContext(ctx).Fatalf("unable to register ns %+v", err)
}
}
}
tmpDir, err := os.MkdirTemp("", config.Name)
if err != nil {
logrus.Fatalf("error creating tmpDir %+v", err)
}
defer func(tmpDir string) { _ = os.Remove(tmpDir) }(tmpDir)
listenOn := &(url.URL{Scheme: "unix", Path: filepath.Join(tmpDir, config.ListenOn)})
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 6: create and register nse with nsm")
// ********************************************************************************
prefixServerCount := len(config.PrefixServerURL)
var subscriptions []chan *ipam.PrefixResponse
for i := 0; i < prefixServerCount; i++ {
subscriptions = append(subscriptions, make(chan *ipam.PrefixResponse, 1))
}
var closeSubscribedChannels = func() {
for i := 0; i < prefixServerCount; i++ {
close(subscriptions[i])
}
}
startListenPrefixes(ctx, config, tlsClientConfig, subscriptions)
ipams := extractIPAMList(ctx, subscriptions)
server := createVl3Endpoint(ctx, cancel, config, vppConn, tlsServerConfig, source, loopOptions, vrfOptions, ipams)
srvErrCh := grpcutils.ListenAndServe(ctx, listenOn, server)
exitOnErr(ctx, cancel, srvErrCh)
log.FromContext(ctx).Infof("grpc server started")
nseRegistration := ®istryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: listenOn.String(),
}
for _, serviceName := range config.ServiceNames {
nseRegistration.NetworkServiceLabels[serviceName] = ®istryapi.NetworkServiceLabels{Labels: config.Labels}
}
nseRegistration, err = nseRegistryClient.Register(ctx, nseRegistration)
log.FromContext(ctx).Infof("registered nse: %+v", nseRegistration)
if err != nil {
log.FromContext(ctx).Fatalf("unable to register nse %+v", err)
}
// Update the nseList to make sure we have all registered vl3-endpoints
nseStream, err := nseRegistryClient.Find(ctx, ®istryapi.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: ®istryapi.NetworkServiceEndpoint{
NetworkServiceNames: config.ServiceNames,
},
})
if err != nil {
log.FromContext(ctx).Fatalf("error getting nses: %+v", err)
}
var nseList = registryapi.ReadNetworkServiceEndpointList(nseStream)
requestCtx, cancelRequest := context.WithTimeout(signalCtx, config.RequestTimeout)
defer cancelRequest()
conn, err := nsmClient.Request(requestCtx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: config.Name + "-kernel",
NetworkServiceEndpointName: config.Name,
NetworkService: config.ServiceNames[0],
Payload: payload.IP,
Labels: config.RequestLabels,
},
})
if err != nil {
log.FromContext(ctx).Fatal(err.Error())
}
defer func(conn *networkservice.Connection) {
closeCtx, cancelClose := context.WithTimeout(ctx, config.RequestTimeout)
defer cancelClose()
_, _ = nsmClient.Close(closeCtx, conn)
}(conn)
config.dnsServerAddr = conn.GetContext().GetIpContext().GetSrcIPNets()[0].IP
config.dnsServerAddrCh <- conn.GetContext().GetIpContext().GetSrcIPNets()[0].IP
vl3Client := createVl3Client(ctx, config, vppConn, tlsClientConfig, source, loopOptions, vrfOptions, ipams, clientAdditionalFunctionality...)
for _, nse := range nseList {
if nse.Name == config.Name {
continue
}
if nse.GetInitialRegistrationTime().AsTime().Local().After(nseRegistration.GetInitialRegistrationTime().AsTime().Local()) {
continue
}
log.FromContext(ctx).Infof("connect to %v", nse.String())
request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkServiceEndpointName: nse.Name,
NetworkService: config.ServiceNames[0],
Payload: payload.IP,
Labels: config.RequestLabels,
},
}
requestCtx, cancelRequest = context.WithTimeout(signalCtx, config.RequestTimeout)
defer cancelRequest()
conn, err = vl3Client.Request(requestCtx, request)
if err != nil {
log.FromContext(ctx).Errorf("request has failed: %v", err.Error())
continue
}
defer func() {
closeCtx, cancelClose := context.WithTimeout(ctx, config.RequestTimeout)
defer cancelClose()
_, _ = vl3Client.Close(closeCtx, conn)
}()
}
if config.UnregisterItself {
defer func() {
_, err = nseRegistryClient.Unregister(context.Background(), nseRegistration)
if err != nil {
log.FromContext(ctx).Errorf("nse failed to unregister itself on termination: %s", err.Error())
}
}()
}
// wait for server to exit
<-signalCtx.Done()
closeSubscribedChannels()
}
func extractIPAMList(ctx context.Context, subscriptions []chan *ipam.PrefixResponse) []*vl3.IPAM {
ipams := make([]*vl3.IPAM, len(subscriptions))
for i := range ipams {
ipams[i] = new(vl3.IPAM)
}
handleChannel := func(prefixCh <-chan *ipam.PrefixResponse, ipam *vl3.IPAM) {
for prefix := range prefixCh {
err := ipam.Reset(prefix.Prefix, prefix.ExcludePrefixes...)
if err != nil {
log.FromContext(ctx).Errorf("failed to reset vl3 IPAM pool: %s", err.Error())
}
}
}
for i, ch := range subscriptions {
go handleChannel(ch, ipams[i])
}
return ipams
}
func createVl3Client(ctx context.Context, config *Config, vppConn vpphelper.Connection, tlsClientConfig *tls.Config, source x509svid.Source,
loopOpts []loopback.Option, vrfOpts []vrf.Option, ipams []*vl3.IPAM, clientAdditionalFunctionality ...networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
dialOptions := append(tracing.WithTracingDial(),
grpcfd.WithChainStreamInterceptor(),
grpcfd.WithChainUnaryInterceptor(),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
grpc.PerRPCCredentials(token.NewPerRPCCredentials(spiffejwt.TokenGeneratorFunc(source, config.MaxTokenLifetime))),
),
grpc.WithTransportCredentials(
grpcfd.TransportCredentials(
credentials.NewTLS(
tlsClientConfig,
),
),
),
)
c := client.NewClient(
ctx,
client.WithClientURL(&config.ConnectTo),
client.WithName(config.Name),
client.WithAdditionalFunctionality(
append(
clientAdditionalFunctionality,
newMultiIPAMClient(ctx, ipams),
vl3dns.NewClient(config.dnsServerAddr, &config.dnsConfigs),
up.NewClient(ctx, vppConn, up.WithLoadSwIfIndex(loopback.Load)),
ipaddress.NewClient(vppConn, ipaddress.WithLoadSwIfIndex(loopback.Load)),
loopback.NewClient(vppConn, loopOpts...),
up.NewClient(ctx, vppConn),
mtu.NewClient(vppConn),
routes.NewClient(vppConn),
unnumbered.NewClient(vppConn, loopback.Load),
vrf.NewClient(vppConn, vrfOpts...),
memif.NewClient(ctx, vppConn),
sendfd.NewClient(),
recvfd.NewClient(),
)...,
),
client.WithHealClient(null.NewClient()),
client.WithDialTimeout(config.DialTimeout),
client.WithDialOptions(dialOptions...),
)
return retry.NewClient(c)
}
func newMultiIPAMClient(ctx context.Context, ipams []*vl3.IPAM) networkservice.NetworkServiceClient {
var clients []networkservice.NetworkServiceClient
for _, ipam := range ipams {
clients = append(clients, vl3.NewClient(ctx, ipam))
}
return next.NewNetworkServiceClient(clients...)
}
func createVl3Endpoint(ctx context.Context, cancel context.CancelFunc, config *Config, vppConn vpphelper.Connection, tlsServerConfig *tls.Config,
source x509svid.Source, loopOpts []loopback.Option, vrfOpts []vrf.Option, ipams []*vl3.IPAM) *grpc.Server {
vl3Endpoint := endpoint.NewServer(ctx,
spiffejwt.TokenGeneratorFunc(source, config.MaxTokenLifetime),
endpoint.WithName(config.Name),
endpoint.WithAuthorizeServer(authorize.NewServer()),
endpoint.WithAdditionalFunctionality(
onidle.NewServer(ctx, cancel, config.IdleTimeout),
vl3dns.NewServer(ctx,
config.dnsServerAddrCh,
vl3dns.WithDomainSchemes(config.DNSTemplates...),
vl3dns.WithConfigs(&config.dnsConfigs),
),
vl3mtu.NewServer(),
strictvl3ipam.NewServer(ctx, vl3.NewServer, ipams...),
up.NewServer(ctx, vppConn, up.WithLoadSwIfIndex(loopback.Load)),
ipaddress.NewServer(vppConn, ipaddress.WithLoadSwIfIndex(loopback.Load)),
unnumbered.NewServer(vppConn, loopback.Load),
vrf.NewServer(vppConn, vrfOpts...),
loopback.NewServer(vppConn, loopOpts...),
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
memif.MECHANISM: chain.NewNetworkServiceServer(
sendfd.NewServer(),
up.NewServer(ctx, vppConn),
mtu.NewServer(vppConn),
routes.NewServer(vppConn),
tag.NewServer(ctx, vppConn),
memif.NewServer(ctx, vppConn),
),
}),
),
)
options := append(
tracing.WithTracing(),
grpc.Creds(
grpcfd.TransportCredentials(
credentials.NewTLS(
tlsServerConfig,
),
),
),
)
server := grpc.NewServer(options...)
vl3Endpoint.Register(server)
return server
}
func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
// If we already have an error, log it and exit
select {
case err := <-errCh:
log.FromContext(ctx).Fatal(err)
default:
}
// Otherwise wait for an error in the background to log and cancel
go func(ctx context.Context, errCh <-chan error) {
err := <-errCh
log.FromContext(ctx).Error(err)
cancel()
}(ctx, errCh)
}
func notifyContext(ctx context.Context) (context.Context, context.CancelFunc) {
return signal.NotifyContext(
ctx,
os.Interrupt,
// More Linux signals here
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGQUIT,
)
}