Commit f74fb0f1 authored by 黎博's avatar 黎博

merge master

parents 0593ab42 e7a037c0
......@@ -5,6 +5,7 @@ import cn.qg.holmes.exception.TokenInvalidException;
import cn.qg.holmes.exception.UnauthorizedException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
......@@ -45,6 +46,15 @@ public class ExceptionHandlerAdvice {
return JsonResult.buildErrorStateResult(objectError.getDefaultMessage(), false);
}
/**
* 参数必须性处理
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public JsonResult handleException(MissingServletRequestParameterException e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult(e.getMessage(), false);
}
/**
* 其他所有异常
*/
......
......@@ -703,4 +703,18 @@ public class K8sController {
return JSON.toJSONString(serviceMap);
}
}
/**
* 获取k8s环境变量
* @param namespace 环境
* @return
*/
@GetMapping("/listEnvVars")
public Map getListEnvVars(@RequestParam String namespace) {
Map<String, Object> detailMap = k8sService.getListEnvVars(namespace);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("details", detailMap);
resultMap.put("success", true);
return resultMap;
}
}
......@@ -557,7 +557,6 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
e.printStackTrace();
}
}
log.info("表{}.{}同步完成.", dbName, tableName);
return true;
}
......
......@@ -2277,6 +2277,54 @@ public class K8sService {
return kubernetesClient.apps().deployments().inNamespace(namespace).withName(serviceName).get();
}
/**
* 获取环境的所有k8s变量
* @param namespace 环境
*/
public Map<String, Object> getListEnvVars(String namespace) {
Map<String, Object> envMap = new TreeMap<>();
List<Pod> podList = kubernetesClient.pods().inNamespace(namespace).list().getItems();
List<Service> serviceList = kubernetesClient.services().inNamespace(namespace).list().getItems();
// 遍历Pod列表
for (Pod pod: podList) {
String serviceName = pod.getMetadata().getLabels().get("qcloud-app");
serviceName = serviceName.replace("-", "_").toUpperCase();
envMap.put(serviceName + "_SERVICE_HOST", pod.getStatus().getHostIP());
}
// 遍历Service列表
for (Service service: serviceList) {
String serviceName = service.getMetadata().getName().toUpperCase().replaceAll("-", "_");
List<ServicePort> servicePortList = service.getSpec().getPorts();
if (service.getMetadata().getLabels().get("type").equals("base")) {
if (servicePortList.get(0).getPort() != null) {
envMap.put(serviceName + "_SERVICE_PORT", servicePortList.get(0).getPort());
if (serviceName.equals("MYSQL")) {
envMap.put("DB_SERVICE_PORT", servicePortList.get(0).getPort());
}
}
for (ServicePort servicePort: servicePortList) {
envMap.put(serviceName + "_SERVICE_PORT_" + servicePort.getPort(), servicePort.getPort());
if (serviceName.equals("MYSQL")) {
envMap.put("DB_SERVICE_PORT_" + servicePort.getPort(), servicePort.getPort());
}
}
} else {
if (servicePortList.get(0).getNodePort() != null) {
envMap.put(serviceName + "_SERVICE_PORT", servicePortList.get(0).getNodePort());
}
for (ServicePort servicePort: servicePortList) {
if (servicePort.getNodePort() != null) {
envMap.put(serviceName + "_SERVICE_PORT_" + servicePort.getPort(), servicePort.getNodePort());
}
if (servicePort.getPort() != null) {
envMap.put(serviceName + "_SERVICE_PORT_" + servicePort.getPort(), servicePort.getPort());
}
}
}
}
return envMap;
}
public void podTest() {
Map<String, String> labels = new HashMap<>();
labels.put("qcloud-app", "mysql");
......
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