Skip to content

Commit

Permalink
resolve cluster domain using env CLUSTER_DOMAIN as a fallback (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Nichols authored Apr 9, 2021
1 parent 4302f33 commit 3a2ae6d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
12 changes: 10 additions & 2 deletions network/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

const (
resolverFileName = "/etc/resolv.conf"
defaultDomainName = "cluster.local"
resolverFileName = "/etc/resolv.conf"
clusterDomainEnvKey = "CLUSTER_DOMAIN"
defaultDomainName = "cluster.local"
)

var (
Expand Down Expand Up @@ -55,6 +56,7 @@ func GetClusterDomainName() string {
}

func getClusterDomainName(r io.Reader) string {
// First look in the conf file.
for scanner := bufio.NewScanner(r); scanner.Scan(); {
elements := strings.Split(scanner.Text(), " ")
if elements[0] != "search" {
Expand All @@ -66,6 +68,12 @@ func getClusterDomainName(r io.Reader) string {
}
}
}

// Then look in the ENV.
if domain := os.Getenv(clusterDomainEnvKey); len(domain) > 0 {
return domain
}

// For all abnormal cases return default domain name.
return defaultDomainName
}
70 changes: 48 additions & 22 deletions network/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,79 @@ limitations under the License.
package network

import (
"os"
"strings"
"testing"
)

func TestGetDomainName(t *testing.T) {
tests := []struct {
name string
env string
resolvConf string
want string
}{
{
name: "all good",
resolvConf: `
}{{
name: "all good",
resolvConf: `
nameserver 1.1.1.1
search default.svc.abc.com svc.abc.com abc.com
options ndots:5
`,
want: "abc.com",
},
{
name: "all good with trailing dot",
resolvConf: `
want: "abc.com",
}, {
name: "all good with env set but ignored",
resolvConf: `
nameserver 1.1.1.1
search default.svc.abc.com svc.abc.com abc.com
options ndots:5
`,
env: "ignored.com",
want: "abc.com",
}, {
name: "all good from env",
resolvConf: ``,
env: "abc.com",
want: "abc.com",
}, {
name: "all good with trailing dot",
resolvConf: `
nameserver 1.1.1.1
search default.svc.abc.com. svc.abc.com. abc.com.
options ndots:5
`,
want: "abc.com",
},
{
name: "missing search line",
resolvConf: `
want: "abc.com",
}, {
name: "missing search line",
resolvConf: `
nameserver 1.1.1.1
options ndots:5
`,
want: defaultDomainName,
},
{
name: "non k8s resolv.conf format",
resolvConf: `
want: defaultDomainName,
}, {
name: "non k8s resolv.conf format",
resolvConf: `
nameserver 1.1.1.1
search abc.com xyz.com
options ndots:5
`,
want: defaultDomainName,
},
}
want: defaultDomainName,
}}

domainWas := os.Getenv(clusterDomainEnvKey)
t.Cleanup(func() {
if len(domainWas) > 0 {
_ = os.Setenv(clusterDomainEnvKey, domainWas)
} else {
_ = os.Unsetenv(clusterDomainEnvKey)
}
})

for _, tt := range tests {
if len(tt.env) > 0 {
_ = os.Setenv(clusterDomainEnvKey, tt.env)
} else {
_ = os.Unsetenv(clusterDomainEnvKey)
}
got := getClusterDomainName(strings.NewReader(tt.resolvConf))
if got != tt.want {
t.Errorf("Test %s failed expected: %s but got: %s", tt.name, tt.want, got)
Expand Down

0 comments on commit 3a2ae6d

Please sign in to comment.