From 712c1a2da157b7224e117b6e216f92162a0f3629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Thu, 26 Sep 2024 09:51:46 +0800 Subject: [PATCH] 'commit' --- dsBuild/src/main/java/Util/K8sClient.java | 175 ------------------ dsBuild/src/main/java/Util/K8sClientTest.java | 51 ++++- 2 files changed, 46 insertions(+), 180 deletions(-) delete mode 100644 dsBuild/src/main/java/Util/K8sClient.java diff --git a/dsBuild/src/main/java/Util/K8sClient.java b/dsBuild/src/main/java/Util/K8sClient.java deleted file mode 100644 index dacca598..00000000 --- a/dsBuild/src/main/java/Util/K8sClient.java +++ /dev/null @@ -1,175 +0,0 @@ -package Util; - -import io.kubernetes.client.custom.IntOrString; -import io.kubernetes.client.openapi.ApiClient; -import io.kubernetes.client.openapi.ApiException; -import io.kubernetes.client.openapi.apis.CoreV1Api; -import io.kubernetes.client.openapi.apis.NetworkingV1Api; -import io.kubernetes.client.openapi.models.*; -import io.kubernetes.client.util.ClientBuilder; -import io.kubernetes.client.util.KubeConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileReader; -import java.io.IOException; -import java.util.Map; - - - - -public class K8sClient { - - private static final Logger log = LoggerFactory.getLogger(K8sClient.class); - - /** - * k8s-api客户端 - */ - private ApiClient apiClient; - - - /** - * 构建集群POD内通过SA访问的客户端 - * loading the in-cluster config, including: - * 1. service-account CA - * 2. service-account bearer-token - * 3. service-account namespace - * 4. master endpoints(ip, port) from pre-set environment variables - */ - public K8sClient() { - try { - this.apiClient = ClientBuilder.cluster().build(); - } catch (IOException e) { - log.error("构建K8s-Client异常", e); - throw new RuntimeException("构建K8s-Client异常"); - } - } - - - /** - * 构建集群外通过UA访问的客户端 - * loading the out-of-cluster config, a kubeconfig from file-system - * - * @param kubeConfigPath kube连接配置文件 - */ - public K8sClient(String kubeConfigPath) { - try { - this.apiClient = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build(); - } catch (IOException e) { - log.error("读取kubeConfigPath异常", e); - throw new RuntimeException("读取kubeConfigPath异常"); - } catch (Exception e) { - log.error("构建K8s-Client异常", e); - throw new RuntimeException("构建K8s-Client异常"); - } - } - - - /** - * 获取所有的Pod - * - * @return podList - */ - public V1PodList getAllPodList() { - // new a CoreV1Api - CoreV1Api api = new CoreV1Api(apiClient); - - - // invokes the CoreV1Api client - try { - V1PodList list = api.listPodForAllNamespaces().execute(); - return list; - } catch (ApiException e) { - log.error("获取podlist异常:" + e.getResponseBody(), e); - } - return null; - } - - - /** - * 创建k8s service - * - * @param namespace 命名空间 - * @param serviceName 服务名称 - * @param port 服务端口号(和目标pod的端口号一致) - * @param selector pod标签选择器 - * @return 创建成功的service对象 - */ - public V1Service createService(String namespace, String serviceName, Integer port, Map selector) { - //构建service的yaml对象 - V1Service svc = new V1ServiceBuilder() - .withNewMetadata() - .withName(serviceName) - .endMetadata() - .withNewSpec() - .addNewPort() - .withProtocol("TCP") - .withPort(port) - .withTargetPort(new IntOrString(port)) - .endPort() - .withSelector(selector) - .endSpec() - .build(); - - - // Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of CoreV1API - CoreV1Api api = new CoreV1Api(apiClient); - V1Service v1Service = null; - try { - v1Service = api.createNamespacedService(namespace, svc).execute(); - } catch (ApiException e) { - log.error("创建service异常:" + e.getResponseBody(), e); - } catch (Exception e) { - log.error("创建service系统异常:", e); - } - return v1Service; - } - - - /** - * 创建k8s V1Ingress - * - * @param namespace 命名空间 - * @param ingressName ingress名称 - * @param annotations ingress注解 - * @param path 匹配的路径 - * @param serviceName 路由到的服务名称 - * @param servicePort 路由到的服务端口 - * @return 创建成功的ingress对象 - */ - public V1Ingress createV1Ingress(String namespace, String ingressName, Map annotations, String path, - String serviceName, Integer servicePort) { - //构建ingress的yaml对象 - V1Ingress ingress = new V1IngressBuilder() - .withNewMetadata() - .withName(ingressName) - .withAnnotations(annotations) - .endMetadata() - .withNewSpec() - .addNewRule() - .withHttp(new V1HTTPIngressRuleValueBuilder().addToPaths(new V1HTTPIngressPathBuilder() - .withPath(path) - .withPathType("Prefix") - .withBackend(new V1IngressBackendBuilder() - .withService(new V1IngressServiceBackendBuilder() - .withName(serviceName) - .withPort(new V1ServiceBackendPortBuilder() - .withNumber(servicePort).build()).build()).build()).build()).build()) - .endRule() - .endSpec() - .build(); - - - //调用对应的API执行创建ingress的操作 - NetworkingV1Api api = new NetworkingV1Api(apiClient); - V1Ingress v1Ingress = null; - try { - v1Ingress = api.createNamespacedIngress(namespace, ingress).execute(); - } catch (ApiException e) { - log.error("创建ingress异常:" + e.getResponseBody(), e); - } catch (Exception e) { - log.error("创建ingress系统异常:", e); - } - return v1Ingress; - } -} \ No newline at end of file diff --git a/dsBuild/src/main/java/Util/K8sClientTest.java b/dsBuild/src/main/java/Util/K8sClientTest.java index 9abdb5f0..e6faef9a 100644 --- a/dsBuild/src/main/java/Util/K8sClientTest.java +++ b/dsBuild/src/main/java/Util/K8sClientTest.java @@ -1,23 +1,64 @@ package Util; import com.jfinal.kit.PathKit; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.ApiException; +import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.V1Pod; import io.kubernetes.client.openapi.models.V1PodList; +import io.kubernetes.client.util.ClientBuilder; +import io.kubernetes.client.util.KubeConfig; import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class K8sClientTest { - public static void main(String[] args) { + + public static void testGetAllPodList() throws IOException, ApiException { //Master /root/.kube/config文件拷贝过来 - String kubeConfigPath = PathKit.getRootClassPath()+ "\\.kube\\config"; + String kubeConfigPath = PathKit.getRootClassPath() + "\\.kube\\config"; if (!new File(kubeConfigPath).exists()) { System.out.println("kubeConfig不存在,跳过"); return; } - K8sClient k8sClient = new K8sClient(kubeConfigPath); - V1PodList podList = k8sClient.getAllPodList(); + + ApiClient apiClient = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build(); + // new a CoreV1Api + CoreV1Api api = new CoreV1Api(apiClient); + // invokes the CoreV1Api client + V1PodList podList = api.listPodForAllNamespaces().execute(); for (V1Pod item : podList.getItems()) { - System.out.println(item.getMetadata().getNamespace() + ":" + item.getMetadata().getName()); + String result = removePrefixAndLastTwoDashes(item.getMetadata().getName()); + if (result.equals("ds-base-web")) { + System.out.println(result); + } } } + + + /** + * 功能:去掉前缀和最后一个两个- + * + * @param input + * @return + */ + public static String removePrefixAndLastTwoDashes(String input) { + // 去掉开头的default: + String trimmed = input.replaceFirst("^default:", ""); + + // 去掉最后两个以-开头的字符串 + Pattern pattern = Pattern.compile("-[^-]+(-[^-]+)?$"); + Matcher matcher = pattern.matcher(trimmed); + if (matcher.find()) { + return trimmed.substring(0, trimmed.length() - matcher.group().length()); + } + return trimmed; + } + + public static void main(String[] args) throws IOException, ApiException { + testGetAllPodList(); + } }