Commit a9d5369d authored by 黎博's avatar 黎博

获取namespace列表新增缓存

parent cba3c864
...@@ -2,6 +2,8 @@ package cn.qg.holmes.service.k8s; ...@@ -2,6 +2,8 @@ package cn.qg.holmes.service.k8s;
import cn.qg.holmes.entity.k8s.DockerProject; import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo; import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import cn.qg.holmes.utils.RedisUtils;
import com.alibaba.fastjson.JSON;
import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.Deployment; import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
...@@ -12,6 +14,7 @@ import io.fabric8.kubernetes.client.Config; ...@@ -12,6 +14,7 @@ import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -27,11 +30,16 @@ import java.util.*; ...@@ -27,11 +30,16 @@ import java.util.*;
@Component @Component
public class K8sService { public class K8sService {
@Autowired
RedisUtils redisUtils;
@Value("${no.healthcheck.service}") @Value("${no.healthcheck.service}")
private String noHealthCheckService; private String noHealthCheckService;
private KubernetesClient kubernetesClient; private KubernetesClient kubernetesClient;
private final String NS_PREFIX = "namespace:";
public K8sService() { public K8sService() {
try { try {
String configYAML = String.join("\n", readConfigFile("kube-config.yml")); String configYAML = String.join("\n", readConfigFile("kube-config.yml"));
...@@ -110,81 +118,89 @@ public class K8sService { ...@@ -110,81 +118,89 @@ public class K8sService {
* 获取namespace列表 * 获取namespace列表
* @return * @return
*/ */
public List<Map<String, Object>> getNamespaceList(String env) { public List<Map> getNamespaceList(String env) {
List<Map<String, Object>> resultList = new ArrayList<>(); // 首先从缓存获取
List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems(); Object redisValue = redisUtils.get(NS_PREFIX + env);
for (Namespace namespace : namespaceList) { if (redisValue != null) {
if (namespace.getMetadata().getAnnotations() != null && env.equals("all")) { return JSON.parseArray(redisValue.toString(), Map.class);
String name = namespace.getMetadata().getName(); } else {
// 获取Service List<Map> resultList = new ArrayList<>();
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get(); List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems();
// 获取Pod for (Namespace namespace : namespaceList) {
Map<String, String> labels = new HashMap<>(); if (namespace.getMetadata().getAnnotations() != null && env.equals("all")) {
labels.put("qcloud-app", "mysql"); String name = namespace.getMetadata().getName();
labels.put("type", "base"); // 获取Service
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems(); Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
Integer port = null; // 获取Pod
String host = null; Map<String, String> labels = new HashMap<>();
if (service != null) { labels.put("qcloud-app", "mysql");
port = service.getSpec().getPorts().get(0).getNodePort(); labels.put("type", "base");
} List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
if (podList.size() > 0) { Integer port = null;
host = podList.get(0).getStatus().getHostIP(); String host = null;
} if (service != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); port = service.getSpec().getPorts().get(0).getNodePort();
df.setTimeZone(TimeZone.getTimeZone("UTC")); }
Map<String, Object> map = new HashMap<>(); if (podList.size() > 0) {
map.put("name", name); host = podList.get(0).getStatus().getHostIP();
map.put("description", namespace.getMetadata().getAnnotations().get("description")); }
map.put("owner", namespace.getMetadata().getAnnotations().get("owner")); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
map.put("status", namespace.getStatus().getPhase()); df.setTimeZone(TimeZone.getTimeZone("UTC"));
map.put("mysqlHost", host); Map map = new HashMap<>();
map.put("mysqlPort", port); map.put("name", name);
try { map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp())); map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
} catch (Exception e) { map.put("status", namespace.getStatus().getPhase());
log.info("时间解析异常!"); map.put("mysqlHost", host);
e.printStackTrace(); map.put("mysqlPort", port);
map.put("createdAt", namespace.getMetadata().getCreationTimestamp()); try {
} map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp()));
resultList.add(map); } catch (Exception e) {
} else if (namespace.getMetadata().getAnnotations() != null && namespace.getMetadata().getAnnotations().get("description").equals(env)) { log.info("时间解析异常!");
String name = namespace.getMetadata().getName(); e.printStackTrace();
// 获取Service map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get(); }
// 获取Pod resultList.add(map);
Map<String, String> labels = new HashMap<>(); } else if (namespace.getMetadata().getAnnotations() != null && namespace.getMetadata().getAnnotations().get("description").equals(env)) {
labels.put("qcloud-app", "mysql"); String name = namespace.getMetadata().getName();
labels.put("type", "base"); // 获取Service
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems(); Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
Integer port = null; // 获取Pod
String host = null; Map<String, String> labels = new HashMap<>();
if (service != null) { labels.put("qcloud-app", "mysql");
port = service.getSpec().getPorts().get(0).getNodePort(); labels.put("type", "base");
} List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
if (podList.size() > 0) { Integer port = null;
host = podList.get(0).getStatus().getHostIP(); String host = null;
} if (service != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); port = service.getSpec().getPorts().get(0).getNodePort();
df.setTimeZone(TimeZone.getTimeZone("UTC")); }
Map<String, Object> map = new HashMap<>(); if (podList.size() > 0) {
map.put("name", name); host = podList.get(0).getStatus().getHostIP();
map.put("description", namespace.getMetadata().getAnnotations().get("description")); }
map.put("owner", namespace.getMetadata().getAnnotations().get("owner")); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
map.put("status", namespace.getStatus().getPhase()); df.setTimeZone(TimeZone.getTimeZone("UTC"));
map.put("mysqlHost", host); Map<String, Object> map = new HashMap<>();
map.put("mysqlPort", port); map.put("name", name);
try { map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp())); map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
} catch (Exception e) { map.put("status", namespace.getStatus().getPhase());
log.info("时间解析异常!"); map.put("mysqlHost", host);
e.printStackTrace(); map.put("mysqlPort", port);
map.put("createdAt", namespace.getMetadata().getCreationTimestamp()); try {
map.put("createdAt", df.parse(namespace.getMetadata().getCreationTimestamp()));
} catch (Exception e) {
log.info("时间解析异常!");
e.printStackTrace();
map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
}
resultList.add(map);
} }
resultList.add(map);
} }
// 设置缓存
redisUtils.set(NS_PREFIX + env, JSON.toJSONString(resultList), 1800);
return resultList;
} }
return resultList;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment