Commit 4c9da3d6 authored by 黎博's avatar 黎博

新增获取pod列表以及namespace列表接口

parent 41070a33
package cn.qg.holmes.controller.k8s;
import cn.qg.holmes.common.JsonResult;
import cn.qg.holmes.utils.K8sService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/k8s")
public class K8sController {
@Autowired
K8sService k8sService;
/**
* 获取namespace列表
* @return
*/
@GetMapping("/namespace")
public JsonResult getNamespaceList() {
return JsonResult.buildSuccessResult(k8sService.getNamespaceList());
}
/**
* 获取pod列表
* @param namespace 环境
* @return
*/
@GetMapping("/pod/list")
public JsonResult getServiceList(@RequestParam String namespace) {
List<Map<String, Object>> podList = k8sService.getPodList(namespace);
return JsonResult.buildSuccessResult(podList);
}
}
package cn.qg.holmes.utils;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
......@@ -18,7 +18,7 @@ import java.util.List;
import java.util.Map;
@Slf4j
//@Component
@Component
public class K8sService {
private KubernetesClient kubernetesClient;
......@@ -77,4 +77,77 @@ public class K8sService {
return resultList;
}
public List<Map<String, Object>> getServiceList() {
List<Map<String, Object>> serviceList = new ArrayList<>();
kubernetesClient.services().inNamespace("fe").list();
return null;
}
public List<Map<String, Object>> getPodList(String namespace) {
List<Pod> podList = kubernetesClient.pods().inNamespace(namespace).list().getItems();
List<Map<String, Object>> result = new ArrayList<>();
for (Pod pod: podList) {
if (pod.getStatus().getPhase().equals("Running")) {
Map<String, Object> map = new HashMap<>();
// 端口映射
List<Map<String, Object>> portMappingList = new ArrayList<>();
ObjectMeta podMetadata= pod.getMetadata();
String serviceName = podMetadata.getLabels().get("qcloud-app");
Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
List<ServicePort> servicePortList = service.getSpec().getPorts();
if (servicePortList.size() > 0) {
for (ServicePort servicePort: servicePortList) {
if (servicePort.getNodePort() != null) {
map.put("port_" + servicePort.getName(), servicePort.getNodePort());
}
Map<String, Object> portMap = new HashMap<>();
portMap.put("name", servicePort.getName());
portMap.put("nodePort", servicePort.getNodePort());
portMap.put("port", servicePort.getPort());
portMap.put("protocol", servicePort.getProtocol());
portMap.put("targetPort", servicePort.getTargetPort());
portMappingList.add(portMap);
}
}
if (portMappingList.size() > 0) {
map.put("portMappings", portMappingList);
}
map.put("createdAt", podMetadata.getCreationTimestamp());
map.put("serviceName", serviceName);
map.put("podName", podMetadata.getName());
map.put("labels", podMetadata.getLabels());
List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses();
map.put("image", pod.getStatus().getContainerStatuses().get(0).getImage());
map.put("imageId", pod.getStatus().getContainerStatuses().get(0).getImageID());
map.put("lanIp", pod.getStatus().getHostIP());
map.put("podIp", pod.getStatus().getPodIP());
map.put("startTime", pod.getStatus().getStartTime());
map.put("status", containerStatuses.get(0).getReady());
result.add(map);
}
}
return result;
}
public static void main(String[] args) {
K8sService k8sService = new K8sService();
// List<Service> serviceList = k8sService.kubernetesClient.services().inNamespace("fe").list().getItems();
// List<Pod> podList = k8sService.kubernetesClient.pods().inNamespace("fe").list().getItems();
// Pod pod = podList.get(22);
// System.out.println("Metadata: " + pod.getMetadata());
// System.out.println("Spec: " + pod.getSpec());
// System.out.println("Status: " + pod.getStatus());
// System.out.println("Api: " + pod.getApiVersion());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImage());
// System.out.println(pod.getStatus().getContainerStatuses().get(0).getImageID());
// System.out.println(pod.getStatus().getPhase());
// System.out.println(pod.getSpec().getContainers().get(0).getName());
// System.out.println(serviceList.get(19).getSpec().getPorts());
Service service = k8sService.kubernetesClient.services().inNamespace("fe").withName("holmes").get();
System.out.println(service);
}
}
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