Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for point-to-multipoint forwarders in NSM #1122

Merged
merged 6 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 53 additions & 59 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ package nsmgr

import (
"context"
"fmt"
"net/url"
"time"

"github.com/google/uuid"

"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -35,13 +37,10 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clientinfo"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/connect"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/discover"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/discoverforwarder"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/excludedprefixes"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/filtermechanisms"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/interpose"
"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/roundrobin"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/registry"
"github.com/networkservicemesh/sdk/pkg/registry/common/checkid"
Expand All @@ -51,13 +50,13 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/expire"
"github.com/networkservicemesh/sdk/pkg/registry/common/localbypass"
"github.com/networkservicemesh/sdk/pkg/registry/common/memory"
"github.com/networkservicemesh/sdk/pkg/registry/common/querycache"
registryrecvfd "github.com/networkservicemesh/sdk/pkg/registry/common/recvfd"
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"

registryserialize "github.com/networkservicemesh/sdk/pkg/registry/common/serialize"
"github.com/networkservicemesh/sdk/pkg/registry/common/setlogoption"
registryadapter "github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
registrychain "github.com/networkservicemesh/sdk/pkg/registry/core/chain"
"github.com/networkservicemesh/sdk/pkg/registry/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/token"
)
Expand All @@ -75,13 +74,14 @@ type nsmgrServer struct {
}

type serverOptions struct {
authorizeServer networkservice.NetworkServiceServer
dialOptions []grpc.DialOption
dialTimeout time.Duration
regURL *url.URL
regDialOptions []grpc.DialOption
name string
url string
authorizeServer networkservice.NetworkServiceServer
dialOptions []grpc.DialOption
dialTimeout time.Duration
regURL *url.URL
regDialOptions []grpc.DialOption
name string
url string
forwarderServiceName string
}

// Option modifies server option value
Expand All @@ -94,6 +94,14 @@ func WithDialOptions(dialOptions ...grpc.DialOption) Option {
}
}

// WithForwarderServiceName overrides default forwarder service name
// By default "forwarder"
func WithForwarderServiceName(forwarderServiceName string) Option {
return func(o *serverOptions) {
o.forwarderServiceName = forwarderServiceName
}
}

