Commit ef25ec83 authored by 黎博's avatar 黎博

新增清空redis接口

parent 99ac67b7
......@@ -3,14 +3,14 @@ package cn.qg.holmes.controller.k8s;
import cn.qg.holmes.common.JsonResult;
import cn.qg.holmes.entity.k8s.DockerProject;
import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import cn.qg.holmes.service.k8s.DockerProjectService;
import cn.qg.holmes.service.k8s.ImageService;
import cn.qg.holmes.service.k8s.MqService;
import cn.qg.holmes.service.k8s.TkeService;
import cn.qg.holmes.service.k8s.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.fabric8.kubernetes.api.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -24,7 +24,7 @@ import java.util.Map;
public class K8sController {
@Autowired
TkeService tkeService;
K8sService k8sService;
@Autowired
ImageService imageService;
......@@ -35,13 +35,16 @@ public class K8sController {
@Autowired
MqService mqService;
@Autowired
RedisService redisService;
/**
* 获取namespace列表
* @return
*/
@GetMapping("/namespace")
public JsonResult getNamespaceList() {
return JsonResult.buildSuccessResult(tkeService.getNamespaceList());
return JsonResult.buildSuccessResult(k8sService.getNamespaceList());
}
/**
......@@ -51,10 +54,17 @@ public class K8sController {
*/
@GetMapping("/service/list")
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);
}
@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 服务名称
......@@ -82,81 +92,81 @@ public class K8sController {
queryWrapper.eq("project_name", serviceName);
if (label.equals("base")) {
// Service
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlService(namespace);
k8sService.createMysqlService(namespace);
}
if (serviceName.equals("redis")) {
tkeService.createRedisService(namespace);
k8sService.createRedisService(namespace);
}
if (serviceName.equals("mongodb")) {
tkeService.createMongodbService(namespace);
k8sService.createMongodbService(namespace);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperService(namespace);
k8sService.createZookeeperService(namespace);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqService(namespace);
k8sService.createRabbitmqService(namespace);
}
}
// Pvc
if (!tkeService.queryIfPvcExistByName(namespace, serviceName)) {
if (!k8sService.queryIfPvcExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlPvc(namespace);
k8sService.createMysqlPvc(namespace);
}
// redis不需要pvc
// if (serviceName.equals("redis")) {
// tkeService.createRedisPvc(namespace);
// }
if (serviceName.equals("mongodb")) {
tkeService.createMongodbPvc(namespace);
k8sService.createMongodbPvc(namespace);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperPvc(namespace);
k8sService.createZookeeperPvc(namespace);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqPvc(namespace);
k8sService.createRabbitmqPvc(namespace);
}
}
// deployment
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
if (serviceName.equals("mysql")) {
tkeService.createMysqlDeployment(namespace, image);
k8sService.createMysqlDeployment(namespace, image);
}
if (serviceName.equals("redis")) {
tkeService.createRedisDeployment(namespace, image);
k8sService.createRedisDeployment(namespace, image);
}
if (serviceName.equals("mongodb")) {
tkeService.createMongoDbDeployment(namespace, image);
k8sService.createMongoDbDeployment(namespace, image);
}
if (serviceName.equals("zookeeper")) {
tkeService.createZookeeperDeployment(namespace, image);
k8sService.createZookeeperDeployment(namespace, image);
}
if (serviceName.equals("rabbitmq")) {
tkeService.createRabbitmqDeployment(namespace, image);
k8sService.createRabbitmqDeployment(namespace, image);
}
}
} else if (label.equals("java")) {
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createJavaService(namespace, serviceName);
if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
k8sService.createJavaService(namespace, serviceName);
}
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createJavaDeployment(serviceCreateVo, dockerProject);
if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
k8sService.createJavaDeployment(serviceCreateVo, dockerProject);
}
if (!tkeService.queryIfIngressExistByName(namespace, serviceName)) {
tkeService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
if (!k8sService.queryIfIngressExistByName(namespace, serviceName)) {
k8sService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
}
} else if (label.equals("ui") || label.equals("node")) {
DockerProject dockerProject = dockerProjectService.getOne(queryWrapper);
if (!tkeService.queryIfServiceExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeService(namespace, serviceName, type, label);
if (!k8sService.queryIfServiceExistByName(namespace, serviceName)) {
k8sService.createUIAndNodeService(namespace, serviceName, type, label);
}
if (!tkeService.queryIfDeploymentExistByName(namespace, serviceName)) {
tkeService.createUIAndNodeDeployment(serviceCreateVo, dockerProject);
if (!k8sService.queryIfDeploymentExistByName(namespace, serviceName)) {
k8sService.createUIAndNodeDeployment(serviceCreateVo, dockerProject);
}
if (!tkeService.queryIfIngressExistByName(namespace, serviceName)) {
tkeService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
if (!k8sService.queryIfIngressExistByName(namespace, serviceName)) {
k8sService.createIngress(namespace, serviceName, serviceCreateVo.getDomain());
}
} else {
return JsonResult.buildErrorStateResult("暂不支持!", false);
......@@ -176,7 +186,7 @@ public class K8sController {
*/
@PostMapping("/service/redeploy")
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 {
*/
@PostMapping("/service/modify")
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 {
*/
@PostMapping("/service/delete")
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 {
return JsonResult.buildSuccessResult(imageService.getSingleTypeApp(namespace));
}
/**
* 同步线上mq数据到测试
* @param host 测试环境mq地址
* @return
*/
@PostMapping("/sync/mq")
public JsonResult syncOnlineMqToTest(String host) {
public JsonResult syncOnlineMqToTest(@RequestParam String 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;
import cn.qg.holmes.entity.k8s.DockerProject;
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.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
......@@ -28,11 +26,11 @@ import java.util.Map;
@Slf4j
@Component
public class TkeService {
public class K8sService {
private KubernetesClient kubernetesClient;
public TkeService() {
public K8sService() {
try {
String configYAML = String.join("\n", readConfigFile("kube-config.yml"));
Config config = Config.fromKubeconfig(configYAML);
......@@ -1830,12 +1828,77 @@ public class TkeService {
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) {
TkeService tkeService = new TkeService();
// tkeService.updateDeployment("k8s", "qa-platform", "qa-test/qa-platform:master-20210729112805284");
// 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"));
K8sService k8sService = new K8sService();
System.out.println(k8sService.getPodDetail("k8s", "base", "redis"));
}
}
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