Skip to content

Commit

Permalink
Do not keep more than 3 DNS servers and init default DNS struct (#1671)
Browse files Browse the repository at this point in the history
* do not store more than 3 dns servers

* initialize default value for dns config

* added rn

* huh I forgot to change it after testing
  • Loading branch information
hellt committed Oct 24, 2023
1 parent f200d06 commit 93e2e92
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/rn/0.47.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ Containerlab now [generates an SSH config](../manual/inventory.md#ssh-config) fi
* podman exec command fix #1653
* APT/YUM repositories are now automatically added to all SR Linux nodes. They are used to install NDK apps and CLI plugins provided by Nokia #1657
* SR Linux's mgmt0.0 interface is now auto-configured with the correct IP-MTU value #1658

## Patches

### 0.47.1

* Do not extract more than 3 DNS servers from the host's resolv.conf #1671
10 changes: 9 additions & 1 deletion types/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,15 @@ func (t *Topology) GetNodeDns(name string) *DNSConfig {
return kindDNS
}
}
return t.GetDefaults().GetDns()

defaultDNS := t.GetDefaults().GetDns()
if defaultDNS == nil {
// initialize defaultDNS to an empty DNSConfig struct
// so that we don't have to check for nil in the caller
defaultDNS = &DNSConfig{}
}

return defaultDNS
}

// GetCertificateConfig returns the certificate configuration for the given node.
Expand Down
8 changes: 8 additions & 0 deletions utils/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ func ExtractDNSServersFromResolvConf(filesys fs.FS, filenames []string) ([]strin
}

DNSServers := make([]string, 0, len(DNSServersMap))
var count int
for k := range DNSServersMap {
if count == 3 {
// keep only the first three DNS servers
// since C DNS resolver can't handle more
break
}
DNSServers = append(DNSServers, k)
count++
}

return DNSServers, nil
}
35 changes: 33 additions & 2 deletions utils/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ search .
want: nil,
wantErr: false,
},
{
name: "One file with more than 3 dns servers",
args: args{
filesys: fstest.MapFS{
"etc/resolv.conf": &fstest.MapFile{
Data: []byte(
`
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
nameserver 1.1.1.1
nameserver 1.1.1.2
nameserver 1.1.1.3
nameserver 1.1.1.4
nameserver 1.1.1.5
options edns0 trust-ad
search .
`,
),
},
},
filenames: []string{"etc/resolv.conf"},
},
want: []string{"1.1.1.1", "1.1.1.2", "1.1.1.3"},
wantErr: false,
},
{
name: "Two files local dns and two remote, two results",
args: args{
Expand Down Expand Up @@ -85,7 +114,7 @@ search .
wantErr: false,
},
{
name: "Duplicat 8.8.8.8",
name: "Duplicate 8.8.8.8",
args: args{
filesys: fstest.MapFS{
"etc/resolv.conf": &fstest.MapFile{
Expand Down Expand Up @@ -128,11 +157,13 @@ search .
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ExtractDNSServersFromResolvConf(tt.args.filesys, tt.args.filenames)

if (err != nil) != tt.wantErr {
t.Errorf("ExtractDNSServerFromResolvConf() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(func(s1, s2 string) bool {

if diff := cmp.Diff(got, tt.want, cmpopts.SortSlices(func(s1, s2 string) bool {
switch strings.Compare(s1, s2) {
case -1:
return false
Expand Down

0 comments on commit 93e2e92

Please sign in to comment.