Commit fa1b2f33 authored by 黎博's avatar 黎博

update

parent ed3dc9a7
...@@ -17,6 +17,7 @@ import org.aspectj.lang.reflect.MethodSignature; ...@@ -17,6 +17,7 @@ import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
...@@ -32,6 +33,7 @@ import java.lang.reflect.Parameter; ...@@ -32,6 +33,7 @@ import java.lang.reflect.Parameter;
@Slf4j @Slf4j
@Aspect @Aspect
@Component @Component
@Order(5)
public class RecordOperationAspect { public class RecordOperationAspect {
@Autowired @Autowired
...@@ -40,7 +42,7 @@ public class RecordOperationAspect { ...@@ -40,7 +42,7 @@ public class RecordOperationAspect {
@Autowired @Autowired
UserBehaviorRecordService userBehaviorRecordService; UserBehaviorRecordService userBehaviorRecordService;
@Before("@annotation(cn.qg.holmes.config.annotation.RecordOperation)") @After("@annotation(cn.qg.holmes.config.annotation.RecordOperation)")
public void after(JoinPoint joinPoint) { public void after(JoinPoint joinPoint) {
// 获取token,并根据token获取到用户名 // 获取token,并根据token获取到用户名
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
...@@ -50,7 +52,7 @@ public class RecordOperationAspect { ...@@ -50,7 +52,7 @@ public class RecordOperationAspect {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
String username = tokenService.getUserInfoFromCache(token).getUsername(); String username = tokenService.getUserInfoFromCache(token).getChineseName();
Signature signature = joinPoint.getSignature(); Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature; MethodSignature methodSignature = (MethodSignature) signature;
RecordOperation recordOperation = methodSignature.getMethod().getAnnotation(RecordOperation.class); RecordOperation recordOperation = methodSignature.getMethod().getAnnotation(RecordOperation.class);
...@@ -58,6 +60,7 @@ public class RecordOperationAspect { ...@@ -58,6 +60,7 @@ public class RecordOperationAspect {
String action = recordOperation.value(); String action = recordOperation.value();
String namespace = ""; String namespace = "";
String serviceName = ""; String serviceName = "";
String image = "";
// 获取方法里的参数 // 获取方法里的参数
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer(); ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
...@@ -71,10 +74,11 @@ public class RecordOperationAspect { ...@@ -71,10 +74,11 @@ public class RecordOperationAspect {
if (parameterNames[i].equals("namespace")) { if (parameterNames[i].equals("namespace")) {
namespace = args[i].toString(); namespace = args[i].toString();
} }
if (parameterNames[i].equals("ServiceCreateVo")) { if (parameterNames[i].equals("serviceCreateVo")) {
ServiceCreateVo serviceCreateVo = (ServiceCreateVo) args[i]; ServiceCreateVo serviceCreateVo = (ServiceCreateVo) args[i];
serviceName = serviceCreateVo.getServiceName(); serviceName = serviceCreateVo.getServiceName();
namespace = serviceCreateVo.getNamespace(); namespace = serviceCreateVo.getNamespace();
image = serviceCreateVo.getImage().split(":")[1];
} }
} }
} }
...@@ -85,6 +89,8 @@ public class RecordOperationAspect { ...@@ -85,6 +89,8 @@ public class RecordOperationAspect {
userBehaviorRecord.setAction(action); userBehaviorRecord.setAction(action);
userBehaviorRecord.setNamespace(namespace); userBehaviorRecord.setNamespace(namespace);
userBehaviorRecord.setServiceName(serviceName); userBehaviorRecord.setServiceName(serviceName);
userBehaviorRecord.setImage(image);
log.info("用户{}在{}环境{}了服务{}。", username, namespace, action, serviceName);
userBehaviorRecordService.save(userBehaviorRecord); userBehaviorRecordService.save(userBehaviorRecord);
} }
} }
...@@ -87,7 +87,7 @@ public class K8sController { ...@@ -87,7 +87,7 @@ public class K8sController {
* @param serviceCreateVo 创建实体类 * @param serviceCreateVo 创建实体类
* @return * @return
*/ */
@RecordOperation("create") @RecordOperation("新增服务")
@PostMapping("/service/create") @PostMapping("/service/create")
public JsonResult createPod(@RequestBody ServiceCreateVo serviceCreateVo) { public JsonResult createPod(@RequestBody ServiceCreateVo serviceCreateVo) {
try { try {
...@@ -212,12 +212,13 @@ public class K8sController { ...@@ -212,12 +212,13 @@ public class K8sController {
/** /**
* 重置pod * 重置pod
* @param namespace 环境 * @param namespace 环境
* @param podName podName * @param serviceName podName
* @return * @return
*/ */
@RecordOperation("重置服务")
@PostMapping("/service/redeploy") @PostMapping("/service/redeploy")
public JsonResult resetPodByName(String namespace, String podName) { public JsonResult resetPodByName(String namespace, String serviceName) {
return JsonResult.buildSuccessResult(k8sService.resetPod(namespace, podName)); return JsonResult.buildSuccessResult(k8sService.resetPod(namespace, serviceName));
} }
/** /**
...@@ -225,6 +226,7 @@ public class K8sController { ...@@ -225,6 +226,7 @@ public class K8sController {
* @param serviceCreateVo * @param serviceCreateVo
* @return * @return
*/ */
@RecordOperation("jenkins打包")
@PostMapping("/service/modify") @PostMapping("/service/modify")
public JsonResult modifyPod(@RequestBody ServiceCreateVo serviceCreateVo) { public JsonResult modifyPod(@RequestBody ServiceCreateVo serviceCreateVo) {
log.info("接口/k8s/service/modify收到请求,请求参数为:{}", JSON.toJSONString(serviceCreateVo)); log.info("接口/k8s/service/modify收到请求,请求参数为:{}", JSON.toJSONString(serviceCreateVo));
...@@ -291,6 +293,7 @@ public class K8sController { ...@@ -291,6 +293,7 @@ public class K8sController {
* @param serviceCreateVo * @param serviceCreateVo
* @return * @return
*/ */
@RecordOperation("更新服务")
@PostMapping("/service/update") @PostMapping("/service/update")
public JsonResult updatePod(@RequestBody ServiceCreateVo serviceCreateVo) { public JsonResult updatePod(@RequestBody ServiceCreateVo serviceCreateVo) {
String namespace = serviceCreateVo.getNamespace(); String namespace = serviceCreateVo.getNamespace();
...@@ -342,7 +345,7 @@ public class K8sController { ...@@ -342,7 +345,7 @@ public class K8sController {
* @param serviceName 服务名称 * @param serviceName 服务名称
* @return * @return
*/ */
@RecordOperation("delete") @RecordOperation("删除服务")
@PostMapping("/service/delete") @PostMapping("/service/delete")
public JsonResult deletePodByName(String namespace, String serviceName) { public JsonResult deletePodByName(String namespace, String serviceName) {
return JsonResult.buildSuccessResult(k8sService.deleteDeployment(namespace, serviceName)); return JsonResult.buildSuccessResult(k8sService.deleteDeployment(namespace, serviceName));
......
...@@ -23,6 +23,8 @@ public class UserBehaviorRecord { ...@@ -23,6 +23,8 @@ public class UserBehaviorRecord {
private String serviceName; private String serviceName;
private String image;
private String namespace; private String namespace;
private String username; private String username;
......
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