Skip to content

Commit

Permalink
use dns target for interdomain scenarious
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <[email protected]>
  • Loading branch information
denis-tingaikin committed Dec 25, 2023
1 parent 4d02ed4 commit b55eb3c
Show file tree
Hide file tree
Showing 31 changed files with 1,099 additions and 778 deletions.
49 changes: 49 additions & 0 deletions pkg/networkservice/chains/nsmgr/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ package nsmgr_test
import (
"context"
"net"
"net/url"
"testing"
"time"

"github.com/edwarnicke/genericsync"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

Expand All @@ -55,6 +57,53 @@ func requireIPv4Lookup(ctx context.Context, t *testing.T, r *net.Resolver, host,
require.Equal(t, expected, addrs[0].String())
}

func Test_LocalUsecase_DNSTarget(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

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

domain := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
SetupDefaultDNSServer().
Build()

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

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

nseReg := defaultRegistryEndpoint(nsReg.Name)

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

var addr, _ = url.Parse("dns://" + domain.DNSServer.URL.Host + "/nsmgr-0.cluster.local:" + domain.Nodes[0].NSMgr.URL.Port())
logrus.Error(addr)
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, client.WithClientURL(addr))

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernelmech.MECHANISM},
},
Connection: &networkservice.Connection{
Id: "1",
NetworkService: nsReg.Name,
Labels: make(map[string]string),
},
}

conn, err := nsc.Request(ctx, request)
require.NoError(t, err)

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

_, err = nse.Unregister(ctx, nseReg)
require.NoError(t, err)
}

