Commit 8db756dd authored by 黎博's avatar 黎博

优化TkeService

parent f4f0c17e
......@@ -113,15 +113,8 @@ public class K8sController {
try {
String namespace = serviceCreateVo.getNamespace();
String serviceName = serviceCreateVo.getServiceName();
String serviceType = serviceCreateVo.getType();
if (tkeService.checkPvcExistence(namespace, serviceName)) {
return JsonResult.buildErrorStateResult("服务已存在", false);
}
// 新增Service
tkeService.createServiceByYaml(serviceCreateVo);
// 新增Deployment
tkeService.createDeploymentByYaml(serviceCreateVo);
if (StringUtils.equals(serviceType, "base")) {
String label = serviceCreateVo.getLabel();
if (StringUtils.equals(label, "base")) {
// 基础服务,如果没有PVC则创建
if (!tkeService.checkPvcExistence(namespace, serviceName)) {
tkeService.createPvcByYaml(serviceCreateVo);
......@@ -130,6 +123,10 @@ public class K8sController {
// 非基础服务,创建Ingress
tkeService.createIngressByYaml(serviceCreateVo);
}
// 新增Service
tkeService.createServiceByYaml(serviceCreateVo);
// 新增Deployment
tkeService.createDeploymentByYaml(serviceCreateVo);
} catch (Exception e) {
log.info("服务创建异常.", e);
e.printStackTrace();
......@@ -535,4 +532,10 @@ public class K8sController {
}
return JsonResult.buildSuccessResult(map);
}
@GetMapping("/clear/is")
public JsonResult clearIs(@RequestParam String env) {
tkeService.clearRubbish(env);
return JsonResult.buildSuccessResult(true);
}
}
......@@ -5,6 +5,7 @@ import cn.qg.holmes.entity.k8s.ServiceCreateVo;
import cn.qg.holmes.utils.DateUtils;
import cn.qg.holmes.utils.FileUtils;
import cn.qg.holmes.utils.TkeUtils;
import cn.qg.holmes.utils.YamlUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
......@@ -529,12 +530,12 @@ public class TkeService {
* @return
*/
public Deployment createDeploymentByYaml(ServiceCreateVo serviceCreateVo) {
String serviceType = serviceCreateVo.getType();
String label = serviceCreateVo.getLabel();
String serviceName = serviceCreateVo.getServiceName();
String namespace = serviceCreateVo.getNamespace();
String deploymentYaml;
try {
switch (serviceType) {
switch (label) {
case "base":
String yamlFilePath = "tke/template/" + StringUtils.capitalize(serviceName) + "-Deployment.yml";
deploymentYaml = FileUtils.readFileFromClassPathResource(yamlFilePath);
......@@ -584,7 +585,7 @@ public class TkeService {
deploymentYaml = TkeUtils.replaceBusinessYaml(deploymentYaml, serviceCreateVo, uiOrNodeProject);
break;
default:
log.info("暂不支持{}类型的deployment", serviceType);
log.info("暂不支持{}类型的deployment", label);
return null;
}
Yaml yaml = new Yaml(new Constructor(Deployment.class));
......@@ -603,6 +604,7 @@ public class TkeService {
deployment.getSpec().getTemplate().getSpec().getContainers().forEach(container -> container.setReadinessProbe(readinessProbe));
deployment.getSpec().getTemplate().getSpec().getContainers().forEach(container -> container.setLivenessProbe(livelinessProbe));
}
log.info("开始创建k8s Deployment: \n{}", YamlUtils.JsonToYamlStr(JSON.toJSONString(deployment)));
return kubernetesClient.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
} catch (IOException e) {
log.info("创建Deployment失败!");
......@@ -617,12 +619,12 @@ public class TkeService {
* @return
*/
public Service createServiceByYaml(ServiceCreateVo serviceCreateVo) {
String serviceType = serviceCreateVo.getType();
String label = serviceCreateVo.getLabel();
String serviceName = serviceCreateVo.getServiceName();
String namespace = serviceCreateVo.getNamespace();
String serviceYaml;
try {
switch (serviceType) {
switch (label) {
case "base":
String yamlFilePath = "tke/template/" + StringUtils.capitalize(serviceName) + "-Service.yml";
serviceYaml = FileUtils.readFileFromClassPathResource(yamlFilePath);
......@@ -638,11 +640,12 @@ public class TkeService {
serviceYaml = TkeUtils.replaceBusinessYaml(serviceYaml, serviceCreateVo, null);
break;
default:
log.info("暂不支持创建{}类型的k8s Service.", serviceType);
log.info("暂不支持创建{}类型的k8s Service.", label);
return null;
}
Yaml yaml = new Yaml(new Constructor(Service.class));
Service service = yaml.load(serviceYaml);
log.info("开始创建k8s Service: \n{}", serviceYaml);
return kubernetesClient.services().inNamespace(namespace).createOrReplace(service);
} catch (IOException e) {
log.info("Service创建失败.");
......@@ -667,7 +670,7 @@ public class TkeService {
String pvcFilePath = "tke/template/" + StringUtils.capitalize(serviceName) + "-Pvc.yml";
String pvcYaml = FileUtils.readFileFromClassPathResource(pvcFilePath);
pvcYaml = TkeUtils.replaceBasicYaml(pvcYaml, serviceCreateVo);
log.info("开始创建Pvc:\n{}", pvcYaml);
log.info("开始创建k8s Pvc:\n{}", pvcYaml);
Yaml yaml = new Yaml(new Constructor(PersistentVolumeClaim.class));
PersistentVolumeClaim persistentVolumeClaim = yaml.load(pvcYaml);
return kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(persistentVolumeClaim);
......@@ -766,4 +769,31 @@ public class TkeService {
return readinessProbe;
}
public void clearRubbish(String env) {
List<Map<String, Object>> mapList = getNamespaceList(env);
for (Map<String, Object> map: mapList) {
String namespace = map.get("name").toString();
log.info("开始清理{}环境的没有Deployment的Ingress和Service", namespace);
// 循环删除某个namespace下没有Deployment的Service和Ingress
List<Service> serviceList = kubernetesClient.services().inNamespace(namespace).list().getItems();
for (Service service: serviceList) {
String serviceName = service.getMetadata().getName();
if (!checkDeploymentExistence(namespace, serviceName)) {
log.info("{}环境{}Deployment不存在,删除Service", namespace, serviceName);
deleteService(namespace, serviceName);
}
}
// 循环删除某个namespace下没有Deployment的Ingress
List<Ingress> ingressList = kubernetesClient.extensions().ingresses().inNamespace(namespace).list().getItems();
for (Ingress ingress: ingressList) {
String serviceName = ingress.getMetadata().getName();
if (!checkDeploymentExistence(namespace, serviceName)) {
log.info("{}环境{}Deployment不存在,删除Ingress", namespace, serviceName);
deleteIngress(namespace, serviceName);
}
}
}
}
}
package cn.qg.holmes.utils;
import org.yaml.snakeyaml.Yaml;
import java.util.Map;
public class YamlUtils {
public static String JsonToYamlStr(String jsonStr){
Yaml yaml = new Yaml();
// 将JSON字符串转化为map
Map<String,Object> map = yaml.load(jsonStr);
//转换成 YAML 字符串
String yamlStr = yaml.dumpAsMap(map);
return yamlStr;
}
}
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