Skip to content

Commit 5130e10

Browse files
authored
chapi: update logic to obtain host domain (#122)
* Problem: * Current logic checks for all IPV4 addresses and perform reverse lookup. * With container environments, they will include overlay network as well * and incorrect host domains are obtained. * Implementation: * Use dnsdomainname when available to obtain host domain name. * Fallback to reverse lookup in other cases(very rare). * Testing: tested chapi on both Ubuntu and RHEL hosts. * Review: gcostea, rkumar Signed-off-by: Shiva Krishna, Merla <[email protected]>
1 parent da495ef commit 5130e10

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

linux/network.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
up = "UP"
2525
unknown = "UNKNOWN"
2626
ethtool = "ethtool"
27+
dnsDomainName = "dnsdomainname"
2728
maskFmt = "%d.%d.%d.%d"
2829
linkStatusPattern = "\\s+Link detected:\\s+yes"
2930
)
@@ -51,8 +52,17 @@ func GetHostNameAndDomain() ([]string, error) {
5152
return []string{hostname, domainname}, nil
5253
}
5354

54-
//GetDomainName : of the host
55+
//GetDomainName : returns domain name of the host
5556
func getDomainName() (string, error) {
57+
// attempt using dnsdomainname when available.
58+
args := []string{}
59+
domain, _, err := util.ExecCommandOutput(dnsDomainName, args)
60+
if err == nil {
61+
// remove any ending dot
62+
return strings.TrimSuffix(strings.TrimSpace(domain), "."), nil
63+
}
64+
65+
// fall back to logic using reverse lookup of IPv4 addresses.
5666
var addr string
5767
interfaces, err := getNetworkInterfaces()
5868
if err != nil {
@@ -71,7 +81,8 @@ func getDomainName() (string, error) {
7181
break
7282
}
7383
}
74-
return addr, nil
84+
// trim hostname and just return domain name
85+
return addr[strings.Index(addr, ".")+1:], nil
7586
}
7687

7788
// GetIPV4NetworkAddress returns network address for given ipv4 address and netmask

0 commit comments

Comments
 (0)