diff --git a/libnetwork/etchosts/etchosts.go b/libnetwork/etchosts/etchosts.go index 52063eeefd..92ea9cad4c 100644 --- a/libnetwork/etchosts/etchosts.go +++ b/libnetwork/etchosts/etchosts.go @@ -75,20 +75,19 @@ func Build(path, IP, hostname, domainname string, extraContent []Record) error { content := bytes.NewBuffer(nil) if IP != "" { - //set main record + // set main record var mainRec Record mainRec.IP = IP // User might have provided a FQDN in hostname or split it across hostname // and domainname. We want the FQDN and the bare hostname. fqdn := hostname if domainname != "" { - fqdn = fmt.Sprintf("%s.%s", fqdn, domainname) + fqdn += "." + domainname } - parts := strings.SplitN(fqdn, ".", 2) - if len(parts) == 2 { - mainRec.Hosts = fmt.Sprintf("%s %s", fqdn, parts[0]) - } else { - mainRec.Hosts = fqdn + mainRec.Hosts = fqdn + + if hostName, _, ok := strings.Cut(fqdn, "."); ok { + mainRec.Hosts += " " + hostName } if _, err := mainRec.WriteTo(content); err != nil { return err diff --git a/libnetwork/netlabel/labels.go b/libnetwork/netlabel/labels.go index f5075a6c34..6a188d4b62 100644 --- a/libnetwork/netlabel/labels.go +++ b/libnetwork/netlabel/labels.go @@ -30,7 +30,7 @@ const ( // DNSServers A list of DNS servers associated with the endpoint DNSServers = Prefix + ".endpoint.dnsservers" - //EnableIPv6 constant represents enabling IPV6 at network level + // EnableIPv6 constant represents enabling IPV6 at network level EnableIPv6 = Prefix + ".enable_ipv6" // DriverMTU constant represents the MTU size for the network driver @@ -106,27 +106,18 @@ func MakeKVClient(scope string) string { // Key extracts the key portion of the label func Key(label string) (key string) { - if kv := strings.SplitN(label, "=", 2); len(kv) > 0 { - key = kv[0] - } - return + key, _, _ = strings.Cut(label, "=") + return key } // Value extracts the value portion of the label func Value(label string) (value string) { - if kv := strings.SplitN(label, "=", 2); len(kv) > 1 { - value = kv[1] - } - return + _, value, _ = strings.Cut(label, "=") + return value } // KeyValue decomposes the label in the (key,value) pair func KeyValue(label string) (key string, value string) { - if kv := strings.SplitN(label, "=", 2); len(kv) > 0 { - key = kv[0] - if len(kv) > 1 { - value = kv[1] - } - } - return + key, value, _ = strings.Cut(label, "=") + return key, value } diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index 8206df6742..384d7e1ac1 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -110,19 +110,18 @@ func (sb *sandbox) updateHostsFile(ifaceIPs []string) error { // User might have provided a FQDN in hostname or split it across hostname // and domainname. We want the FQDN and the bare hostname. fqdn := sb.config.hostName - mhost := sb.config.hostName if sb.config.domainName != "" { - fqdn = fmt.Sprintf("%s.%s", fqdn, sb.config.domainName) + fqdn += "." + sb.config.domainName } + hosts := fqdn - parts := strings.SplitN(fqdn, ".", 2) - if len(parts) == 2 { - mhost = fmt.Sprintf("%s %s", fqdn, parts[0]) + if hostName, _, ok := strings.Cut(fqdn, "."); ok { + hosts += " " + hostName } var extraContent []etchosts.Record for _, ip := range ifaceIPs { - extraContent = append(extraContent, etchosts.Record{Hosts: mhost, IP: ip}) + extraContent = append(extraContent, etchosts.Record{Hosts: hosts, IP: ip}) } sb.addHostsEntries(extraContent)