Skip to content

Commit

Permalink
Add interdomain funcs to vl3-dns template
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 Feb 20, 2023
1 parent ff8f08b commit 6624d46
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 4 deletions.
194 changes: 192 additions & 2 deletions pkg/networkservice/chains/nsmgr/vl3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@ import (

"github.com/edwarnicke/genericsync"
"github.com/google/uuid"
"github.com/networkservicemesh/api/pkg/api/ipam"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/api/pkg/api/ipam"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/upstreamrefresh"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkconnection"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/memory"
"github.com/networkservicemesh/sdk/pkg/tools/interdomain"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
)

Expand Down Expand Up @@ -290,3 +296,187 @@ func Test_NSC_GetsVl3DnsAddressAfterRefresh(t *testing.T) {
}
require.NoError(t, ctx.Err())
}

func Test_Interdomain_vl3_dns(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(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).
Build()

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

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

nseReg := &registry.NetworkServiceEndpoint{
Name: "final-endpoint",
NetworkServiceNames: []string{nsReg.Name},
}

var serverPrefixCh = make(chan *ipam.PrefixResponse, 1)
defer close(serverPrefixCh)

serverPrefixCh <- &ipam.PrefixResponse{Prefix: "10.0.0.1/24"}
dnsServerIPCh := make(chan net.IP, 1)
dnsServerIPCh <- net.ParseIP("127.0.0.1")

cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken,
vl3.NewServer(ctx, serverPrefixCh),
vl3dns.NewServer(ctx,
dnsServerIPCh,
vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ target .NetworkService }}.{{ domain .NetworkService }}."),
vl3dns.WithDNSPort(40053)),
checkrequest.NewServer(t, func(t *testing.T, nsr *networkservice.NetworkServiceRequest) {
require.False(t, interdomain.Is(nsr.GetConnection().GetNetworkService()))
},
),
)

resolver := net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
var dialer net.Dialer
return dialer.DialContext(ctx, network, "127.0.0.1:40053")
},
}

nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)
req := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernel.MECHANISM},
},
Connection: &networkservice.Connection{
Id: uuid.New().String(),
NetworkService: fmt.Sprint(nsReg.Name, "@", cluster2.Name),
Labels: map[string]string{"podName": nscName},
},
}

resp, err := nsc.Request(ctx, req)
require.NoError(t, err)

req.Connection = resp.Clone()
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs(), 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps, 1)

requireIPv4Lookup(ctx, t, &resolver, nscName+".vl3", "10.0.0.1")

resp, err = nsc.Request(ctx, req)
require.NoError(t, err)

requireIPv4Lookup(ctx, t, &resolver, nscName+".vl3", "10.0.0.1")

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

_, err = resolver.LookupIP(ctx, "ip4", nscName+".vl3")
require.Error(t, err)
}

func Test_FloatingInterdomain_vl3_dns(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(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).
Build()

floating := sandbox.NewBuilder(ctx, t).
SetNodesCount(0).
SetDNSDomainName("floating.domain").
SetDNSResolver(dnsServer).
SetNSMgrProxySupplier(nil).
SetRegistryProxySupplier(nil).
Build()

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

nsReg, err := nsRegistryClient.Register(ctx, defaultRegistryService("vl3@"+floating.Name))
require.NoError(t, err)

nseReg := &registry.NetworkServiceEndpoint{
Name: "final-endpoint@" + floating.Name,
NetworkServiceNames: []string{"vl3"},
}

var serverPrefixCh = make(chan *ipam.PrefixResponse, 1)
defer close(serverPrefixCh)

serverPrefixCh <- &ipam.PrefixResponse{Prefix: "10.0.0.1/24"}
dnsServerIPCh := make(chan net.IP, 1)
dnsServerIPCh <- net.ParseIP("127.0.0.1")

cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken,
vl3.NewServer(ctx, serverPrefixCh),
vl3dns.NewServer(ctx,
dnsServerIPCh,
vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ target .NetworkService }}.{{ domain .NetworkService }}."),
vl3dns.WithDNSPort(40053)),
)

resolver := net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
var dialer net.Dialer
return dialer.DialContext(ctx, network, "127.0.0.1:40053")
},
}

nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)
req := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernel.MECHANISM},
},
Connection: &networkservice.Connection{
Id: uuid.New().String(),
NetworkService: fmt.Sprint(nsReg.Name),
Labels: map[string]string{"podName": nscName},
},
}

resp, err := nsc.Request(ctx, req)
require.NoError(t, err)

req.Connection = resp.Clone()
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs(), 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps, 1)

requireIPv4Lookup(ctx, t, &resolver, nscName+".vl3."+floating.Name, "10.0.0.1")

resp, err = nsc.Request(ctx, req)
require.NoError(t, err)

requireIPv4Lookup(ctx, t, &resolver, nscName+".vl3."+floating.Name, "10.0.0.1")

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

_, err = resolver.LookupIP(ctx, "ip4", nscName+".vl3."+floating.Name)
require.Error(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"

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

// Option configures vl3DNSServer
Expand All @@ -50,7 +51,13 @@ func WithDomainSchemes(domainSchemes ...string) Option {
return func(vd *vl3DNSServer) {
vd.domainSchemeTemplates = nil
for i, domainScheme := range domainSchemes {
vd.domainSchemeTemplates = append(vd.domainSchemeTemplates, template.Must(template.New(fmt.Sprintf("dnsScheme%d", i)).Parse(domainScheme)))
vd.domainSchemeTemplates = append(vd.domainSchemeTemplates,
template.Must(template.New(fmt.Sprintf("dnsScheme%d", i)).
Funcs(template.FuncMap{
"target": interdomain.Target,
"domain": interdomain.Domain,
}).
Parse(domainScheme)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"net"
"regexp"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -190,7 +191,7 @@ func (n *vl3DNSServer) buildSrcDNSRecords(c *networkservice.Connection) ([]strin
if err := templ.Execute(recordBuilder, c); err != nil {
return nil, errors.Wrap(err, "error occurred executing the template or writing its output")
}
result = append(result, recordBuilder.String())
result = append(result, removeDupDots(recordBuilder.String()))
}
return result, nil
}
Expand Down Expand Up @@ -257,3 +258,9 @@ func getSrcIPs(c *networkservice.Connection) []net.IP {
}
return ips
}

var regexDot = regexp.MustCompile(`\.+`)

func removeDupDots(str string) string {
return regexDot.ReplaceAllString(str, ".")
}

0 comments on commit 6624d46

Please sign in to comment.