Skip to content

Commit

Permalink
Add sandbox.Unregister()
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Jan 29, 2021
1 parent 2e7b290 commit fc55c72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
28 changes: 8 additions & 20 deletions pkg/networkservice/chains/nsmgr/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,23 +548,15 @@ func testNSEAndClient(
domain *sandbox.Domain,
nseReg *registry.NetworkServiceEndpoint,
) {
// 1. Create an endpoint
nseCtx, nseCancel := context.WithCancel(ctx)
defer nseCancel()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

_, err := sandbox.NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
_, err := sandbox.NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
require.NoError(t, err)

// 2. Create a client
clientCtx, clientCancel := context.WithCancel(ctx)
defer clientCancel()

nsc := sandbox.NewClient(clientCtx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)

// 3. Request endpoint
requestCtx, requestCancel := context.WithCancel(ctx)
nsc := sandbox.NewClient(ctx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)

conn, err := nsc.Request(requestCtx, &networkservice.NetworkServiceRequest{
conn, err := nsc.Request(ctx, &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernelmech.MECHANISM},
},
Expand All @@ -574,15 +566,11 @@ func testNSEAndClient(
})
require.NoError(t, err)

requestCancel()

// 4. Close connection
closeCtx, closeCancel := context.WithCancel(ctx)

_, err = nsc.Close(closeCtx, conn)
_, err = nsc.Close(ctx, conn)
require.NoError(t, err)

closeCancel()
err = sandbox.UnregisterEndpoint(ctx, nseReg, domain.Nodes[0].NSMgr)
require.NoError(t, err)
}

type passThroughClient struct {
Expand Down
37 changes: 24 additions & 13 deletions pkg/tools/sandbox/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice/payload"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/networkservicemesh/sdk/pkg/tools/logger"
Expand Down Expand Up @@ -64,17 +65,22 @@ func NewEndpoint(ctx context.Context, nse *registry.NetworkServiceEndpoint, gene
}
}
serve(ctx, u, ep.Register)

if nse.Url == "" {
nse.Url = u.String()
}

if nse.ExpirationTime == nil {
nse.ExpirationTime = timestamppb.New(time.Now().Add(time.Hour))
}
if nse, err = mgr.NetworkServiceEndpointRegistryServer().Register(ctx, nse); err != nil {

var reg *registry.NetworkServiceEndpoint
if reg, err = mgr.NetworkServiceEndpointRegistryServer().Register(ctx, nse); err != nil {
return nil, err
}

nse.Name = reg.Name
nse.ExpirationTime = reg.ExpirationTime

for _, service := range nse.NetworkServiceNames {
if _, err := mgr.NetworkServiceRegistryServer().Register(ctx, &registry.NetworkService{
Name: service,
Expand All @@ -84,22 +90,27 @@ func NewEndpoint(ctx context.Context, nse *registry.NetworkServiceEndpoint, gene
}
}

go func() {
<-ctx.Done()
for _, service := range nse.NetworkServiceNames {
_, _ = mgr.NetworkServiceRegistryServer().Unregister(context.Background(), &registry.NetworkService{
Name: service,
Payload: payload.IP,
})
}
_, _ = mgr.NetworkServiceEndpointRegistryServer().Unregister(context.Background(), nse)
}()

logger.Log(ctx).Infof("Started listen endpoint %v on %v.", nse.Name, u.String())

return &EndpointEntry{Endpoint: ep, URL: u}, nil
}

// UnregisterEndpoint unregisters endpoint from NSMgr
func UnregisterEndpoint(ctx context.Context, nse *registry.NetworkServiceEndpoint, mgr nsmgr.Nsmgr) (err error) {
if _, unregisterErr := mgr.NetworkServiceEndpointRegistryServer().Unregister(ctx, nse); unregisterErr != nil {
err = errors.Wrapf(unregisterErr, "%v\n", err)
}
for _, name := range nse.NetworkServiceNames {
if _, unregisterErr := mgr.NetworkServiceRegistryServer().Unregister(ctx, &registry.NetworkService{
Name: name,
Payload: payload.IP,
}); unregisterErr != nil {
err = errors.Wrapf(unregisterErr, "%v\n", err)
}
}
return err
}

// NewClient is a client.NewClient over *url.URL with some fields preset for testing
func NewClient(ctx context.Context, generatorFunc token.GeneratorFunc, connectTo *url.URL, additionalFunctionality ...networkservice.NetworkServiceClient) networkservice.NetworkServiceClient {
return clienturl.NewClient(
Expand Down

0 comments on commit fc55c72

Please sign in to comment.