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

qfix: DNSNSEResolve server should correctly translate NSE for floating side and for querying side #1027

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
83 changes: 83 additions & 0 deletions pkg/networkservice/chains/nsmgrproxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,89 @@ func TestNSMGR_FloatingInterdomainUseCase(t *testing.T) {
require.NoError(t, err)
}

// TestNSMGR_FloatingInterdomainUseCase_FloatingNetworkServiceNameRegistration covers simple interdomain scenario with resolving endpoint from floating registry:
//
// registration: {"name": "nse@floating.domain", "networkservice_names": ["my-service-interdomain@floating.domain"]}
//
// nsc -> nsmgr1 -> forwarder1 -> nsmgr1 -> nsmgr-proxy1 -> nsmg-proxy2 -> nsmgr2 ->forwarder2 -> nsmgr2 -> nse3
func TestNSMGR_FloatingInterdomainUseCase_FloatingNetworkServiceNameRegistration(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

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

var dnsServer = new(sandbox.FakeDNSResolver)

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 := &registry.NetworkService{
Name: "my-service-interdomain@" + floating.Name,
}

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

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

cluster2.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)

nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken)

request := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
{Cls: cls.LOCAL, Type: kernel.MECHANISM},
},
Connection: &networkservice.Connection{
Id: "1",
NetworkService: fmt.Sprint(nsReg.Name),
Context: &networkservice.ConnectionContext{},
},
}

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

require.Equal(t, 10, len(conn.Path.PathSegments))

// Simulate refresh from client.

refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

conn, err = nsc.Request(ctx, refreshRequest)
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, 10, len(conn.Path.PathSegments))

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

// TestNSMGR_FloatingInterdomain_FourClusters covers scenarion with connection from the one client to two endpoints
// from diffrenret clusters using floating registry for resolving endpoints.
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/common/dnsresolve/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resolveDomain(ctx context.Context, service, domain string, r Resolver) (*ur
return nil, err
}

log.FromContext(ctx).Debug("Resolved url: %v", u)
log.FromContext(ctx).Debugf("Resolved url: %v", u)
return u, nil
}

Expand Down
124 changes: 86 additions & 38 deletions pkg/registry/common/dnsresolve/nse_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package dnsresolve

import (
"context"
"errors"
"net"
"net/url"

Expand Down Expand Up @@ -61,75 +60,124 @@ func NewNetworkServiceEndpointRegistryServer(opts ...Option) registry.NetworkSer
return r
}

func (d *dnsNSEResolveServer) Register(ctx context.Context, ns *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
domain := interdomain.Domain(ns.Name)
u, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
func translateNSE(nse *registry.NetworkServiceEndpoint, translator func(string) string) {
nse.Name = translator(nse.Name)

for i := 0; i < len(nse.GetNetworkServiceNames()); i++ {
var service = nse.GetNetworkServiceNames()[i]
var target = translator(service)

nse.GetNetworkServiceNames()[i] = target

if nse.GetNetworkServiceNames() == nil {
continue
}

var labels = nse.GetNetworkServiceLabels()[service]

if labels == nil {
continue
}

delete(nse.GetNetworkServiceLabels(), service)

nse.GetNetworkServiceLabels()[target] = labels
}
}

// TODO: consider to return error if NSE is not consistent and have multi domains target.
func resolveNSE(nse *registry.NetworkServiceEndpoint) string {
var domain string

for _, name := range append([]string{nse.Name}, nse.GetNetworkServiceNames()...) {
if interdomain.Is(name) {
domain = interdomain.Domain(name)
break
}
}

return domain
}

func (d *dnsNSEResolveServer) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
var domain = resolveNSE(nse)
var u, err = resolveDomain(ctx, d.registryService, domain, d.resolver)

if err != nil {
return nil, err
}

ctx = clienturlctx.WithClientURL(ctx, u)
ns.Name = interdomain.Target(ns.Name)
return next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, ns)

translateNSE(nse, interdomain.Target)

resp, err := next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, nse)

if err != nil {
return nil, err
}

translateNSE(nse, func(s string) string {
return interdomain.Join(s, domain)
})

return resp, err
}

type dnsFindNSEServer struct {
domain string
u *url.URL
nseURL *url.URL
registry.NetworkServiceEndpointRegistry_FindServer
}

