Commit 664afe93 authored by 黎博's avatar 黎博

NamespaceCache初始化修改

parent 0ba84c2f
package cn.qg.qaplatform.config; package cn.qg.qaplatform.config;
import cn.qg.qaplatform.utils.HttpClientUtils; import cn.qg.qaplatform.utils.HttpClientUtils;
import cn.qg.qaplatform.utils.K8sService;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -20,16 +22,16 @@ import java.util.Map; ...@@ -20,16 +22,16 @@ import java.util.Map;
@Component @Component
public class NamespaceCache { public class NamespaceCache {
@Autowired
K8sService k8sService;
public static Map<String, String> namespaceMap = new HashMap<>(); public static Map<String, String> namespaceMap = new HashMap<>();
@PostConstruct @PostConstruct
public void init() { public void init() {
String url = "http://holmes.liangkebang.com/k8s/namespace/list?env=all"; List<Map<String, String>> namespaceList = k8sService.getNamespaceList("all");
JSONObject response = HttpClientUtils.doGetReturnJson(url); for (Map<String, String> namespace: namespaceList) {
JSONArray nsJsonArray = (JSONArray) response.get("data"); namespaceMap.put(namespace.get("name"), namespace.get("mysqlHost") + ":" + namespace.get("mysqlPort"));
List<Map> namespaceList = JSONArray.parseArray(nsJsonArray.toJSONString(), Map.class);
for (Map map: namespaceList) {
namespaceMap.put(map.get("name").toString(), map.get("mysqlHost") + ":" + map.get("mysqlPort"));
} }
log.info("获取到Namespace列表:{}", JSON.toJSONString(namespaceMap)); log.info("获取到Namespace列表:{}", JSON.toJSONString(namespaceMap));
} }
......
package cn.qg.qaplatform.utils; package cn.qg.qaplatform.utils;
import com.alibaba.fastjson.JSON;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServicePort; import io.fabric8.kubernetes.api.model.ServicePort;
...@@ -14,10 +16,8 @@ import java.io.BufferedReader; ...@@ -14,10 +16,8 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.text.SimpleDateFormat;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.TreeMap;
@Slf4j @Slf4j
@Component @Component
...@@ -111,4 +111,103 @@ public class K8sService { ...@@ -111,4 +111,103 @@ public class K8sService {
} }
return envMap; return envMap;
} }
/**
* 获取namespace列表
* @return
*/
public List<Map<String, String>> getNamespaceList(String env) {
List<Map<String, String>> resultList = new ArrayList<>();
List<Namespace> namespaceList = kubernetesClient.namespaces().list().getItems();
for (Namespace namespace : namespaceList) {
if (namespace.getMetadata().getAnnotations() != null && env.equals("all")) {
String name = namespace.getMetadata().getName();
// 获取Service
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
// 获取Pod
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
labels.put("type", "base");
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
Integer port = null;
String host = null;
if (service != null) {
port = service.getSpec().getPorts().get(0).getNodePort();
}
if (podList.size() == 1) {
host = podList.get(0).getStatus().getHostIP();
}
if (podList.size() >= 2) {
for (Pod pod: podList) {
if (pod.getStatus().getHostIP() != null) {
host = pod.getStatus().getHostIP();
break;
}
}
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
map.put("status", namespace.getStatus().getPhase());
map.put("mysqlHost", host);
map.put("mysqlPort", String.valueOf(port));
try {
map.put("createdAt", df2.format(df.parse(namespace.getMetadata().getCreationTimestamp())));
} catch (Exception e) {
log.info("时间解析异常!");
e.printStackTrace();
map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
}
resultList.add(map);
} else if (namespace.getMetadata().getAnnotations() != null && namespace.getMetadata().getAnnotations().get("description").equals(env)) {
String name = namespace.getMetadata().getName();
// 获取Service
Service service = kubernetesClient.services().inNamespace(name).withName("mysql").get();
// 获取Pod
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
labels.put("type", "base");
List<Pod> podList = kubernetesClient.pods().inNamespace(name).withLabels(labels).list().getItems();
Integer port = null;
String host = null;
if (service != null) {
port = service.getSpec().getPorts().get(0).getNodePort();
}
if (podList.size() == 1) {
host = podList.get(0).getStatus().getHostIP();
}
if (podList.size() >= 2) {
for (Pod pod: podList) {
if (pod.getStatus().getHostIP() != null) {
host = pod.getStatus().getHostIP();
break;
}
}
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("description", namespace.getMetadata().getAnnotations().get("description"));
map.put("owner", namespace.getMetadata().getAnnotations().get("owner"));
map.put("status", namespace.getStatus().getPhase());
map.put("mysqlHost", host);
map.put("mysqlPort", String.valueOf(port));
try {
map.put("createdAt", df2.format(df.parse(namespace.getMetadata().getCreationTimestamp())));
} catch (Exception e) {
log.info("时间解析异常!");
e.printStackTrace();
map.put("createdAt", namespace.getMetadata().getCreationTimestamp());
}
resultList.add(map);
}
}
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