Skip to content

Commit ecb7539

Browse files
authored
Fix in-cluster kube_host_port generation for IPv6 (#875)
Signed-off-by: Lu Ma <[email protected]>
1 parent 05b8bf6 commit ecb7539

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

kube-client/src/config/incluster_config.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ pub fn kube_dns() -> http::Uri {
6060
fn kube_host_port() -> Option<String> {
6161
let host = kube_host()?;
6262
let port = kube_port()?;
63-
Some(format!("https://{}:{}", host, port))
63+
if host.contains(":") {
64+
// IPv6 cluster
65+
Some(format!("https://[{}]:{}", host, port))
66+
} else {
67+
Some(format!("https://{}:{}", host, port))
68+
}
6469
}
6570

6671
fn kube_host() -> Option<String> {
@@ -109,3 +114,15 @@ fn test_kube_server() {
109114
env::set_var(SERVICE_PORTENV, port);
110115
assert_eq!(kube_server().unwrap(), "https://fake.io:8080");
111116
}
117+
118+
#[test]
119+
fn test_kube_server_ipv6() {
120+
let host = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
121+
let port = "8080";
122+
env::set_var(SERVICE_HOSTENV, host);
123+
env::set_var(SERVICE_PORTENV, port);
124+
assert_eq!(
125+
kube_server().unwrap(),
126+
"https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
127+
);
128+
}

0 commit comments

Comments
 (0)