// WithDialTimeout sets dial timeout for the client
func WithDialTimeout(dialTimeout time.Duration) Option {
return func(o *serverOptions) {
Expand Down Expand Up @@ -141,64 +149,66 @@ var _ Nsmgr = (*nsmgrServer)(nil)
// options - a set of Nsmgr options.
func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...Option) Nsmgr {
opts := &serverOptions{
authorizeServer: authorize.NewServer(authorize.Any()),
name: "nsmgr-" + uuid.New().String(),
authorizeServer: authorize.NewServer(authorize.Any()),
name: "nsmgr-" + uuid.New().String(),
forwarderServiceName: "forwarder",
}
for _, opt := range options {
opt(opts)
}

rv := &nsmgrServer{}

var urlsRegistryServer, interposeRegistryServer registryapi.NetworkServiceEndpointRegistryServer

var nsRegistry registryapi.NetworkServiceRegistryServer
var nsRegistry = memory.NewNetworkServiceRegistryServer()
if opts.regURL != nil {
// Use remote registry
nsRegistry = registrychain.NewNetworkServiceRegistryServer(
clienturl.NewNetworkServiceRegistryServer(opts.regURL),
registryconnect.NewNetworkServiceRegistryServer(ctx, registryconnect.WithDialOptions(opts.regDialOptions...)),
)
} else {
// Use memory registry if no registry is passed
nsRegistry = memory.NewNetworkServiceRegistryServer()
}

var nseRegistry registryapi.NetworkServiceEndpointRegistryServer
nsRegistry = registrychain.NewNetworkServiceRegistryServer(
registryserialize.NewNetworkServiceRegistryServer(),
setlogoption.NewNetworkServiceRegistryServer(map[string]string{"name": "NetworkServiceRegistryServer." + opts.name}),
nsRegistry,
)

var nseInMemoryRegistry = registrychain.NewNetworkServiceEndpointRegistryServer(
Copy link
Member Author

@denis-tingaikin denis-tingaikin Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: now nsmgr has its own memory registry cache by default.

setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{"name": fmt.Sprintf("NetworkServiceRegistryServer.%v", opts.name)}),
registryclientinfo.NewNetworkServiceEndpointRegistryServer(),
registryserialize.NewNetworkServiceEndpointRegistryServer(),
checkid.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
registryrecvfd.NewNetworkServiceEndpointRegistryServer(), // Allow to receive a passed files
registrysendfd.NewNetworkServiceEndpointRegistryServer(),
memory.NewNetworkServiceEndpointRegistryServer(),
localbypass.NewNetworkServiceEndpointRegistryServer(opts.url),
)

var nseRegistry = nseInMemoryRegistry

if opts.regURL != nil {
// Use remote registry
// Add remote registry
nseRegistry = registrychain.NewNetworkServiceEndpointRegistryServer(
nseRegistry,
clienturl.NewNetworkServiceEndpointRegistryServer(opts.regURL),
registryconnect.NewNetworkServiceEndpointRegistryServer(ctx, registryconnect.WithDialOptions(opts.regDialOptions...)),
)
} else {
// Use memory registry if no registry is passed
nseRegistry = memory.NewNetworkServiceEndpointRegistryServer()
}

localBypassRegistryServer := localbypass.NewNetworkServiceEndpointRegistryServer(opts.url)

nseClient := next.NewNetworkServiceEndpointRegistryClient(
registryserialize.NewNetworkServiceEndpointRegistryClient(),
registryadapter.NetworkServiceEndpointServerToClient(localBypassRegistryServer),
querycache.NewClient(ctx),
registryadapter.NetworkServiceEndpointServerToClient(nseRegistry),
)

nsClient := registryadapter.NetworkServiceServerToClient(nsRegistry)

// Construct Endpoint
rv.Endpoint = endpoint.NewServer(ctx, tokenGenerator,
endpoint.WithName(opts.name),
endpoint.WithAuthorizeServer(opts.authorizeServer),
endpoint.WithAdditionalFunctionality(
adapters.NewClientToServer(clientinfo.NewClient()),
discover.NewServer(nsClient, nseClient),
roundrobin.NewServer(),
discoverforwarder.NewServer(
registryadapter.NetworkServiceEndpointServerToClient(nseInMemoryRegistry),
discoverforwarder.WithForwarderServiceName(opts.forwarderServiceName),
),
excludedprefixes.NewServer(ctx),
recvfd.NewServer(), // Receive any files passed
interpose.NewServer(&interposeRegistryServer),
filtermechanisms.NewServer(&urlsRegistryServer),
connect.NewServer(
client.NewClient(
ctx,
Expand All @@ -215,27 +225,11 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
sendfd.NewServer()),
)

nsChain := registrychain.NewNetworkServiceRegistryServer(
setlogoption.NewNetworkServiceRegistryServer(map[string]string{"name": opts.name}),
registryserialize.NewNetworkServiceRegistryServer(),
rv.Registry = registry.NewServer(
nsRegistry,
nseRegistry,
)

nseChain := registrychain.NewNetworkServiceEndpointRegistryServer(
setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{"name": opts.name}),
registryclientinfo.NewNetworkServiceEndpointRegistryServer(),
registryserialize.NewNetworkServiceEndpointRegistryServer(),
checkid.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
registryrecvfd.NewNetworkServiceEndpointRegistryServer(), // Allow to receive a passed files
urlsRegistryServer, // Store endpoints URLs
interposeRegistryServer, // Store cross connect NSEs
localBypassRegistryServer, // Perform URL transformations
nseRegistry, // Register NSE inside Remote registry
)

rv.Registry = registry.NewServer(nsChain, nseChain)

return rv
}

Expand Down
78 changes: 77 additions & 1 deletion pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func Test_ShouldCorrectlyAddForwardersWithSameNames(t *testing.T) {
require.NoError(t, err)

forwarderReg := &registry.NetworkServiceEndpoint{
Name: "forwarder",
Name: "forwarder",
NetworkServiceNames: []string{"forwarder"},
Copy link
Member Author

@denis-tingaikin denis-tingaikin Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edwarnicke

  1. Should we add const for forwarder networkservicename into api?
  2. Should we create a registry network service endpoint client chain element for registration forwarders?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a registry network service endpoint client chain element for registration forwarders?

I don't quite follow this, could you say more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can add something like:

type setForwarderNetworkServiceNameNSEClient struct{} 

func (n *setNetworkServiceNamesNSEClient) Register(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
	in.NetworkServiceNames = []string{"forwarder"}
...

This client can be used by forwarders on registration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... that's interesting... but how does it handle the case of that being overriden for some reason by env variables in the forwarder?

Better question... where is the 'forwarder' string encoded in the NSMgr ?

Copy link
Member Author

@denis-tingaikin denis-tingaikin Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better question... where is the 'forwarder' string encoded in the NSMgr ?

See at discovercrossnse chain element.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are fine with this, then I'll add the option as you suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forwarders should already be able to overload the NS name via ENV variable... do they not?

Copy link
Member Author

@denis-tingaikin denis-tingaikin Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but all forwarders and nsmgrs on the cluster should use the same network service name. So constant in api can simplify it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly, I like your suggestion, but just shared another possible way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1a0be07

}

// 1. Add forwarders
Expand Down Expand Up @@ -253,3 +254,78 @@ func Test_ShouldParseNetworkServiceLabelsTemplate(t *testing.T) {
_, err = nse.Unregister(ctx, nseReg)
require.NoError(t, err)
}

func Test_UsecasePoint2MultiPoint(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edwarnicke Is this test look valid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetRegistryProxySupplier(nil).
SetNodeSetup(func(ctx context.Context, node *sandbox.Node, _ int) {
node.NewNSMgr(ctx, "nsmgr", nil, sandbox.GenerateTestToken, nsmgr.NewServer)
}).
SetRegistryExpiryDuration(sandbox.RegistryExpiryDuration).
Build()

domain.Nodes[0].NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: "p2mp forwarder",
NetworkServiceNames: []string{"forwarder"},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"forwarder": {
Labels: map[string]string{
"p2mp": "true",
},
},
},
}, sandbox.GenerateTestToken)

domain.Nodes[0].NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: "p2p forwarder",
NetworkServiceNames: []string{"forwarder"},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"forwarder": {
Labels: map[string]string{
"p2p": "true",
},
},
},
}, sandbox.GenerateTestToken)

domain.Nodes[0].NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: "special forwarder",
NetworkServiceNames: []string{"forwarder"},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"forwarder": {
Labels: map[string]string{
"special": "true",
},
},
},
}, sandbox.GenerateTestToken)

nsRegistryClient := domain.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService())
require.NoError(t, err)

nseReg := defaultRegistryEndpoint(nsReg.Name)

domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)

nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := defaultRequest(nsReg.Name)

request.GetConnection().Labels = map[string]string{
"p2mp": "true",
}

conn, err := nsc.Request(ctx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 4, len(conn.Path.PathSegments))
require.Equal(t, "p2mp forwarder", conn.GetPath().GetPathSegments()[2].Name)
}
Loading