Skip to content

Commit

Permalink
fix: apply changes for distributed vl3 dns after manual testing (#1315)
Browse files Browse the repository at this point in the history
* apply vl3dns fixes

Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>

* fix linter issues

Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>

* handle corner cases

Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin committed Jul 11, 2022
1 parent 9531f5d commit 454062d
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 51 deletions.
25 changes: 17 additions & 8 deletions pkg/networkservice/chains/nsmgr/vl3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func Test_NSC_ConnectsTo_vl3NSE(t *testing.T) {
nseReg,
sandbox.GenerateTestToken,
vl3.NewServer(ctx, serverPrefixCh),
vl3dns.NewServer(ctx, vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ .NetworkService }}."), vl3dns.WithDNSPort(40053)),
vl3dns.NewServer(ctx,
func() net.IP { return net.ParseIP("127.0.0.1") },
vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ .NetworkService }}."),
vl3dns.WithDNSPort(40053)),
)

resolver := net.Resolver{
Expand All @@ -95,7 +98,6 @@ func Test_NSC_ConnectsTo_vl3NSE(t *testing.T) {
require.NoError(t, err)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs(), 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps, 1)
require.Equal(t, "10.0.0.0", resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps[0])

req.Connection = resp.Clone()

Expand Down Expand Up @@ -152,14 +154,22 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) {

serverPrefixCh <- &ipam.PrefixResponse{Prefix: "10.0.0.1/24"}

var dnsConfigs = new(vl3dns.Map)

_ = domain.Nodes[0].NewEndpoint(
ctx,
nseReg,
sandbox.GenerateTestToken,
vl3.NewServer(ctx, serverPrefixCh),
vl3dns.NewServer(ctx, vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ .NetworkService }}."), vl3dns.WithDNSListenAndServeFunc(func(ctx context.Context, handler dnsutils.Handler, listenOn string) {
dnsutils.ListenAndServe(ctx, handler, ":50053")
}), vl3dns.WithDNSPort(40053)),
vl3dns.NewServer(ctx,
func() net.IP { return net.ParseIP("0.0.0.0") },
vl3dns.WithDomainSchemes("{{ index .Labels \"podName\" }}.{{ .NetworkService }}."),
vl3dns.WithDNSListenAndServeFunc(func(ctx context.Context, handler dnsutils.Handler, listenOn string) {
dnsutils.ListenAndServe(ctx, handler, ":50053")
}),
vl3dns.WithConfigs(dnsConfigs),
vl3dns.WithDNSPort(40053),
),
)

resolver := net.Resolver{
Expand All @@ -174,7 +184,7 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) {
defer close(clientPrefixCh)

clientPrefixCh <- &ipam.PrefixResponse{Prefix: "127.0.0.1/32"}
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, client.WithAdditionalFunctionality(vl3.NewClient(ctx, clientPrefixCh)))
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, client.WithAdditionalFunctionality(vl3dns.NewClient(net.ParseIP("127.0.0.1"), dnsConfigs), vl3.NewClient(ctx, clientPrefixCh)))

req := defaultRequest(nsReg.Name)
req.Connection.Id = uuid.New().String()
Expand All @@ -183,9 +193,8 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) {

resp, err := nsc.Request(ctx, req)
require.NoError(t, err)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs(), 1)
require.Len(t, resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps, 1)
require.Equal(t, "10.0.0.0", resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps[0])
require.Equal(t, "127.0.0.1", resp.GetContext().GetDnsContext().GetConfigs()[0].DnsServerIps[0])

require.Equal(t, "127.0.0.1/32", resp.GetContext().GetIpContext().GetSrcIpAddrs()[0])
req.Connection = resp.Clone()
Expand Down
84 changes: 84 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/vl3dns/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2022 Cisco 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 vl3dns

import (
"context"
"net"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type vl3DNSClient struct {
dnsServerIP net.IP
dnsConfigs *Map
}

// NewClient - returns a new null client that does nothing but call next.Client(ctx).{Request/Close} and return the result
// This is very useful in testing
func NewClient(dnsServerIP net.IP, dnsConfigs *Map) networkservice.NetworkServiceClient {
return &vl3DNSClient{
dnsServerIP: dnsServerIP,
dnsConfigs: dnsConfigs,
}
}

func (n *vl3DNSClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if request.GetConnection() == nil {
request.Connection = new(networkservice.Connection)
}
if request.GetConnection().GetContext() == nil {
request.GetConnection().Context = new(networkservice.ConnectionContext)
}
if request.GetConnection().GetContext().GetDnsContext() == nil {
request.GetConnection().GetContext().DnsContext = new(networkservice.DNSContext)
}

request.GetConnection().GetContext().GetDnsContext().Configs = []*networkservice.DNSConfig{
{
DnsServerIps: []string{n.dnsServerIP.String()},
},
}
resp, err := next.Client(ctx).Request(ctx, request, opts...)

if err == nil {
for _, config := range resp.GetContext().GetDnsContext().GetConfigs() {
var skip = false
for _, ip := range config.DnsServerIps {
if ip == n.dnsServerIP.String() {
skip = true
break
}
}
if skip {
continue
}
n.dnsConfigs.Store(resp.GetId(), config)
}
}

return resp, err
}

func (n *vl3DNSClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
n.dnsConfigs.Delete(conn.GetId())
return next.Client(ctx).Close(ctx, conn, opts...)
}
26 changes: 26 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/vl3dns/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 Cisco 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 vl3dns

import (
"sync"
)

//go:generate go-syncmap -output map.gen.go -type Map<string,*github.com/networkservicemesh/api/pkg/api/networkservice.DNSConfig>

// Map - sync.Map with key == string and value == networkservice.DNSConfig
type Map sync.Map
75 changes: 75 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/vl3dns/map.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package vl3dns
import (
"context"
"fmt"
"net/url"
"text/template"

"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
Expand All @@ -28,10 +27,10 @@ import (
// Option configures vl3DNSServer
type Option func(*vl3DNSServer)

// WithInitialFanoutList sets initial list to fanout queries
func WithInitialFanoutList(initialFanoutList []url.URL) Option {
// WithConfigs sets initial list to fanout queries
func WithConfigs(m *Map) Option {
return func(vd *vl3DNSServer) {
vd.initialFanoutList = initialFanoutList
vd.configs = m
}
}

Expand Down
Loading

0 comments on commit 454062d

Please sign in to comment.