From aafdd639d18214e34d6a7bde6c86632b5d731040 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Tue, 13 Sep 2022 19:14:05 +0700 Subject: [PATCH 1/3] add options for registry-memory server Signed-off-by: Nikita Skrynnik --- pkg/registry/chains/memory/server.go | 55 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/pkg/registry/chains/memory/server.go b/pkg/registry/chains/memory/server.go index be2875547..47ad4d601 100644 --- a/pkg/registry/chains/memory/server.go +++ b/pkg/registry/chains/memory/server.go @@ -26,7 +26,10 @@ import ( "github.com/networkservicemesh/api/pkg/api/registry" + registryapi "github.com/networkservicemesh/api/pkg/api/registry" registryserver "github.com/networkservicemesh/sdk/pkg/registry" + registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize" + "github.com/networkservicemesh/sdk/pkg/registry/common/begin" "github.com/networkservicemesh/sdk/pkg/registry/common/clientconn" "github.com/networkservicemesh/sdk/pkg/registry/common/clienturl" @@ -41,10 +44,55 @@ import ( "github.com/networkservicemesh/sdk/pkg/tools/interdomain" ) +type serverOptions struct { + authorizeNSRegistryServer registryapi.NetworkServiceRegistryServer + authorizeNSERegistryServer registryapi.NetworkServiceEndpointRegistryServer + dialOptions []grpc.DialOption +} + +// Option modifies server option value +type Option func(o *serverOptions) + +// WithDialOptions sets grpc.DialOptions for the client +func WithDialOptions(dialOptions ...grpc.DialOption) Option { + return func(o *serverOptions) { + o.dialOptions = dialOptions + } +} + +// WithAuthorizeNSRegistryServer sets authorization NetworkServiceRegistry chain element +func WithAuthorizeNSRegistryServer(authorizeNSRegistryServer registryapi.NetworkServiceRegistryServer) Option { + if authorizeNSRegistryServer == nil { + panic("authorizeNSRegistryServer cannot be nil") + } + return func(o *serverOptions) { + o.authorizeNSRegistryServer = authorizeNSRegistryServer + } +} + +// WithAuthorizeNSERegistryServer sets authorization NetworkServiceEndpointRegistry chain element +func WithAuthorizeNSERegistryServer(authorizeNSERegistryServer registryapi.NetworkServiceEndpointRegistryServer) Option { + if authorizeNSERegistryServer == nil { + panic("authorizeNSERegistryServer cannot be nil") + } + return func(o *serverOptions) { + o.authorizeNSERegistryServer = authorizeNSERegistryServer + } +} + // NewServer creates new registry server based on memory storage -func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, dialOptions ...grpc.DialOption) registryserver.Registry { +func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...Option) registryserver.Registry { + opts := &serverOptions{ + authorizeNSRegistryServer: registryauthorize.NewNetworkServiceRegistryServer(registryauthorize.Any()), + authorizeNSERegistryServer: registryauthorize.NewNetworkServiceEndpointRegistryServer(registryauthorize.Any()), + } + for _, opt := range options { + opt(opts) + } + nseChain := chain.NewNetworkServiceEndpointRegistryServer( begin.NewNetworkServiceEndpointRegistryServer(), + opts.authorizeNSERegistryServer, switchcase.NewNetworkServiceEndpointRegistryServer(switchcase.NSEServerCase{ Condition: func(c context.Context, nse *registry.NetworkServiceEndpoint) bool { if interdomain.Is(nse.GetName()) { @@ -64,7 +112,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU clienturl.NewNetworkServiceEndpointRegistryClient(proxyRegistryURL), clientconn.NewNetworkServiceEndpointRegistryClient(), dial.NewNetworkServiceEndpointRegistryClient(ctx, - dial.WithDialOptions(dialOptions...), + dial.WithDialOptions(opts.dialOptions...), ), connect.NewNetworkServiceEndpointRegistryClient(), ), @@ -82,6 +130,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU ), ) nsChain := chain.NewNetworkServiceRegistryServer( + opts.authorizeNSRegistryServer, setpayload.NewNetworkServiceRegistryServer(), switchcase.NewNetworkServiceRegistryServer( switchcase.NSServerCase{ @@ -94,7 +143,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU begin.NewNetworkServiceRegistryClient(), clientconn.NewNetworkServiceRegistryClient(), dial.NewNetworkServiceRegistryClient(ctx, - dial.WithDialOptions(dialOptions...), + dial.WithDialOptions(opts.dialOptions...), ), connect.NewNetworkServiceRegistryClient(), ), From c1d4c71e51780a16dc76664a7280d61a37980b16 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Tue, 13 Sep 2022 21:05:23 +0700 Subject: [PATCH 2/3] fix CI Signed-off-by: Nikita Skrynnik --- pkg/registry/chains/memory/server.go | 9 ++++----- pkg/tools/sandbox/builder.go | 2 +- pkg/tools/sandbox/types.go | 3 ++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/registry/chains/memory/server.go b/pkg/registry/chains/memory/server.go index 47ad4d601..9fcd6935e 100644 --- a/pkg/registry/chains/memory/server.go +++ b/pkg/registry/chains/memory/server.go @@ -26,7 +26,6 @@ import ( "github.com/networkservicemesh/api/pkg/api/registry" - registryapi "github.com/networkservicemesh/api/pkg/api/registry" registryserver "github.com/networkservicemesh/sdk/pkg/registry" registryauthorize "github.com/networkservicemesh/sdk/pkg/registry/common/authorize" @@ -45,8 +44,8 @@ import ( ) type serverOptions struct { - authorizeNSRegistryServer registryapi.NetworkServiceRegistryServer - authorizeNSERegistryServer registryapi.NetworkServiceEndpointRegistryServer + authorizeNSRegistryServer registry.NetworkServiceRegistryServer + authorizeNSERegistryServer registry.NetworkServiceEndpointRegistryServer dialOptions []grpc.DialOption } @@ -61,7 +60,7 @@ func WithDialOptions(dialOptions ...grpc.DialOption) Option { } // WithAuthorizeNSRegistryServer sets authorization NetworkServiceRegistry chain element -func WithAuthorizeNSRegistryServer(authorizeNSRegistryServer registryapi.NetworkServiceRegistryServer) Option { +func WithAuthorizeNSRegistryServer(authorizeNSRegistryServer registry.NetworkServiceRegistryServer) Option { if authorizeNSRegistryServer == nil { panic("authorizeNSRegistryServer cannot be nil") } @@ -71,7 +70,7 @@ func WithAuthorizeNSRegistryServer(authorizeNSRegistryServer registryapi.Network } // WithAuthorizeNSERegistryServer sets authorization NetworkServiceEndpointRegistry chain element -func WithAuthorizeNSERegistryServer(authorizeNSERegistryServer registryapi.NetworkServiceEndpointRegistryServer) Option { +func WithAuthorizeNSERegistryServer(authorizeNSERegistryServer registry.NetworkServiceEndpointRegistryServer) Option { if authorizeNSERegistryServer == nil { panic("authorizeNSERegistryServer cannot be nil") } diff --git a/pkg/tools/sandbox/builder.go b/pkg/tools/sandbox/builder.go index d84515322..0a8722207 100644 --- a/pkg/tools/sandbox/builder.go +++ b/pkg/tools/sandbox/builder.go @@ -264,7 +264,7 @@ func (b *Builder) newRegistry() *RegistryEntry { ctx, b.registryExpiryDuration, nsmgrProxyURL, - DialOptions(WithTokenGenerator(b.generateTokenFunc))..., + memory.WithDialOptions(DialOptions(WithTokenGenerator(b.generateTokenFunc))...), ) serve(ctx, b.t, entry.URL, entry.Register) diff --git a/pkg/tools/sandbox/types.go b/pkg/tools/sandbox/types.go index db7bacb1f..d19c1242a 100644 --- a/pkg/tools/sandbox/types.go +++ b/pkg/tools/sandbox/types.go @@ -29,6 +29,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgrproxy" "github.com/networkservicemesh/sdk/pkg/registry" registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" + "github.com/networkservicemesh/sdk/pkg/registry/chains/memory" "github.com/networkservicemesh/sdk/pkg/registry/common/dnsresolve" "github.com/networkservicemesh/sdk/pkg/tools/token" ) @@ -40,7 +41,7 @@ type SupplyNSMgrProxyFunc func(ctx context.Context, regURL, proxyURL *url.URL, t type SupplyNSMgrFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr // SupplyRegistryFunc supplies Registry -type SupplyRegistryFunc func(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...grpc.DialOption) registry.Registry +type SupplyRegistryFunc func(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...memory.Option) registry.Registry // SupplyRegistryProxyFunc supplies registry proxy type SupplyRegistryProxyFunc func(ctx context.Context, dnsResolver dnsresolve.Resolver, options ...grpc.DialOption) registry.Registry From 9fc854ae7b86b38c1539975164c2064dd2f0cef1 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 15 Sep 2022 15:07:17 +0700 Subject: [PATCH 3/3] add more options to registry memory server Signed-off-by: Nikita Skrynnik --- pkg/registry/chains/memory/server.go | 40 ++++++++++++++++++++-------- pkg/tools/sandbox/builder.go | 4 +-- pkg/tools/sandbox/types.go | 3 +-- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/pkg/registry/chains/memory/server.go b/pkg/registry/chains/memory/server.go index 9fcd6935e..0dd88d303 100644 --- a/pkg/registry/chains/memory/server.go +++ b/pkg/registry/chains/memory/server.go @@ -46,19 +46,14 @@ import ( type serverOptions struct { authorizeNSRegistryServer registry.NetworkServiceRegistryServer authorizeNSERegistryServer registry.NetworkServiceEndpointRegistryServer + expireDuration time.Duration + proxyRegistryURL *url.URL dialOptions []grpc.DialOption } // Option modifies server option value type Option func(o *serverOptions) -// WithDialOptions sets grpc.DialOptions for the client -func WithDialOptions(dialOptions ...grpc.DialOption) Option { - return func(o *serverOptions) { - o.dialOptions = dialOptions - } -} - // WithAuthorizeNSRegistryServer sets authorization NetworkServiceRegistry chain element func WithAuthorizeNSRegistryServer(authorizeNSRegistryServer registry.NetworkServiceRegistryServer) Option { if authorizeNSRegistryServer == nil { @@ -79,11 +74,34 @@ func WithAuthorizeNSERegistryServer(authorizeNSERegistryServer registry.NetworkS } } +// WithExpireDuration sets expire duration for the server +func WithExpireDuration(expireDuration time.Duration) Option { + return func(o *serverOptions) { + o.expireDuration = expireDuration + } +} + +// WithProxyRegistryURL sets URL to reach the proxy registry +func WithProxyRegistryURL(proxyRegistryURL *url.URL) Option { + return func(o *serverOptions) { + o.proxyRegistryURL = proxyRegistryURL + } +} + +// WithDialOptions sets grpc.DialOptions for the server +func WithDialOptions(dialOptions ...grpc.DialOption) Option { + return func(o *serverOptions) { + o.dialOptions = dialOptions + } +} + // NewServer creates new registry server based on memory storage -func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...Option) registryserver.Registry { +func NewServer(ctx context.Context, options ...Option) registryserver.Registry { opts := &serverOptions{ authorizeNSRegistryServer: registryauthorize.NewNetworkServiceRegistryServer(registryauthorize.Any()), authorizeNSERegistryServer: registryauthorize.NewNetworkServiceEndpointRegistryServer(registryauthorize.Any()), + expireDuration: time.Minute, + proxyRegistryURL: nil, } for _, opt := range options { opt(opts) @@ -108,7 +126,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU connect.NewNetworkServiceEndpointRegistryServer( chain.NewNetworkServiceEndpointRegistryClient( begin.NewNetworkServiceEndpointRegistryClient(), - clienturl.NewNetworkServiceEndpointRegistryClient(proxyRegistryURL), + clienturl.NewNetworkServiceEndpointRegistryClient(opts.proxyRegistryURL), clientconn.NewNetworkServiceEndpointRegistryClient(), dial.NewNetworkServiceEndpointRegistryClient(ctx, dial.WithDialOptions(opts.dialOptions...), @@ -122,7 +140,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU Condition: func(c context.Context, nse *registry.NetworkServiceEndpoint) bool { return true }, Action: chain.NewNetworkServiceEndpointRegistryServer( setregistrationtime.NewNetworkServiceEndpointRegistryServer(), - expire.NewNetworkServiceEndpointRegistryServer(ctx, expiryDuration), + expire.NewNetworkServiceEndpointRegistryServer(ctx, opts.expireDuration), memory.NewNetworkServiceEndpointRegistryServer(), ), }, @@ -138,7 +156,7 @@ func NewServer(ctx context.Context, expiryDuration time.Duration, proxyRegistryU }, Action: connect.NewNetworkServiceRegistryServer( chain.NewNetworkServiceRegistryClient( - clienturl.NewNetworkServiceRegistryClient(proxyRegistryURL), + clienturl.NewNetworkServiceRegistryClient(opts.proxyRegistryURL), begin.NewNetworkServiceRegistryClient(), clientconn.NewNetworkServiceRegistryClient(), dial.NewNetworkServiceRegistryClient(ctx, diff --git a/pkg/tools/sandbox/builder.go b/pkg/tools/sandbox/builder.go index 0a8722207..b84eafc93 100644 --- a/pkg/tools/sandbox/builder.go +++ b/pkg/tools/sandbox/builder.go @@ -262,8 +262,8 @@ func (b *Builder) newRegistry() *RegistryEntry { entry.restartableServer = newRestartableServer(b.ctx, b.t, entry.URL, func(ctx context.Context) { entry.Registry = b.supplyRegistry( ctx, - b.registryExpiryDuration, - nsmgrProxyURL, + memory.WithExpireDuration(b.registryExpiryDuration), + memory.WithProxyRegistryURL(nsmgrProxyURL), memory.WithDialOptions(DialOptions(WithTokenGenerator(b.generateTokenFunc))...), ) serve(ctx, b.t, entry.URL, entry.Register) diff --git a/pkg/tools/sandbox/types.go b/pkg/tools/sandbox/types.go index d19c1242a..34d56aa33 100644 --- a/pkg/tools/sandbox/types.go +++ b/pkg/tools/sandbox/types.go @@ -19,7 +19,6 @@ package sandbox import ( "context" "net/url" - "time" registryapi "github.com/networkservicemesh/api/pkg/api/registry" "google.golang.org/grpc" @@ -41,7 +40,7 @@ type SupplyNSMgrProxyFunc func(ctx context.Context, regURL, proxyURL *url.URL, t type SupplyNSMgrFunc func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr // SupplyRegistryFunc supplies Registry -type SupplyRegistryFunc func(ctx context.Context, expiryDuration time.Duration, proxyRegistryURL *url.URL, options ...memory.Option) registry.Registry +type SupplyRegistryFunc func(ctx context.Context, options ...memory.Option) registry.Registry // SupplyRegistryProxyFunc supplies registry proxy type SupplyRegistryProxyFunc func(ctx context.Context, dnsResolver dnsresolve.Resolver, options ...grpc.DialOption) registry.Registry