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

fix: apply changes for distributed vl3 dns after manual testing #1315

Merged
merged 3 commits into from
Jul 11, 2022
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
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