From 42a7642d4ecfc2987a12fc019f435a46f56956be Mon Sep 17 00:00:00 2001 From: Iacopo Rozzo Date: Fri, 30 Dec 2022 15:13:40 +0100 Subject: [PATCH 1/2] 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. --- kubernetes/config/incluster_config.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/kubernetes/config/incluster_config.c b/kubernetes/config/incluster_config.c index 94181b5c..609e492d 100644 --- a/kubernetes/config/incluster_config.c +++ b/kubernetes/config/incluster_config.c @@ -65,13 +65,24 @@ static int setBasePathInCluster(char **pBasePath) } int basePathSize = strlen(SERVICE_HTTPS_PREFIX) + strlen(service_host_env) + strlen(service_port_env) + 2 /* 1 for ':', 1 for '\0' */ ; + bool isIPv6 = false; + if (strchr(service_host_env, ':') == NULL) + { + isIPv6 = true; + // Takes into account the square brackets to escape the IP v6 address. + basePathSize += 2; + } char *basePath = calloc(basePathSize, sizeof(char)); if (!basePath) { fprintf(stderr, "%s: Cannot allocate the memory for base path for kubernetes service.\n", fname); return -1; } - snprintf(basePath, basePathSize, "%s%s:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env); + if (isIPv6) { + snprintf(basePath, basePathSize, "%s[%s]:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env); + } else { + snprintf(basePath, basePathSize, "%s%s:%s", SERVICE_HTTPS_PREFIX, service_host_env, service_port_env); + } *pBasePath = basePath; return 0; } From fc4f5d1f275cc5f1a3c758f393efa3f86674310b Mon Sep 17 00:00:00 2001 From: Iacopo Rozzo Date: Fri, 30 Dec 2022 16:36:12 +0100 Subject: [PATCH 2/2] Address review comments --- kubernetes/config/incluster_config.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kubernetes/config/incluster_config.c b/kubernetes/config/incluster_config.c index 609e492d..3e1708e1 100644 --- a/kubernetes/config/incluster_config.c +++ b/kubernetes/config/incluster_config.c @@ -66,8 +66,7 @@ static int setBasePathInCluster(char **pBasePath) int basePathSize = strlen(SERVICE_HTTPS_PREFIX) + strlen(service_host_env) + strlen(service_port_env) + 2 /* 1 for ':', 1 for '\0' */ ; bool isIPv6 = false; - if (strchr(service_host_env, ':') == NULL) - { + if (strchr(service_host_env, ':') != NULL) { isIPv6 = true; // Takes into account the square brackets to escape the IP v6 address. basePathSize += 2;