func Test_DNSUsecase(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

Expand Down
6 changes: 6 additions & 0 deletions pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -520,13 +522,15 @@ func Test_FailedRegistryAuthorization(t *testing.T) {
ctx context.Context,
tokenGenerator token.GeneratorFunc,
expiryDuration time.Duration,
nsmgrProxyURL *url.URL,
proxyRegistryURL *url.URL,
options ...grpc.DialOption) registry.Registry {
registryName := sandbox.UniqueName("registry-memory")

return memory.NewServer(
ctx,
tokenGeneratorFunc("spiffe://test.com/"+registryName),
memory.WithNSMgrProxyURL(nsmgrProxyURL),
memory.WithProxyRegistryURL(proxyRegistryURL),
memory.WithDefaultExpiration(expiryDuration),
memory.WithDialOptions(options...),
Expand Down Expand Up @@ -691,12 +695,14 @@ func Test_Expire(t *testing.T) {
ctx context.Context,
tokenGenerator token.GeneratorFunc,
expiryDuration time.Duration,
nsmgrProxyURL *url.URL,
proxyRegistryURL *url.URL,
options ...grpc.DialOption) registry.Registry {
return memory.NewServer(
ctx,
tokenGenerator,
memory.WithProxyRegistryURL(proxyRegistryURL),
memory.WithNSMgrProxyURL(nsmgrProxyURL),
memory.WithDefaultExpiration(expiryDuration),
memory.WithDialOptions(options...),
memory.WithAuthorizeNSRegistryServer(
Expand Down
55 changes: 23 additions & 32 deletions pkg/networkservice/chains/nsmgr/vl3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,20 +325,17 @@ func Test_Interdomain_vl3_dns(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()

var dnsServer = sandbox.NewFakeResolver()

cluster1 := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetDNSResolver(dnsServer).
SetDNSDomainName("cluster1").
Build()

cluster2 := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetDNSDomainName("cluster2").
SetDNSResolver(dnsServer).
var domains = sandbox.NewInterdomainBuilder(ctx, t).
BuildDomain(func(b *sandbox.Builder) *sandbox.Builder {
return b.SetNodesCount(1).SetName("cluster1")
}).
BuildDomain(func(b *sandbox.Builder) *sandbox.Builder {
return b.SetNodesCount(1).SetName("cluster2")
}).
Build()

cluster1 := domains[0]
cluster2 := domains[1]
nsRegistryClient := cluster2.NewNSRegistryClient(ctx, sandbox.GenerateTestToken)

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService("vl3"))
Expand Down Expand Up @@ -418,27 +415,21 @@ func Test_FloatingInterdomain_vl3_dns(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()

var dnsServer = sandbox.NewFakeResolver()

cluster1 := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetDNSResolver(dnsServer).
SetDNSDomainName("cluster1").
var domains = sandbox.NewInterdomainBuilder(ctx, t).
BuildDomain(func(b *sandbox.Builder) *sandbox.Builder {
return b.SetNodesCount(1).SetName("cluster1")
}).
BuildDomain(func(b *sandbox.Builder) *sandbox.Builder {
return b.SetNodesCount(1).SetName("cluster2")
}).
BuildDomain(func(b *sandbox.Builder) *sandbox.Builder {
return b.SetNodesCount(0).SetNSMgrProxySupplier(nil).SetRegistryProxySupplier(nil).SetName("floating")
}).
Build()

cluster2 := sandbox.NewBuilder(ctx, t).
SetNodesCount(1).
SetDNSDomainName("cluster2").
SetDNSResolver(dnsServer).
Build()

floating := sandbox.NewBuilder(ctx, t).
SetNodesCount(0).
SetDNSDomainName("floating.domain").
SetDNSResolver(dnsServer).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
Build()
cluster1 := domains[0]
cluster2 := domains[1]
floating := domains[2]

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

Expand Down Expand Up @@ -492,7 +483,7 @@ func Test_FloatingInterdomain_vl3_dns(t *testing.T) {
req.Connection = resp.Clone()
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs(), 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps, 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].SearchDomains, 3)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].SearchDomains, 2)

searchDomain := resp.GetContext().GetDnsContext().GetConfigs()[0].SearchDomains[0]

Expand Down
114 changes: 51 additions & 63 deletions pkg/networkservice/chains/nsmgrproxy/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,49 +33,43 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clusterinfo"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/connect"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/discover"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/interdomainbypass"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/swapip"
"github.com/networkservicemesh/sdk/pkg/registry"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/switchcase"
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"
registryclusterinfo "github.com/networkservicemesh/sdk/pkg/registry/common/clusterinfo"
registryconnect "github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
registryswapip "github.com/networkservicemesh/sdk/pkg/registry/common/swapip"
"github.com/networkservicemesh/sdk/pkg/registry/common/updatepath"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
"github.com/networkservicemesh/sdk/pkg/tools/clienturlctx"
"github.com/networkservicemesh/sdk/pkg/tools/fs"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/interdomain"
"github.com/networkservicemesh/sdk/pkg/tools/log"
authmonitor "github.com/networkservicemesh/sdk/pkg/tools/monitorconnection/authorize"
"github.com/networkservicemesh/sdk/pkg/tools/token"
)

func (n *nsmgrProxyServer) Register(s *grpc.Server) {
grpcutils.RegisterHealthServices(s, n, n.NetworkServiceEndpointRegistryServer(), n.NetworkServiceRegistryServer())
grpcutils.RegisterHealthServices(s, n)
networkservice.RegisterNetworkServiceServer(s, n)
networkservice.RegisterMonitorConnectionServer(s, n)
registryapi.RegisterNetworkServiceRegistryServer(s, n.Registry.NetworkServiceRegistryServer())
registryapi.RegisterNetworkServiceEndpointRegistryServer(s, n.Registry.NetworkServiceEndpointRegistryServer())
}

type nsmgrProxyServer struct {
endpoint.Endpoint
registry.Registry
}

type serverOptions struct {
name string
mapipFilePath string
listenOn *url.URL
listenOn, registryURL *url.URL
authorizeServer networkservice.NetworkServiceServer
authorizeMonitorConnectionServer networkservice.MonitorConnectionServer
authorizeNSRegistryServer registryapi.NetworkServiceRegistryServer
Expand Down Expand Up @@ -204,8 +200,15 @@ func WithDialTimeout(dialTimeout time.Duration) Option {
}
}

// WithRegistryURL sets URL to the registry
func WithRegistryURL(u *url.URL) Option {
return func(o *serverOptions) {
o.registryURL = u
}
}

// NewServer creates new proxy NSMgr
func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator token.GeneratorFunc, options ...Option) nsmgr.Nsmgr {
func NewServer(ctx context.Context, regURL *url.URL, tokenGenerator token.GeneratorFunc, options ...Option) endpoint.Endpoint {
rv := new(nsmgrProxyServer)
opts := &serverOptions{
name: "nsmgr-proxy-" + uuid.New().String(),
Expand All @@ -222,11 +225,8 @@ func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator to
opt(opts)
}

var interdomainBypassNSEServer registryapi.NetworkServiceEndpointRegistryServer

nseClient := chain.NewNetworkServiceEndpointRegistryClient(
begin.NewNetworkServiceEndpointRegistryClient(),
clienturl.NewNetworkServiceEndpointRegistryClient(regURL),
clientconn.NewNetworkServiceEndpointRegistryClient(),
dial.NewNetworkServiceEndpointRegistryClient(ctx,
dial.WithDialOptions(opts.dialOptions...),
Expand All @@ -235,23 +235,56 @@ func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator to
registryconnect.NewNetworkServiceEndpointRegistryClient(),
)

regNSEClient := chain.NewNetworkServiceEndpointRegistryClient(
clienturl.NewNetworkServiceEndpointRegistryClient(opts.registryURL),
nseClient,
)

proxyRegNSEClient := chain.NewNetworkServiceEndpointRegistryClient(
clienturl.NewNetworkServiceEndpointRegistryClient(regURL),
nseClient,
)

nsClient := chain.NewNetworkServiceRegistryClient(
begin.NewNetworkServiceRegistryClient(),
clienturl.NewNetworkServiceRegistryClient(regURL),
clientconn.NewNetworkServiceRegistryClient(),
dial.NewNetworkServiceRegistryClient(ctx,
dial.WithDialOptions(opts.dialOptions...),
),
registryconnect.NewNetworkServiceRegistryClient(),
)

regNSClient := chain.NewNetworkServiceRegistryClient(
clienturl.NewNetworkServiceRegistryClient(opts.registryURL),
nsClient,
)

proxyRegNSClient := chain.NewNetworkServiceRegistryClient(
clienturl.NewNetworkServiceRegistryClient(regURL),
nsClient,
)

rv.Endpoint = endpoint.NewServer(ctx, tokenGenerator,
endpoint.WithName(opts.name),
endpoint.WithAuthorizeServer(opts.authorizeServer),
endpoint.WithAuthorizeMonitorConnectionServer(opts.authorizeMonitorConnectionServer),
endpoint.WithAdditionalFunctionality(
interdomainbypass.NewServer(&interdomainBypassNSEServer, opts.listenOn),
discover.NewServer(nsClient, nseClient),
switchcase.NewServer(&switchcase.ServerCase{
Condition: func(ctx context.Context, c *networkservice.Connection) bool {
return interdomain.Is(c.GetNetworkServiceEndpointName())
},
Server: discover.NewServer(proxyRegNSClient, proxyRegNSEClient),
}, &switchcase.ServerCase{
Condition: switchcase.Default,
Server: discover.NewServer(regNSClient, regNSEClient),
}),
switchcase.NewServer(&switchcase.ServerCase{
Condition: func(ctx context.Context, c *networkservice.Connection) bool {
var u = clienturlctx.ClientURL(ctx)
return u != nil && u.Scheme != "tcp" && c.GetPath().GetIndex() < 4
},
Server: interdomainbypass.NewServer(),
}),
swapip.NewServer(opts.openMapIPChannel(ctx)),
clusterinfo.NewServer(),
connect.NewServer(
Expand All @@ -269,50 +302,5 @@ func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator to
),
)

var nsServerChain = registryconnect.NewNetworkServiceRegistryServer(
chain.NewNetworkServiceRegistryClient(
begin.NewNetworkServiceRegistryClient(),
clienturl.NewNetworkServiceRegistryClient(proxyURL),
clientconn.NewNetworkServiceRegistryClient(),
opts.authorizeNSRegistryClient,
grpcmetadata.NewNetworkServiceRegistryClient(),
dial.NewNetworkServiceRegistryClient(ctx,
dial.WithDialOptions(opts.dialOptions...),
),
registryconnect.NewNetworkServiceRegistryClient(),
),
)

nsServerChain = chain.NewNetworkServiceRegistryServer(
grpcmetadata.NewNetworkServiceRegistryServer(),
updatepath.NewNetworkServiceRegistryServer(tokenGenerator),
opts.authorizeNSRegistryServer,
nsServerChain,
)

var nseServerChain = chain.NewNetworkServiceEndpointRegistryServer(
grpcmetadata.NewNetworkServiceEndpointRegistryServer(),
updatepath.NewNetworkServiceEndpointRegistryServer(tokenGenerator),
opts.authorizeNSERegistryServer,
begin.NewNetworkServiceEndpointRegistryServer(),
clienturl.NewNetworkServiceEndpointRegistryServer(proxyURL),
interdomainBypassNSEServer,
registryswapip.NewNetworkServiceEndpointRegistryServer(opts.openMapIPChannel(ctx)),
registryclusterinfo.NewNetworkServiceEndpointRegistryServer(),
registryconnect.NewNetworkServiceEndpointRegistryServer(
chain.NewNetworkServiceEndpointRegistryClient(
clientconn.NewNetworkServiceEndpointRegistryClient(),
opts.authorizeNSERegistryClient,
grpcmetadata.NewNetworkServiceEndpointRegistryClient(),
dial.NewNetworkServiceEndpointRegistryClient(ctx,
dial.WithDialOptions(opts.dialOptions...),
dial.WithDialTimeout(opts.dialTimeout),
),
registryconnect.NewNetworkServiceEndpointRegistryClient(),
),
),
)

rv.Registry = registry.NewServer(nsServerChain, nseServerChain)
return rv
}
Loading

0 comments on commit b55eb3c

Please sign in to comment.