forked from cisco-app-networking/networkservicemesh
/
registry.go
217 lines (181 loc) · 6.93 KB
/
registry.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
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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.
package nsmd
import (
"time"
"github.com/adodon2go/networkservicemesh/utils"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/adodon2go/networkservicemesh/pkg/tools/spanhelper"
"github.com/adodon2go/networkservicemesh/controlplane/pkg/model"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"github.com/adodon2go/networkservicemesh/controlplane/api/registry"
)
const (
// NSETrackingIntervalDefault - default registry notification interval that NSE is still alive
NSETrackingIntervalDefault = 2 * time.Minute
// NSETrackingIntervalSecondsEnv - environment variable contains registry notification interval that NSE is still alive in seconds
NSETrackingIntervalSecondsEnv = utils.EnvVar("NSE_TRACKING_INTERVAL")
)
type NSERegistryServer interface {
registry.NetworkServiceRegistryServer
RegisterNSEWithClient(ctx context.Context, request *registry.NSERegistration, client registry.NetworkServiceRegistryClient) (*registry.NSERegistration, error)
}
type registryServer struct {
nsm *nsmServer
workspace *Workspace
nseTrackers map[string]chan bool
}
func NewRegistryServer(nsm *nsmServer, workspace *Workspace) NSERegistryServer {
return ®istryServer{
nsm: nsm,
workspace: workspace,
nseTrackers: make(map[string]chan bool),
}
}
func (es *registryServer) RegisterNSE(ctx context.Context, request *registry.NSERegistration) (*registry.NSERegistration, error) {
span := spanhelper.FromContext(ctx, "RegisterNSE")
defer span.Finish()
span.Logger().Infof("Received RegisterNSE request: %v", request)
// Check if there is already Network Service Endpoint object with the same name, if there is
// success will be returned to NSE, since it is a case of NSE pod coming back up.
client, err := es.nsm.serviceRegistry.NseRegistryClient(span.Context())
if err != nil {
err = errors.Wrap(err, "attempt to connect to upstream registry failed with")
span.LogError(err)
return nil, err
}
reg, err := es.RegisterNSEWithClient(span.Context(), request, client)
if err != nil {
span.LogError(err)
return reg, err
}
// Append to workspace...
err = es.workspace.localRegistry.AppendNSERegRequest(es.workspace.name, reg)
if err != nil {
err = errors.Errorf("failed to store NSE into local registry service: %v", err)
span.LogError(err)
_, _ = client.RemoveNSE(span.Context(), ®istry.RemoveNSERequest{NetworkServiceEndpointName: reg.GetNetworkServiceEndpoint().GetName()})
return nil, err
}
span.LogObject("registration", reg)
return reg, nil
}
func (es *registryServer) RegisterNSEWithClient(ctx context.Context, request *registry.NSERegistration, client registry.NetworkServiceRegistryClient) (*registry.NSERegistration, error) {
// Some notes here:
// 1) Yes, we are overwriting anything we get for NetworkServiceManager
// from the NSE. NSE's shouldn't specify NetworkServiceManager
// 2) We are not specifying Name, the nsmd-k8s will fill those
// in
request.NetworkServiceManager = ®istry.NetworkServiceManager{
Url: es.nsm.serviceRegistry.GetPublicAPI(),
}
registration, err := client.RegisterNSE(ctx, request)
if err != nil {
err = errors.Wrap(err, "attempt to pass through from nsm to upstream registry failed with")
logrus.Error(err)
return nil, err
}
ep := es.nsm.model.GetEndpoint(registration.GetNetworkServiceEndpoint().GetName())
modelEndpoint := &model.Endpoint{
SocketLocation: es.workspace.NsmClientSocket(),
Endpoint: registration,
Workspace: es.workspace.Name(),
}
if ep == nil {
es.nsm.model.AddEndpoint(ctx, modelEndpoint)
}
logrus.Infof("Received upstream NSERegitration: %v", registration)
err = es.startNSETracking(registration)
if err != nil {
logrus.Infof("Error starting NSE tracking requests : %v", err)
}
return registration, nil
}
func (es *registryServer) BulkRegisterNSE(srv registry.NetworkServiceRegistry_BulkRegisterNSEServer) error {
<-srv.Context().Done()
return nil
}
func (es *registryServer) RemoveNSE(ctx context.Context, request *registry.RemoveNSERequest) (*empty.Empty, error) {
span := spanhelper.FromContext(ctx, "RemoveNSE")
defer span.Finish()
span.LogObject("request", request)
// TODO make sure we track which registry server we got the RegisterNSE from so we can only allow a deletion
// of what you advertised
span.Logger().Infof("Received Endpoint Remove request: %+v", request)
err := es.stopNSETracking(request.NetworkServiceEndpointName)
if err != nil {
span.Logger().Warnf("Attempt to stop tracking NSE failed : %v", err)
}
client, err := es.nsm.serviceRegistry.NseRegistryClient(span.Context())
if err != nil {
err = errors.Wrap(err, "attempt to pass through from nsm to upstream registry failed with")
span.LogError(err)
return nil, err
}
_, err = client.RemoveNSE(span.Context(), request)
if err != nil {
err = errors.Wrap(err, "attempt to pass through from nsm to upstream registry failed")
span.LogError(err)
return nil, err
}
es.nsm.model.DeleteEndpoint(span.Context(), request.GetNetworkServiceEndpointName())
return &empty.Empty{}, nil
}
func (es *registryServer) Close() {
}
func (es *registryServer) startNSETracking(request *registry.NSERegistration) error {
ctx, cancel := context.WithCancel(context.Background())
client, err := es.nsm.serviceRegistry.NseRegistryClient(ctx)
if err != nil {
cancel()
return errors.Wrapf(err, "cannot start NSE tracking : %v", err)
}
stream, err := client.BulkRegisterNSE(ctx)
if err != nil {
cancel()
return errors.Wrapf(err, "cannot start NSE tracking : %v", err)
}
stopped := make(chan bool)
es.nseTrackers[request.NetworkServiceEndpoint.Name] = stopped
trackingInterval := NSETrackingIntervalSecondsEnv.GetOrDefaultDuration(NSETrackingIntervalDefault)
go func() {
defer cancel()
for {
select {
case <-ctx.Done():
goto FinishTracking
case <-stopped:
goto FinishTracking
case <-time.After(trackingInterval):
err := stream.Send(request)
if err != nil {
logrus.Errorf("Error sending BulkRegisterNSE request : %v", err)
}
}
}
FinishTracking:
delete(es.nseTrackers, request.NetworkServiceEndpoint.Name)
logrus.Errorf("NSE tracking done : %v", request)
}()
return nil
}
func (es *registryServer) stopNSETracking(nseName string) error {
if c, ok := es.nseTrackers[nseName]; ok {
c <- true
return nil
}
return errors.Errorf("tracker for NSE with name %s not found ", nseName)
}