Skip to content

Commit 874da3b

Browse files
authored
Edit CLUSTER_DOMAIN environment variable support (#3283)
* Edit CLUSTER_DOMAIN environment variable support for custom cluster domains This change modifies the getClusterDomainName() function to prioritize the CLUSTER_DOMAIN environment variable over /etc/resolv.conf parsing, enabling explicit configuration of cluster domains. This is particularly useful when using service mesh configurations where the internal DNS domain (e.g., mesh.net) differs from the Kubernetes cluster domain. Changes: - Modified getClusterDomainName() to check CLUSTER_DOMAIN env var first - Falls back to /etc/resolv.conf parsing if env var is not set - Updated tests to validate precedence behavior - Added test cases for custom domain scenarios * Edit CLUSTER_DOMAIN environment variable support for custom cluster domains This change modifies the getClusterDomainName() function to prioritize the CLUSTER_DOMAIN environment variable over /etc/resolv.conf parsing, enabling explicit configuration of cluster domains. This is particularly useful when using service mesh configurations where the internal DNS domain (e.g., mesh.net) differs from the Kubernetes cluster domain. Changes: - Modified getClusterDomainName() to check CLUSTER_DOMAIN env var first - Falls back to /etc/resolv.conf parsing if env var is not set - Updated tests to validate precedence behavior - Added test cases for custom domain scenarios
1 parent a440059 commit 874da3b

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

network/domain.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ func GetClusterDomainName() string {
5656
}
5757

5858
func getClusterDomainName(r io.Reader) string {
59-
// First look in the conf file.
59+
// First check the ENV variable (allows override).
60+
if domain := os.Getenv(clusterDomainEnvKey); len(domain) > 0 {
61+
return domain
62+
}
63+
64+
// Then look in the conf file.
6065
for scanner := bufio.NewScanner(r); scanner.Scan(); {
6166
elements := strings.Split(scanner.Text(), " ")
6267
if elements[0] != "search" {
@@ -69,11 +74,6 @@ func getClusterDomainName(r io.Reader) string {
6974
}
7075
}
7176

72-
// Then look in the ENV.
73-
if domain := os.Getenv(clusterDomainEnvKey); len(domain) > 0 {
74-
return domain
75-
}
76-
7777
// For all abnormal cases return default domain name.
7878
return defaultDomainName
7979
}

network/domain_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ options ndots:5
3636
`,
3737
want: "abc.com",
3838
}, {
39-
name: "all good with env set but ignored",
39+
name: "env variable takes precedence over resolv.conf",
4040
resolvConf: `
4141
nameserver 1.1.1.1
4242
search default.svc.abc.com svc.abc.com abc.com
4343
options ndots:5
4444
`,
45-
env: "ignored.com",
46-
want: "abc.com",
45+
env: "mesh.net",
46+
want: "mesh.net",
4747
}, {
48-
name: "all good from env",
48+
name: "env variable used when resolv.conf is empty",
4949
resolvConf: ``,
5050
env: "abc.com",
5151
want: "abc.com",

0 commit comments

Comments
 (0)