Skip to content

Commit

Permalink
Add dnsresolve client chain elements
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art committed May 4, 2022
1 parent 241ef75 commit 24b6529
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 20 deletions.
5 changes: 4 additions & 1 deletion pkg/networkservice/chains/nsmgr/heal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/null"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/registry/chains/client"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

Expand Down Expand Up @@ -413,7 +414,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.WithNSEClientURLResolver(clienturl.NewNetworkServiceEndpointRegistryClient(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
4 changes: 3 additions & 1 deletion pkg/networkservice/chains/nsmgr/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/count"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client"
registryclienturl "github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

Expand Down Expand Up @@ -161,7 +162,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.WithNSEClientURLResolver(registryclienturl.NewNetworkServiceEndpointRegistryClient(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
16 changes: 16 additions & 0 deletions pkg/registry/chains/client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ import (
// Option is an option pattern for NewNetworkServiceRegistryClient, NewNetworkServiceEndpointRegistryClient
type Option func(clientOpts *clientOptions)

// 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 +60,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
124 changes: 124 additions & 0 deletions pkg/registry/common/dnsresolve/ns_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// 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...)

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...)
}
Loading

0 comments on commit 24b6529

Please sign in to comment.