diff --git a/pkg/networkservice/chains/nsmgrproxy/server_test.go b/pkg/networkservice/chains/nsmgrproxy/server_test.go index fe23972919..35a784f40c 100644 --- a/pkg/networkservice/chains/nsmgrproxy/server_test.go +++ b/pkg/networkservice/chains/nsmgrproxy/server_test.go @@ -115,6 +115,113 @@ func TestNSMGR_InterdomainUseCase(t *testing.T) { require.NoError(t, err) } +// TestNSMGR_InterdomainUseCase covers simple interdomain scenario: +// +// request1: nsc -> nsmgr1 -> forwarder1 -> nsmgr1 -> nsmgr-proxy1 -> nsmg-proxy2 -> nsmgr2 ->forwarder2 -> nsmgr2 -> final-endpoint via cluster2 +// request2: nsc -> nsmgr1 -> forwarder1 -> nsmgr1 -> nsmgr-proxy1 -> nsmg-proxy2 -> nsmgr2 ->forwarder2 -> nsmgr2 -> final-endpoint via floating registry +func Test_NSEMovedFromInterdomainToFloatingUseCase(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) + + nsReg1 := ®istry.NetworkService{ + Name: "my-service-interdomain", + } + + var err error + + nsReg1, err = nsRegistryClient.Register(ctx, nsReg1) + require.NoError(t, err) + + nsReg2 := ®istry.NetworkService{ + Name: "my-service-interdomain@" + floating.Name, + } + + nsReg2, err = nsRegistryClient.Register(ctx, nsReg2) + require.NoError(t, err) + + nseReg1 := ®istry.NetworkServiceEndpoint{ + Name: "final-endpoint", + NetworkServiceNames: []string{nsReg1.Name}, + } + + cluster2.Nodes[0].NewEndpoint(ctx, nseReg1, sandbox.GenerateTestToken) + + nseReg2 := ®istry.NetworkServiceEndpoint{ + Name: "final-endpoint@" + floating.Name, + NetworkServiceNames: []string{nsReg1.Name}, + } + + cluster2.Nodes[0].NewEndpoint(ctx, nseReg2, sandbox.GenerateTestToken) + + nsc := cluster1.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken) + + var finalNSE = map[string]string{ + fmt.Sprint(nsReg1.Name, "@", cluster2.Name): nseReg1.GetName(), + nsReg2.GetName(): nseReg2.GetName(), + } + + for _, nsName := range []string{fmt.Sprint(nsReg1.Name, "@", cluster2.Name), nsReg2.GetName()} { + request := &networkservice.NetworkServiceRequest{ + MechanismPreferences: []*networkservice.Mechanism{ + {Cls: cls.LOCAL, Type: kernel.MECHANISM}, + }, + Connection: &networkservice.Connection{ + Id: "1", + NetworkService: nsName, + Context: &networkservice.ConnectionContext{}, + }, + } + + conn, err := nsc.Request(ctx, request) + require.NoError(t, err) + require.NotNil(t, conn) + + require.Equal(t, 8, len(conn.Path.PathSegments)) + + require.Equal(t, finalNSE[nsName], conn.GetPath().GetPathSegments()[7].GetName()) + + // 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, 8, len(conn.Path.PathSegments)) + require.Equal(t, finalNSE[nsName], conn.GetPath().GetPathSegments()[7].GetName()) + + // Close + _, err = nsc.Close(ctx, conn) + require.NoError(t, err) + } +} + // TestNSMGR_Interdomain_TwoNodesNSEs covers scenarion with connection from the one client to two endpoints from diffrenret clusters. // // nsc -> nsmgr1 -> forwarder1 -> nsmgr1 -> nsmgr-proxy1 -> nsmg-proxy2 -> nsmgr2 ->forwarder2 -> nsmgr2 -> nse2 diff --git a/pkg/registry/chains/proxydns/server_ns_test.go b/pkg/registry/chains/proxydns/server_ns_test.go index c425cfa799..b11ce24dae 100644 --- a/pkg/registry/chains/proxydns/server_ns_test.go +++ b/pkg/registry/chains/proxydns/server_ns_test.go @@ -18,6 +18,7 @@ package proxydns_test import ( "context" + "strings" "testing" "time" @@ -132,6 +133,7 @@ func TestLocalDomain_NetworkServiceRegistry(t *testing.T) { }, ) require.Nil(t, err) + require.True(t, strings.Contains(expected.GetName(), "@"+domain1.Name)) cc, err := grpc.DialContext(ctx, grpcutils.URLToTarget(domain1.Registry.URL), grpc.WithBlock(), grpc.WithInsecure()) require.Nil(t, err) @@ -142,7 +144,7 @@ func TestLocalDomain_NetworkServiceRegistry(t *testing.T) { stream, err := client.Find(context.Background(), ®istryapi.NetworkServiceQuery{ NetworkService: ®istryapi.NetworkService{ - Name: expected.Name + "@" + domain1.Name, + Name: expected.Name, }, }) diff --git a/pkg/registry/common/dnsresolve/ns_server.go b/pkg/registry/common/dnsresolve/ns_server.go index a0c39431c2..25e047e0cf 100644 --- a/pkg/registry/common/dnsresolve/ns_server.go +++ b/pkg/registry/common/dnsresolve/ns_server.go @@ -66,6 +66,8 @@ func (d *dnsNSResolveServer) Register(ctx context.Context, ns *registry.NetworkS ns.Name = interdomain.Target(ns.Name) resp, err := next.NetworkServiceRegistryServer(ctx).Register(ctx, ns) + resp.Name = interdomain.Join(resp.Name, domain) + return resp, err } @@ -103,6 +105,9 @@ func (d *dnsNSResolveServer) Unregister(ctx context.Context, ns *registry.Networ } ctx = clienturlctx.WithClientURL(ctx, url) ns.Name = interdomain.Target(ns.Name) + defer func() { + ns.Name = interdomain.Join(ns.Name, domain) + }() return next.NetworkServiceRegistryServer(ctx).Unregister(ctx, ns) } diff --git a/pkg/registry/common/dnsresolve/nse_server.go b/pkg/registry/common/dnsresolve/nse_server.go index 56728949d3..af912eaeba 100644 --- a/pkg/registry/common/dnsresolve/nse_server.go +++ b/pkg/registry/common/dnsresolve/nse_server.go @@ -117,7 +117,7 @@ func (d *dnsNSEResolveServer) Register(ctx context.Context, nse *registry.Networ return nil, err } - translateNSE(nse, func(s string) string { + translateNSE(resp, func(s string) string { return interdomain.Join(s, domain) })