Skip to content

Commit 42a7642

Browse files
committed
Add support for IPv6 addresses in the in-cluster resolution
This change adds support for kubernetes services with cluster IPs v6 for the in cluster resolution. Previously, the IP v6 addresses were not wrapped in square brackets when forming the Apiserver URL.
1 parent 0525735 commit 42a7642

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

kubernetes/config/incluster_config.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,24 @@ static int setBasePathInCluster(char **pBasePath)
6565
}
6666

6767
int basePathSize = strlen(SERVICE_HTTPS_PREFIX) + strlen(service_host_env) + strlen(service_port_env) + 2 /* 1 for ':', 1 for '\0' */ ;
68+
bool isIPv6 = false;
69+
if (strchr(service_host_env, ':') == NULL)
70+
{
71+
isIPv6 = true;
72+
// Takes into account the square brackets to escape the IP v6 address.
73+
basePathSize += 2;
74+
}
6875
char *basePath = calloc(basePathSize, sizeof(char));
6976
if (!basePath) {
7077
fprintf(stderr, "%s: Cannot allocate the memory for base path for kubernetes service.\n", fname);
7178
return -1;
7279
}
7380

74-
snprintf(basePath, basePathSize, "%s%s:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env);
81+
if (isIPv6) {
82+
snprintf(basePath, basePathSize, "%s[%s]:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env);
83+
} else {
84+
snprintf(basePath, basePathSize, "%s%s:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env);
85+
}
7586
*pBasePath = basePath;
7687
return 0;
7788
}

0 commit comments

Comments
 (0)