Skip to content

Commit 18ebb28

Browse files
tamilmani1989sharmasushant
authored andcommitted
Set dns domain on azure0 interface for ubuntu 18.0 and above(#350)
1 parent 01f722b commit 18ebb28

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

network/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type externalInterface struct {
2626
Networks map[string]*network
2727
Subnets []string
2828
BridgeName string
29-
DNSServerIP string
29+
DNSInfo DNSInfo
3030
MacAddress net.HardwareAddr
3131
IPAddresses []*net.IPNet
3232
Routes []*route

network/network_linux.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
distroID = "ID"
2525
ubuntuStr = "ubuntu"
2626
dnsServersStr = "DNS Servers"
27+
dnsDomainStr = "DNS Domain"
2728
ubuntuVersion17 = 17
2829
defaultDnsServerIP = "168.63.129.16"
2930
systemdResolvConfFile = "/run/systemd/resolve/resolv.conf"
@@ -190,44 +191,51 @@ func isGreaterOrEqaulUbuntuVersion(versionToMatch int) bool {
190191
return false
191192
}
192193

193-
func readDnsServerIP(ifName string) (string, error) {
194+
func readDnsInfo(ifName string) (DNSInfo, error) {
195+
var dnsInfo DNSInfo
196+
194197
cmd := fmt.Sprintf("systemd-resolve --status %s", ifName)
195198
out, err := platform.ExecuteCommand(cmd)
196199
if err != nil {
197-
return "", err
200+
return dnsInfo, err
198201
}
199202

200203
log.Printf("[net] console output for above cmd: %s", out)
201204

202205
lineArr := strings.Split(out, lineDelimiter)
203206
if len(lineArr) <= 0 {
204-
return "", fmt.Errorf("[net] Output of cmd %s returned nothing", cmd)
207+
return dnsInfo, fmt.Errorf("[net] Console output doesn't have any lines")
205208
}
206209

207210
for _, line := range lineArr {
208211
if strings.Contains(line, dnsServersStr) {
209212
dnsServerSplit := strings.Split(line, colonDelimiter)
210213
if len(dnsServerSplit) > 1 {
211-
return dnsServerSplit[1], nil
214+
dnsServerSplit[1] = strings.TrimSpace(dnsServerSplit[1])
215+
dnsInfo.Servers = append(dnsInfo.Servers, dnsServerSplit[1])
216+
}
217+
} else if strings.Contains(line, dnsDomainStr) {
218+
dnsDomainSplit := strings.Split(line, colonDelimiter)
219+
if len(dnsDomainSplit) > 1 {
220+
dnsInfo.Suffix = strings.TrimSpace(dnsDomainSplit[1])
212221
}
213222
}
214223
}
215224

216-
return "", fmt.Errorf("[net] Dns server ip not set on interface %v", ifName)
225+
return dnsInfo, nil
217226
}
218227

219-
func saveDnsConfig(extIf *externalInterface) {
220-
var err error
221-
222-
extIf.DNSServerIP, err = readDnsServerIP(extIf.Name)
223-
if err != nil {
224-
log.Printf("[net] Failed to read dns server IP from interface %v: %v", extIf.Name, err)
225-
extIf.DNSServerIP = defaultDnsServerIP
226-
return
228+
func saveDnsConfig(extIf *externalInterface) error {
229+
dnsInfo, err := readDnsInfo(extIf.Name)
230+
if err != nil || len(dnsInfo.Servers) == 0 || dnsInfo.Suffix == "" {
231+
log.Printf("[net] Failed to read dns info %+v from interface %v: %v", dnsInfo, extIf.Name, err)
232+
return err
227233
}
228234

229-
extIf.DNSServerIP = strings.TrimSpace(extIf.DNSServerIP)
230-
log.Printf("[net] Saved DNS server IP %v from %v", extIf.DNSServerIP, extIf.Name)
235+
extIf.DNSInfo = dnsInfo
236+
log.Printf("[net] Saved DNS Info %v from %v", extIf.DNSInfo, extIf.Name)
237+
238+
return nil
231239
}
232240

233241
// ApplyIPConfig applies a previously saved IP configuration to an interface.
@@ -260,8 +268,14 @@ func (nm *networkManager) applyIPConfig(extIf *externalInterface, targetIf *net.
260268
}
261269

262270
func applyDnsConfig(extIf *externalInterface, ifName string) error {
263-
cmd := fmt.Sprintf("systemd-resolve --interface=%s --set-dns=%s", ifName, extIf.DNSServerIP)
271+
cmd := fmt.Sprintf("systemd-resolve --interface=%s --set-dns=%s", ifName, extIf.DNSInfo.Servers[0])
264272
_, err := platform.ExecuteCommand(cmd)
273+
if err != nil {
274+
return err
275+
}
276+
277+
cmd = fmt.Sprintf("systemd-resolve --interface=%s --set-domain=%s", ifName, extIf.DNSInfo.Suffix)
278+
_, err = platform.ExecuteCommand(cmd)
265279
return err
266280
}
267281

@@ -337,7 +351,10 @@ func (nm *networkManager) connectExternalInterface(extIf *externalInterface, nwI
337351
isGreaterOrEqualUbuntu17 := isGreaterOrEqaulUbuntuVersion(ubuntuVersion17)
338352
if isGreaterOrEqualUbuntu17 {
339353
log.Printf("[net] Saving dns config from %v", extIf.Name)
340-
saveDnsConfig(extIf)
354+
if err := saveDnsConfig(extIf); err != nil {
355+
log.Printf("[net] Failed to save dns config: %v", err)
356+
return err
357+
}
341358
}
342359

343360
// External interface down.
@@ -394,7 +411,7 @@ func (nm *networkManager) connectExternalInterface(extIf *externalInterface, nwI
394411
return err
395412
}
396413

397-
log.Printf("[net] Applied dns IP %v on %v", extIf.DNSServerIP, bridgeName)
414+
log.Printf("[net] Applied dns config %v on %v", extIf.DNSInfo, bridgeName)
398415
}
399416

400417
extIf.BridgeName = bridgeName

0 commit comments

Comments
 (0)