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

Add dnsresolve client chain elements #1275

Merged
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
4 changes: 3 additions & 1 deletion pkg/networkservice/chains/nsmgr/heal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ func testNSMGRCloseHeal(t *testing.T, withNSEExpiration bool) {
if withNSEExpiration {
// 3.1 Wait for the endpoint expiration
time.Sleep(time.Second)
c := client.NewNetworkServiceEndpointRegistryClient(ctx, domain.Nodes[0].NSMgr.URL, client.WithDialOptions(sandbox.DialOptions(sandbox.WithTokenGenerator(sandbox.GenerateTestToken))...))
c := client.NewNetworkServiceEndpointRegistryClient(ctx,
client.WithClientURL(domain.Nodes[0].NSMgr.URL),
client.WithDialOptions(sandbox.DialOptions(sandbox.WithTokenGenerator(sandbox.GenerateTestToken))...))

stream, err := c.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Expand Down
3 changes: 2 additions & 1 deletion pkg/networkservice/chains/nsmgr/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func (s *nsmgrSuite) Test_SelectsRestartingEndpointUsecase() {
})
require.NoError(t, err)

nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, sandbox.CloneURL(s.domain.Nodes[0].NSMgr.URL),
nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx,
registryclient.WithClientURL(sandbox.CloneURL(s.domain.Nodes[0].NSMgr.URL)),
registryclient.WithDialOptions(sandbox.DialOptions()...))

nseReg, err = nseRegistryClient.Register(ctx, nseReg)
Expand Down
11 changes: 6 additions & 5 deletions pkg/registry/chains/client/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ package client

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

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/common/begin"
"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
"github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
"github.com/networkservicemesh/sdk/pkg/registry/common/heal"
"github.com/networkservicemesh/sdk/pkg/registry/common/null"
"github.com/networkservicemesh/sdk/pkg/registry/common/retry"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
)

