Commit a9d5369d authored by 黎博's avatar 黎博

获取namespace列表新增缓存

parent cba3c864
......@@ -2,6 +2,8 @@ package cn.qg.holmes.service.k8s;
import cn.qg.holmes.entity.k8s.DockerProject;
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.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
......@@ -12,6 +14,7 @@ import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
......@@ -27,11 +30,16 @@ import java.util.*;
@Component
public class K8sService {
@Autowired
RedisUtils redisUtils;
@Value("${no.healthcheck.service}")
private String noHealthCheckService;
private KubernetesClient kubernetesClient;
private final String NS_PREFIX = "namespace:";
public K8sService() {
try {
String configYAML = String.join("\n", readConfigFile("kube-config.yml"));
......@@ -110,8 +118,13 @@ public class K8sService {
* 获取namespace列表
* @return
*/
public List<Map<String, Object>> getNamespaceList(String env) {
List<Map<String, Object>> resultList = new ArrayList<>();
public List<Map> getNamespaceList(String env) {
// 首先从缓存获取
Object redisValue = redisUtils.get(NS_PREFIX + env);
if (redisValue != null) {
return JSON.parseArray(redisValue.toString(), Map.class);
} else {
List<Map> resultList = new ArrayList<>();
List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems();
for (Namespace namespace : namespaceList) {
if (namespace.getMetadata().getAnnotations() != null && env.equals("all")) {
......@@ -133,7 +146,7 @@ public class K8sService {
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Map<String, Object> map = new HashMap<>();
Map map = new HashMap<>();
map.put("name", name);
map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
......@@ -184,8 +197,11 @@ public class K8sService {
resultList.add(map);
}
}
// 设置缓存
redisUtils.set(NS_PREFIX + env, JSON.toJSONString(resultList), 1800);
return resultList;
}
}
/**
* 获取运行中的pod列表
......
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