From fa740a6b52dfca8a32e7a35b0345412ee3fb45ec Mon Sep 17 00:00:00 2001 From: CH3CHO Date: Wed, 18 Dec 2024 14:21:33 +0800 Subject: [PATCH] feat: Allow connecting to a separate K8s server when running in a K8s cluster --- .../kubernetes/KubernetesClientService.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java index bddfd679..be6abd45 100644 --- a/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java +++ b/backend/sdk/src/main/java/com/alibaba/higress/sdk/service/kubernetes/KubernetesClientService.java @@ -89,7 +89,7 @@ public class KubernetesClientService { private final OkHttpClient okHttpClient = new OkHttpClient(); - private Boolean inCluster; + private final Boolean inClusterMode; private final String kubeConfig; @@ -120,17 +120,17 @@ public KubernetesClientService(HigressServiceConfig config) throws IOException { this.controllerIngressClassName = config.getIngressClassName(); this.controllerJwtPolicy = config.getControllerJwtPolicy(); this.controllerAccessToken = config.getControllerAccessToken(); - this.inCluster = isInCluster(); + this.inClusterMode = Strings.isNullOrEmpty(kubeConfig) && isInCluster(); - if (inCluster) { + if (inClusterMode) { client = ClientBuilder.cluster().build(); - log.info("init KubernetesClientService InCluster"); + log.info("init KubernetesClientService with InCluster mode"); } else { String kubeConfigPath = !Strings.isNullOrEmpty(kubeConfig) ? kubeConfig : KUBE_CONFIG_DEFAULT_PATH; try (FileReader reader = new FileReader(kubeConfigPath)) { client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(reader)).build(); } - log.info("init KubernetesClientService LoadKubeConfig"); + log.info("init KubernetesClientService with KubeConfig: {}", kubeConfigPath); } initializeK8sCapabilities(); @@ -584,11 +584,11 @@ private void checkResponseStatus(V1Status status) { } private Request buildControllerRequest(String path) throws IOException { - String serviceHost = inCluster ? controllerServiceName + "." + controllerNamespace : controllerServiceHost; + String serviceHost = inClusterMode ? controllerServiceName + "." + controllerNamespace : controllerServiceHost; String url = "http://" + serviceHost + ":" + controllerServicePort + path; Request.Builder builder = new Request.Builder().url(url); String token = controllerAccessToken; - if (Strings.isNullOrEmpty(token) && inCluster) { + if (Strings.isNullOrEmpty(token) && inClusterMode) { token = readTokenFromFile(); } if (!Strings.isNullOrEmpty(token)) {