// NewNetworkServiceRegistryClient creates a new NewNetworkServiceRegistryClient that can be used for NS registration.
func NewNetworkServiceRegistryClient(ctx context.Context, connectTo *url.URL, opts ...Option) registry.NetworkServiceRegistryClient {
clientOpts := new(clientOptions)
func NewNetworkServiceRegistryClient(ctx context.Context, opts ...Option) registry.NetworkServiceRegistryClient {
clientOpts := &clientOptions{
nsClientURLResolver: null.NewNetworkServiceRegistryClient(),
}
for _, opt := range opts {
opt(clientOpts)
}
Expand All @@ -46,7 +47,7 @@ func NewNetworkServiceRegistryClient(ctx context.Context, connectTo *url.URL, op
begin.NewNetworkServiceRegistryClient(),
retry.NewNetworkServiceRegistryClient(ctx),
heal.NewNetworkServiceRegistryClient(ctx),
clienturl.NewNetworkServiceRegistryClient(connectTo),
clientOpts.nsClientURLResolver,
clientconn.NewNetworkServiceRegistryClient(),
dial.NewNetworkServiceRegistryClient(ctx,
dial.WithDialOptions(clientOpts.dialOptions...),
Expand Down
11 changes: 6 additions & 5 deletions pkg/registry/chains/client/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ package client

import (
"context"
"net/url"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/common/begin"
"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
"github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
"github.com/networkservicemesh/sdk/pkg/registry/common/heal"
"github.com/networkservicemesh/sdk/pkg/registry/common/null"
"github.com/networkservicemesh/sdk/pkg/registry/common/refresh"
"github.com/networkservicemesh/sdk/pkg/registry/common/retry"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
)

// NewNetworkServiceEndpointRegistryClient creates a new NewNetworkServiceEndpointRegistryClient that can be used for NSE registration.
func NewNetworkServiceEndpointRegistryClient(ctx context.Context, connectTo *url.URL, opts ...Option) registry.NetworkServiceEndpointRegistryClient {
clientOpts := new(clientOptions)
func NewNetworkServiceEndpointRegistryClient(ctx context.Context, opts ...Option) registry.NetworkServiceEndpointRegistryClient {
clientOpts := &clientOptions{
nseClientURLResolver: null.NewNetworkServiceEndpointRegistryClient(),
}
for _, opt := range opts {
opt(clientOpts)
}
Expand All @@ -48,7 +49,7 @@ func NewNetworkServiceEndpointRegistryClient(ctx context.Context, connectTo *url
retry.NewNetworkServiceEndpointRegistryClient(ctx),
heal.NewNetworkServiceEndpointRegistryClient(ctx),
refresh.NewNetworkServiceEndpointRegistryClient(ctx),
clienturl.NewNetworkServiceEndpointRegistryClient(connectTo),
clientOpts.nseClientURLResolver,
clientconn.NewNetworkServiceEndpointRegistryClient(),
dial.NewNetworkServiceEndpointRegistryClient(ctx,
dial.WithDialOptions(clientOpts.dialOptions...),
Expand Down
33 changes: 31 additions & 2 deletions pkg/registry/chains/client/option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -17,13 +17,40 @@
package client

import (
"github.com/networkservicemesh/api/pkg/api/registry"
"net/url"

"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
)

// Option is an option pattern for NewNetworkServiceRegistryClient, NewNetworkServiceEndpointRegistryClient
type Option func(clientOpts *clientOptions)

// WithClientURL sets client URL
func WithClientURL(u *url.URL) Option {
return func(clientOpts *clientOptions) {
clientOpts.nsClientURLResolver = clienturl.NewNetworkServiceRegistryClient(u)
clientOpts.nseClientURLResolver = clienturl.NewNetworkServiceEndpointRegistryClient(u)
}
}

// WithNSClientURLResolver sets ns client URL resolver
func WithNSClientURLResolver(c registry.NetworkServiceRegistryClient) Option {
return func(clientOpts *clientOptions) {
clientOpts.nsClientURLResolver = c
}
}

// WithNSEClientURLResolver sets nse client URL resolver
func WithNSEClientURLResolver(c registry.NetworkServiceEndpointRegistryClient) Option {
return func(clientOpts *clientOptions) {
clientOpts.nseClientURLResolver = c
}
}

// WithNSAdditionalFunctionality sets additional functionality
func WithNSAdditionalFunctionality(additionalFunctionality ...registry.NetworkServiceRegistryClient) Option {
return func(clientOpts *clientOptions) {
Expand All @@ -46,6 +73,8 @@ func WithDialOptions(dialOptions ...grpc.DialOption) Option {
}

type clientOptions struct {
nsClientURLResolver registry.NetworkServiceRegistryClient
nseClientURLResolver registry.NetworkServiceEndpointRegistryClient
nsAdditionalFunctionality []registry.NetworkServiceRegistryClient
nseAdditionalFunctionality []registry.NetworkServiceEndpointRegistryClient
dialOptions []grpc.DialOption
Expand Down
127 changes: 127 additions & 0 deletions pkg/registry/common/dnsresolve/ns_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) 2022 Cisco Systems, Inc.
//
// 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.

package dnsresolve

import (
"context"
"errors"
"net"

"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"

"github.com/networkservicemesh/sdk/pkg/tools/clienturlctx"
"github.com/networkservicemesh/sdk/pkg/tools/interdomain"
)

type dnsNSResolveClient struct {
resolver Resolver
registryService string
}

// NewNetworkServiceRegistryClient creates new NetworkServiceRegistryClient that can resolve passed domain to clienturl
func NewNetworkServiceRegistryClient(opts ...Option) registry.NetworkServiceRegistryClient {
var clientOptions = &options{
resolver: net.DefaultResolver,
registryService: DefaultRegistryService,
}

for _, opt := range opts {
opt(clientOptions)
}

r := &dnsNSResolveClient{
resolver: clientOptions.resolver,
registryService: clientOptions.registryService,
}

return r
}

func (d *dnsNSResolveClient) Register(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) {
domain := interdomain.Domain(ns.Name)
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
if err != nil {
return nil, err
}
ctx = clienturlctx.WithClientURL(ctx, url)
ns.Name = interdomain.Target(ns.Name)
resp, err := next.NetworkServiceRegistryClient(ctx).Register(ctx, ns, opts...)
if err != nil {
return nil, err
}

resp.Name = interdomain.Join(resp.Name, domain)

return resp, err
}

type dnsNSResolveFindClient struct {
registry.NetworkServiceRegistry_FindClient
domain string
}

func (c *dnsNSResolveFindClient) Recv() (*registry.NetworkServiceResponse, error) {
resp, err := c.NetworkServiceRegistry_FindClient.Recv()
if err != nil {
return resp, err
}
resp.NetworkService.Name = interdomain.Join(resp.NetworkService.Name, c.domain)

return resp, err
}

func (d *dnsNSResolveClient) Find(ctx context.Context, q *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) {
domain := interdomain.Domain(q.NetworkService.Name)
if domain == "" {
return nil, errors.New("domain cannot be empty")
}
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
if err != nil {
return nil, err
}
ctx = clienturlctx.WithClientURL(ctx, url)
q.NetworkService.Name = interdomain.Target(q.NetworkService.Name)

resp, err := next.NetworkServiceRegistryClient(ctx).Find(ctx, q, opts...)
if err != nil {
return nil, err
}

return &dnsNSResolveFindClient{
NetworkServiceRegistry_FindClient: resp,
domain: domain,
}, nil
}

func (d *dnsNSResolveClient) Unregister(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) {
domain := interdomain.Domain(ns.Name)
url, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
if err != nil {
return nil, err
}
ctx = clienturlctx.WithClientURL(ctx, url)
ns.Name = interdomain.Target(ns.Name)
defer func() {
ns.Name = interdomain.Join(ns.Name, domain)
}()
return next.NetworkServiceRegistryClient(ctx).Unregister(ctx, ns, opts...)
}
3 changes: 3 additions & 0 deletions pkg/registry/common/dnsresolve/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (d *dnsNSResolveServer) Register(ctx context.Context, ns *registry.NetworkS
ctx = clienturlctx.WithClientURL(ctx, url)
ns.Name = interdomain.Target(ns.Name)
resp, err := next.NetworkServiceRegistryServer(ctx).Register(ctx, ns)
if err != nil {
return nil, err
}

resp.Name = interdomain.Join(resp.Name, domain)

Expand Down
Loading