Commit ef25ec83 authored by 黎博's avatar 黎博

新增清空redis接口

parent 99ac67b7
...@@ -3,14 +3,14 @@ package cn.qg.holmes.controller.k8s; ...@@ -3,14 +3,14 @@ package cn.qg.holmes.controller.k8s;
import cn.qg.holmes.common.JsonResult; import cn.qg.holmes.common.JsonResult;
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.service.k8s.DockerProjectService; import cn.qg.holmes.service.k8s.*;
import cn.qg.holmes.service.k8s.ImageService;
import cn.qg.holmes.service.k8s.MqService;
import cn.qg.holmes.service.k8s.TkeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.fabric8.kubernetes.api.model.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -24,7 +24,7 @@ import java.util.Map; ...@@ -24,7 +24,7 @@ import java.util.Map;
public class K8sController { public class K8sController {
@Autowired @Autowired
TkeService tkeService; K8sService k8sService;
@Autowired @Autowired
ImageService imageService; ImageService imageService;
...@@ -35,13 +35,16 @@ public class K8sController { ...@@ -35,13 +35,16 @@ public class K8sController {
@Autowired @Autowired
MqService mqService; MqService mqService;
@Autowired
RedisService redisService;
/** /**
* 获取namespace列表 * 获取namespace列表
* @return * @return
*/ */
@GetMapping("/namespace") @GetMapping("/namespace")
public JsonResult getNamespaceList() { public JsonResult getNamespaceList() {
return JsonResult.buildSuccessResult(tkeService.getNamespaceList()); return JsonResult.buildSuccessResult(k8sService.getNamespaceList());
} }
/** /**
...@@ -51,10 +54,17 @@ public class K8sController { ...@@ -51,10 +54,17 @@ public class K8sController {
*/ */
@GetMapping("/service/list") @GetMapping("/service/list")
public JsonResult getServiceList(@RequestParam String namespace) { public JsonResult getServiceList(@RequestParam String namespace) {
List<Map<String, Object>> podList = tkeService.getPodList(namespace); List<Map<String, Object>> podList = k8sService.getPodList(namespace);
return JsonResult.buildSuccessResult(podList); return JsonResult.buildSuccessResult(podList);
} }
@GetMapping("/service/detail")
public JsonResult getServiceDetail(@RequestParam String namespace,
@RequestParam String serviceType,
@RequestParam String serviceName) {
return JsonResult.buildSuccessResult(k8sService.getPodAndServiceInfo(namespace, serviceType, serviceName));
}
/** /**
* 根据服务名称获取镜像列表 * 根据服务名称获取镜像列表
* @param repoName 服务名称 * @param repoName 服务名称
...@@ -82,81 +92,81 @@ public class K8sController { ...@@ -82,81 +92,81 @@ public class K8sController {
queryWrapper.eq("project_name", serviceName); queryWrapper.eq("project_name", serviceName);
if (label.equals("base")) { if (label.equals("base")) {
// Service // Service
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) { if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) { if (serviceName.equals("mysql")) {
tkeService.createMysqlService(namespace); k8sService.createMysqlService(namespace);
} }
if (serviceName.equals("redis")) { if (serviceName.equals("redis")) {
tkeService.createRedisService(namespace); k8sService.createRedisService(namespace);
} }
if (serviceName.equals("mongodb")) { if (serviceName.equals("mongodb")) {
tkeService.createMongodbService(namespace); k8sService.createMongodbService(namespace);
} }
if (serviceName.equals("zookeeper")) { if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperService(namespace); k8sService.createZookeeperService(namespace);
} }
if (serviceName.equals("rabbitmq")) { if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqService(namespace); k8sService.createRabbitmqService(namespace);
} }
} }
// Pvc // Pvc
if (!tkeService.queryIfPvcExistByName(namespace, serviceName)) { if (!k8sService.queryIfPvcExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) { if (serviceName.equals("mysql")) {
tkeService.createMysqlPvc(namespace); k8sService.createMysqlPvc(namespace);
} }
// redis不需要pvc // redis不需要pvc
// if (serviceName.equals("redis")) { // if (serviceName.equals("redis")) {
// tkeService.createRedisPvc(namespace); // tkeService.createRedisPvc(namespace);
// } // }
if (serviceName.equals("mongodb")) { if (serviceName.equals("mongodb")) {
tkeService.createMongodbPvc(namespace); k8sService.createMongodbPvc(namespace);
} }
if (serviceName.equals("zookeeper")) { if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperPvc(namespace); k8sService.createZookeeperPvc(namespace);
} }
if (serviceName.equals("rabbitmq")) { if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqPvc(namespace); k8sService.createRabbitmqPvc(namespace);
} }
} }
// deployment // deployment
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) { if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) { if (serviceName.equals("mysql")) {
tkeService.createMysqlDeployment(namespace, image); k8sService.createMysqlDeployment(namespace, image);
} }
if (serviceName.equals("redis")) { if (serviceName.equals("redis")) {
tkeService.createRedisDeployment(namespace, image); k8sService.createRedisDeployment(namespace, image);
} }
if (serviceName.equals("mongodb")) { if (serviceName.equals("mongodb")) {
tkeService.createMongoDbDeployment(namespace, image); k8sService.createMongoDbDeployment(namespace, image);
} }
if (serviceName.equals("zookeeper")) { if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperDeployment(namespace, image); k8sService.createZookeeperDeployment(namespace, image);
} }
if (serviceName.equals("rabbitmq")) { if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqDeployment(namespace, image); k8sService.createRabbitmqDeployment(namespace, image);
} }
} }
} else if (label.equals("java")) { } else if (label.equals("java")) {
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper); DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) { if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createJavaService(namespace, serviceName); k8sService.createJavaService(namespace, serviceName);
} }
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) { if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createJavaDeployment(serviceCreateVo, dockerProject); k8sService.createJavaDeployment(serviceCreateVo, dockerProject);
} }
if (!tkeService.queryIfIngressExistByName(namespace, serviceName)) { if (!k8sService.queryIfIngressExistByName(namespace, serviceName)) {
tkeService.createIngress(namespace, serviceName, serviceCreateVo.getDomain()); k8sService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
} }
} else if (label.equals("ui") || label.equals("node")) { } else if (label.equals("ui") || label.equals("node")) {
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper); DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) { if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeService(namespace, serviceName, type, label); k8sService.createUIAndNodeService(namespace, serviceName, type, label);
} }
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) { if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeDeployment(serviceCreateVo, dockerProject); k8sService.createUIAndNodeDeployment(serviceCreateVo, dockerProject);
} }
if (!tkeService.queryIfIngressExistByName(namespace, serviceName)) { if (!k8sService.queryIfIngressExistByName(namespace, serviceName)) {
tkeService.createIngress(namespace, serviceName, serviceCreateVo.getDomain()); k8sService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
} }
} else { } else {
return JsonResult.buildErrorStateResult("暂不支持!", false); return JsonResult.buildErrorStateResult("暂不支持!", false);
...@@ -176,7 +186,7 @@ public class K8sController { ...@@ -176,7 +186,7 @@ public class K8sController {
*/ */
@PostMapping("/service/redeploy") @PostMapping("/service/redeploy")
public JsonResult resetPodByName(String namespace, String podName) { public JsonResult resetPodByName(String namespace, String podName) {
return JsonResult.buildSuccessResult(tkeService.resetPod(namespace, podName)); return JsonResult.buildSuccessResult(k8sService.resetPod(namespace, podName));
} }
/** /**
...@@ -188,7 +198,7 @@ public class K8sController { ...@@ -188,7 +198,7 @@ public class K8sController {
*/ */
@PostMapping("/service/modify") @PostMapping("/service/modify")
public JsonResult modifyPod(String namespace, String serviceName, String image) { public JsonResult modifyPod(String namespace, String serviceName, String image) {
return JsonResult.buildSuccessResult(tkeService.updateDeployment(namespace, serviceName, image)); return JsonResult.buildSuccessResult(k8sService.updateDeployment(namespace, serviceName, image));
} }
/** /**
...@@ -199,7 +209,7 @@ public class K8sController { ...@@ -199,7 +209,7 @@ public class K8sController {
*/ */
@PostMapping("/service/delete") @PostMapping("/service/delete")
public JsonResult deletePodByName(String namespace, String serviceName) { public JsonResult deletePodByName(String namespace, String serviceName) {
return JsonResult.buildSuccessResult(tkeService.deleteDeployment(namespace, serviceName)); return JsonResult.buildSuccessResult(k8sService.deleteDeployment(namespace, serviceName));
} }
/** /**
...@@ -212,8 +222,34 @@ public class K8sController { ...@@ -212,8 +222,34 @@ public class K8sController {
return JsonResult.buildSuccessResult(imageService.getSingleTypeApp(namespace)); return JsonResult.buildSuccessResult(imageService.getSingleTypeApp(namespace));
} }
/**
* 同步线上mq数据到测试
* @param host 测试环境mq地址
* @return
*/
@PostMapping("/sync/mq") @PostMapping("/sync/mq")
public JsonResult syncOnlineMqToTest(String host) { public JsonResult syncOnlineMqToTest(@RequestParam String host) {
return JsonResult.buildSuccessResult(mqService.setDefinitions(host)); return JsonResult.buildSuccessResult(mqService.setDefinitions(host));
} }
/**
* 情况测试环境redis
* @param namespace 环境
* @return
*/
@PostMapping("/redis/flush")
public JsonResult flushRedis(@RequestParam String namespace) {
try {
Map<String, Object> redisInfo = k8sService.getPodAndServiceInfo(namespace, "base", "redis");
String lanIp = redisInfo.get("lanIp").toString();
List<Map<String, Object>> portMappings = (List<Map<String, Object>>) redisInfo.get("portMappings");
for (Map<String, Object> portMap: portMappings) {
redisService.flushRedis(lanIp, (Integer) portMap.get("nodePort"));
}
return JsonResult.buildSuccessResult("redid清除成功!", true);
} catch (Exception e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult("redis清除失败!", false);
}
}
} }
...@@ -2,8 +2,6 @@ package cn.qg.holmes.service.k8s; ...@@ -2,8 +2,6 @@ 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 com.alibaba.fastjson.JSON;
import com.jayway.jsonpath.JsonPath;
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;
...@@ -28,11 +26,11 @@ import java.util.Map; ...@@ -28,11 +26,11 @@ import java.util.Map;
@Slf4j @Slf4j
@Component @Component
public class TkeService { public class K8sService {
private KubernetesClient kubernetesClient; private KubernetesClient kubernetesClient;
public TkeService() { public K8sService() {
try { try {
String configYAML = String.join("\n", readConfigFile("kube-config.yml")); String configYAML = String.join("\n", readConfigFile("kube-config.yml"));
Config config = Config.fromKubeconfig(configYAML); Config config = Config.fromKubeconfig(configYAML);
...@@ -1830,12 +1828,77 @@ public class TkeService { ...@@ -1830,12 +1828,77 @@ public class TkeService {
return kubernetesClient.extensions().ingresses().inNamespace(namespace).withName(serviceName).get() != null; return kubernetesClient.extensions().ingresses().inNamespace(namespace).withName(serviceName).get() != null;
} }
/**
* 获取Service详情
* @param namespace 环境
* @param serviceName service名称
* @return
*/
public Service getServiceDetail(String namespace, String serviceName) {
return kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
}
public Pod getPodDetail(String namespace, String serviceType, String serviceName) {
LabelSelector labelSelector = new LabelSelector();
Map<String, String> labelMap = new HashMap<>();
labelMap.put("type", serviceType);
labelMap.put("qcloud-app", serviceName);
labelSelector.setMatchLabels(labelMap);
return kubernetesClient.pods().inNamespace(namespace).withLabelSelector(labelSelector).list().getItems().get(0);
}
/**
* 获取pod和service的一些信息
* @param namespace
* @param serviceType
* @param serviceName
* @return
*/
public Map<String, Object> getPodAndServiceInfo(String namespace, String serviceType, String serviceName) {
Service service = getServiceDetail(namespace, serviceName);
Pod pod = getPodDetail(namespace, serviceType, serviceName);
Map<String, Object> map = new HashMap<>();
if (pod.getStatus().getPhase().equals("Running")) {
// 端口映射
List<Map<String, Object>> portMappingList = new ArrayList<>();
ObjectMeta podMetadata = pod.getMetadata();
if (service != null) {
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());
}
return map;
}
public static void main(String[] args) { public static void main(String[] args) {
TkeService tkeService = new TkeService(); K8sService k8sService = new K8sService();
// tkeService.updateDeployment("k8s", "qa-platform", "qa-test/qa-platform:master-20210729112805284"); System.out.println(k8sService.getPodDetail("k8s", "base", "redis"));
// tkeService.createIngress("fe", "qa-platform-ui", "qa-platform-ui");
// tkeService.createUIAndNodeService("fe", "qa-platform-ui", "ui", "ui");
// System.out.println(tkeService.queryIfIngressExistByName("k8s", "qa-platform"));
// System.out.println(tkeService.queryIfIngressExistByName("namespace", "qa-platform-ui"));
} }
} }
package cn.qg.holmes.service.k8s;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
@Slf4j
@Component
public class RedisService {
/**
* 清空redis
* @param redisHost redis host
* @param redisPort redis port
* @return
*/
public boolean flushRedis(String redisHost, Integer redisPort) {
try {
Jedis jedis = new Jedis(redisHost, redisPort);
jedis.flushAll();
jedis.quit();
jedis.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
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