func (s *dnsFindNSEServer) Send(nse *registry.NetworkServiceEndpoint) error {
nse.Name = interdomain.Join(nse.Name, s.domain)
if s.u != nil {
nse.Url = s.u.String()
translateNSE(nse, func(str string) string {
return interdomain.Join(str, s.domain)
})

if s.nseURL != nil {
nse.Url = s.nseURL.String()
}

return s.NetworkServiceEndpointRegistry_FindServer.Send(nse)
}

func (d *dnsNSEResolveServer) Find(q *registry.NetworkServiceEndpointQuery, s registry.NetworkServiceEndpointRegistry_FindServer) error {
ctx := s.Context()
domain := domainOf(q.NetworkServiceEndpoint)
if domain == "" {
return errors.New("domain cannot be empty")
}
q.NetworkServiceEndpoint.Name = interdomain.Target(q.NetworkServiceEndpoint.Name)
u, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
var ctx = s.Context()
var domain = resolveNSE(q.NetworkServiceEndpoint)
var nsmgrProxyURL, err = resolveDomain(ctx, d.registryService, domain, d.resolver)

if err != nil {
return err
}
ctx = clienturlctx.WithClientURL(s.Context(), u)
u, err = resolveDomain(ctx, d.nsmgrProxyService, domain, d.resolver)

ctx = clienturlctx.WithClientURL(s.Context(), nsmgrProxyURL)
nsmgrProxyURL, err = resolveDomain(ctx, d.nsmgrProxyService, domain, d.resolver)

if err != nil {
log.FromContext(ctx).Errorf("nsmgrProxyService is not reachable by domain: %v", domain)
}

s = streamcontext.NetworkServiceEndpointRegistryFindServer(ctx, s)
return next.NetworkServiceEndpointRegistryServer(s.Context()).Find(q, &dnsFindNSEServer{NetworkServiceEndpointRegistry_FindServer: s, domain: domain, u: u})

translateNSE(q.NetworkServiceEndpoint, interdomain.Target)

return next.NetworkServiceEndpointRegistryServer(s.Context()).Find(q, &dnsFindNSEServer{NetworkServiceEndpointRegistry_FindServer: s, domain: domain, nseURL: nsmgrProxyURL})
}

func (d *dnsNSEResolveServer) Unregister(ctx context.Context, ns *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
domain := interdomain.Domain(ns.Name)
u, err := resolveDomain(ctx, d.registryService, domain, d.resolver)
func (d *dnsNSEResolveServer) Unregister(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
var domain = resolveNSE(nse)
var u, err = resolveDomain(ctx, d.registryService, domain, d.resolver)

if err != nil {
return nil, err
}

ctx = clienturlctx.WithClientURL(ctx, u)
ns.Name = interdomain.Target(ns.Name)
return next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, ns)
}

func domainOf(nse *registry.NetworkServiceEndpoint) string {
if nse.Name != "" {
return interdomain.Domain(nse.Name)
}
translateNSE(nse, interdomain.Target)

for i, ns := range nse.NetworkServiceNames {
if interdomain.Is(ns) {
result := interdomain.Domain(ns)
nse.NetworkServiceNames[i] = interdomain.Target(ns)
return result
}
}
return ""
return next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, nse)
}

func (d *dnsNSEResolveServer) setResolver(r Resolver) {
Expand Down
22 changes: 22 additions & 0 deletions pkg/registry/common/dnsresolve/nse_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,30 @@ func Test_DNSResolve(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

resp, err := s.Register(ctx, &registry.NetworkServiceEndpoint{Name: "nse-1@domain1"})
require.NoError(t, err)
require.Equal(t, "nse-1@domain1", resp.Name)

resp, err = s.Register(ctx, &registry.NetworkServiceEndpoint{
Name: "nse-1",
NetworkServiceNames: []string{"ns1@domain1"},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns1@domain1": {
Labels: map[string]string{
"app": "myapp",
},
},
},
})
require.NoError(t, err)
require.Equal(t, "nse-1@domain1", resp.Name)
require.Len(t, resp.GetNetworkServiceLabels(), 1)
require.NotNil(t, resp.GetNetworkServiceLabels()["ns1@domain1"])
require.Equal(t, "myapp", resp.GetNetworkServiceLabels()["ns1@domain1"].Labels["app"])

_, err = s.Register(ctx, &registry.NetworkServiceEndpoint{Name: "nse-1@domain1"})
require.NoError(t, err)

err = s.Find(&registry.NetworkServiceEndpointQuery{NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{Name: "nse-1@domain1"}}, streamchannel.NewNetworkServiceEndpointFindServer((ctx), nil))
require.NoError(t, err)
_, err = s.Unregister(ctx, &registry.NetworkServiceEndpoint{Name: "nse-1@domain1"})
Expand Down