Skip to content

Commit

Permalink
Remove duplicate dns configs in response connection in DNSContextClie…
Browse files Browse the repository at this point in the history
…nt (#1318)

* remove duplicate dns configs in response

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix linter

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* delete metadata + add removeDuplicates before request

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix linter

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* check intersections + add removeDuplicates test

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* add dns configs check on dnsContextServer

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix ci

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix Test_DNSContextClient_Usecases

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* rework dns configs check in dnsContextServer

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* fix Test_DNSUsecase

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik authored Jul 12, 2022
1 parent 454062d commit 65679c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
func Test_DNSUsecase(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

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

domain := sandbox.NewBuilder(ctx, t).
Expand Down
20 changes: 17 additions & 3 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ func (c *dnsContextClient) initialize() {

c.storeOriginalResolvConf()

c.resolvconfDNSConfig = &networkservice.DNSConfig{
SearchDomains: r.Value(dnscontext.AnyDomain),
DnsServerIps: r.Value(dnscontext.NameserverProperty),
nameserver := r.Value(dnscontext.NameserverProperty)
if !containsNameserver(nameserver, c.defaultNameServerIP) {
c.resolvconfDNSConfig = &networkservice.DNSConfig{
SearchDomains: r.Value(dnscontext.AnyDomain),
DnsServerIps: nameserver,
}
}

c.dnsConfigManager.Store("", c.resolvconfDNSConfig)

r.SetValue(dnscontext.NameserverProperty, c.defaultNameServerIP)
Expand All @@ -169,3 +173,13 @@ func (c *dnsContextClient) initialize() {

c.updateCorefileQueue.AsyncExec(c.updateCorefile)
}

func containsNameserver(servers []string, value string) bool {
for i := range servers {
if servers[i] == value {
return true
}
}

return false
}
40 changes: 39 additions & 1 deletion pkg/networkservice/connectioncontext/dnscontext/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -43,10 +45,46 @@ func (d *dnsContextServer) Request(ctx context.Context, request *networkservice.
if request.GetConnection().GetContext().GetDnsContext() == nil {
request.GetConnection().GetContext().DnsContext = new(networkservice.DNSContext)
}
request.GetConnection().GetContext().GetDnsContext().Configs = append(request.GetConnection().GetContext().GetDnsContext().Configs, d.configs...)

for _, config := range d.configs {
if !contains(request.GetConnection().GetContext().GetDnsContext().Configs, config) {
request.GetConnection().GetContext().GetDnsContext().Configs = append(request.GetConnection().GetContext().GetDnsContext().Configs, config)
}
}
return next.Server(ctx).Request(ctx, request)
}

func (d *dnsContextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}

func contains(array []*networkservice.DNSConfig, value *networkservice.DNSConfig) bool {
for i := range array {
if equal(array[i].DnsServerIps, value.DnsServerIps) && equal(array[i].SearchDomains, value.SearchDomains) {
return true
}
}
return false
}

func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}

diff := make(map[string]int, len(a))
for _, v := range a {
diff[v]++
}

for _, v := range b {
if _, ok := diff[v]; !ok {
return false
}
diff[v]--
if diff[v] == 0 {
delete(diff, v)
}
}
return len(diff) == 0
}

0 comments on commit 65679c0

Please sign in to comment.