forked from networkservicemesh/cmd-nsc-vpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
250 lines (218 loc) · 9.32 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
// Copyright (c) 2021 Doc.ai 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.
// +build linux
package main
import (
"context"
"net/url"
"os"
"os/signal"
"syscall"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/debug"
"github.com/edwarnicke/grpcfd"
"github.com/edwarnicke/vpphelper"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/connectioncontext"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/memif"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/up"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/heal"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/recvfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/retry"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
"github.com/networkservicemesh/sdk/pkg/tools/nsurl"
"github.com/networkservicemesh/sdk/pkg/tools/opentracing"
"github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
"github.com/networkservicemesh/sdk/pkg/tools/token"
)
// Config - configuration for cmd-forwarder-vpp
type Config struct {
Name string `default:"cmd-nsc-vpp" desc:"Name of Endpoint"`
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"`
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"`
NetworkServices []url.URL `default:"" desc:"A list of Network Service Requests" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// ********************************************************************************
// setup logging
// ********************************************************************************
logrus.SetFormatter(&nested.Formatter{})
log.EnableTracing(true)
ctx = log.WithFields(ctx, map[string]interface{}{"cmd": os.Args[0]})
ctx = log.WithLog(ctx, logruslogger.New(ctx))
// ********************************************************************************
// Debug self if necessary
// ********************************************************************************
if err := debug.Self(); err != nil {
log.FromContext(ctx).Infof("%s", err)
}
starttime := time.Now()
// enumerating phases
log.FromContext(ctx).Infof("there are 5 phases which will be executed followed by a success message:")
log.FromContext(ctx).Infof("the phases include:")
log.FromContext(ctx).Infof("1: get config from environment")
log.FromContext(ctx).Infof("2: run vpp and get a connection to it")
log.FromContext(ctx).Infof("3: retrieve spiffe svid")
log.FromContext(ctx).Infof("4: create network service client")
log.FromContext(ctx).Infof("5: connect to all passed services")
log.FromContext(ctx).Infof("a final success message with start time duration")
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 1: get config from environment (time since start: %s)", time.Since(starttime))
// ********************************************************************************
now := time.Now()
config := &Config{}
if err := envconfig.Usage("nsm", config); err != nil {
logrus.Fatal(err)
}
if err := envconfig.Process("nsm", config); err != nil {
logrus.Fatalf("error processing config from env: %+v", err)
}
log.FromContext(ctx).Infof("Config: %#v", config)
l, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
logrus.Fatalf("invalid log level %s", config.LogLevel)
}
logrus.SetLevel(l)
log.FromContext(ctx).WithField("duration", time.Since(now)).Infof("completed phase 1: get config from environment")
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 2: run vpp and get a connection to it (time since start: %s)", time.Since(starttime))
// ********************************************************************************
now = time.Now()
vppConn, vppErrCh := vpphelper.StartAndDialContext(ctx)
exitOnErrCh(ctx, cancel, vppErrCh)
defer func() {
cancel()
<-vppErrCh
}()
log.FromContext(ctx).WithField("duration", time.Since(now)).Info("completed phase 2: run vpp and get a connection to it")
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 3: retrieving svid, check spire agent logs if this is the last line you see (time since start: %s)", time.Since(starttime))
// ********************************************************************************
now = time.Now()
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)
}
logrus.Infof("SVID: %q", svid.ID)
log.FromContext(ctx).WithField("duration", time.Since(now)).Info("completed phase 3: retrieving svid")
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 4: create network service client (time since start: %s)", time.Since(starttime))
// ********************************************************************************
dialOptions := append(opentracing.WithTracingDial(),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
grpc.PerRPCCredentials(token.NewPerRPCCredentials(spiffejwt.TokenGeneratorFunc(source, config.MaxTokenLifetime))),
),
grpc.WithTransportCredentials(
grpcfd.TransportCredentials(
credentials.NewTLS(
tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeAny()),
),
),
),
grpcfd.WithChainStreamInterceptor(),
grpcfd.WithChainUnaryInterceptor(),
)
c := client.NewClient(
ctx,
client.WithClientURL(&config.ConnectTo),
client.WithName(config.Name),
client.WithHealClient(heal.NewClient(ctx)),
client.WithAdditionalFunctionality(
metadata.NewClient(),
up.NewClient(ctx, vppConn),
connectioncontext.NewClient(vppConn),
memif.NewClient(vppConn),
sendfd.NewClient(),
recvfd.NewClient(),
),
client.WithDialTimeout(config.DialTimeout),
client.WithDialOptions(dialOptions...),
)
c = retry.NewClient(c, retry.WithTryTimeout(config.RequestTimeout))
// ********************************************************************************
log.FromContext(ctx).Infof("executing phase 5: connect to all passed services (time since start: %s)", time.Since(starttime))
// ********************************************************************************
signalCtx, cancelSignalCtx := notifyContext(ctx)
defer cancelSignalCtx()
for i := 0; i < len(config.NetworkServices); i++ {
u := nsurl.NSURL(config.NetworkServices[i])
mech := u.Mechanism()
if mech.Type != memif.MECHANISM {
log.FromContext(ctx).Fatalf("mechanism type: %v is not supproted", mech.Type)
}
request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: u.NetworkService(),
Labels: u.Labels(),
},
}
resp, err := c.Request(ctx, request)
if err != nil {
log.FromContext(ctx).Fatalf("request has failed: %v", err.Error())
}
defer func() {
closeCtx, cancelClose := context.WithTimeout(ctx, config.RequestTimeout)
defer cancelClose()
_, _ = c.Close(closeCtx, resp)
}()
}
<-signalCtx.Done()
}
func exitOnErrCh(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,
)
}