diff --git a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java index 0828d462..c78fc129 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/deploy/integration/impl/K8sServiceIntegrationImpl.java @@ -625,12 +625,17 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp log.warn("Pod不存在: {}/{}", namespace, podName); throw new BusinessException(ResponseCode.K8S_POD_NOT_FOUND); } - log.error("查询K8S Pod日志失败,集群: {}, 命名空间: {}, Pod: {}, 错误: {}", - externalSystem.getName(), namespace, podName, e.getMessage(), e); + if (e.getCode() == 400) { + log.error("查询K8S Pod日志参数错误,集群: {}, 命名空间: {}, Pod: {}, 容器: {}, HTTP状态码: {}, 错误信息: {}, 响应体: {}", + externalSystem.getName(), namespace, podName, container, e.getCode(), e.getMessage(), e.getResponseBody(), e); + throw new BusinessException(ResponseCode.INVALID_PARAM); + } + log.error("查询K8S Pod日志失败,集群: {}, 命名空间: {}, Pod: {}, 容器: {}, HTTP状态码: {}, 错误信息: {}, 响应体: {}", + externalSystem.getName(), namespace, podName, container, e.getCode(), e.getMessage(), e.getResponseBody(), e); throw new BusinessException(ResponseCode.K8S_OPERATION_FAILED); } catch (Exception e) { - log.error("查询K8S Pod日志失败,集群: {}, 命名空间: {}, Pod: {}, 错误: {}", - externalSystem.getName(), namespace, podName, e.getMessage(), e); + log.error("查询K8S Pod日志失败,集群: {}, 命名空间: {}, Pod: {}, 容器: {}, 错误: {}", + externalSystem.getName(), namespace, podName, container, e.getMessage(), e); throw new BusinessException(ResponseCode.K8S_OPERATION_FAILED); } } @@ -648,16 +653,20 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp AppsV1Api api = new AppsV1Api(cache.apiClient); // 构建patch内容:更新spec.template.metadata.annotations添加重启时间戳 + // 使用ISO 8601格式的时间戳 + String timestamp = java.time.format.DateTimeFormatter.ISO_INSTANT + .format(java.time.Instant.now()); + String patchBody = String.format( "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"kubectl.kubernetes.io/restartedAt\":\"%s\"}}}}}", - LocalDateTime.now().toString() + timestamp ); - // 使用strategic merge patch更新Deployment + // 使用strategic merge patch更新Deployment,明确指定patch类型 api.patchNamespacedDeployment( deploymentName, namespace, - new io.kubernetes.client.custom.V1Patch(patchBody), + new io.kubernetes.client.custom.V1Patch(patchBody, io.kubernetes.client.custom.V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH), null, null, null, @@ -672,8 +681,8 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp log.warn("Deployment不存在: {}/{}", namespace, deploymentName); throw new BusinessException(ResponseCode.K8S_RESOURCE_NOT_FOUND); } - log.error("重启K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, 错误: {}", - externalSystem.getName(), namespace, deploymentName, e.getMessage(), e); + log.error("重启K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, HTTP状态码: {}, 错误信息: {}, 响应体: {}", + externalSystem.getName(), namespace, deploymentName, e.getCode(), e.getMessage(), e.getResponseBody(), e); throw new BusinessException(ResponseCode.K8S_OPERATION_FAILED); } catch (Exception e) { log.error("重启K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, 错误: {}", @@ -697,11 +706,11 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp // 构建patch内容:更新spec.replicas String patchBody = String.format("{\"spec\":{\"replicas\":%d}}", replicas); - // 使用strategic merge patch更新Deployment的scale + // 使用strategic merge patch更新Deployment的scale,明确指定patch类型 api.patchNamespacedDeploymentScale( deploymentName, namespace, - new io.kubernetes.client.custom.V1Patch(patchBody), + new io.kubernetes.client.custom.V1Patch(patchBody, io.kubernetes.client.custom.V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH), null, null, null, @@ -716,8 +725,8 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp log.warn("Deployment不存在: {}/{}", namespace, deploymentName); throw new BusinessException(ResponseCode.K8S_RESOURCE_NOT_FOUND); } - log.error("扩缩容K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, 错误: {}", - externalSystem.getName(), namespace, deploymentName, e.getMessage(), e); + log.error("扩缩容K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, HTTP状态码: {}, 错误信息: {}, 响应体: {}", + externalSystem.getName(), namespace, deploymentName, e.getCode(), e.getMessage(), e.getResponseBody(), e); throw new BusinessException(ResponseCode.K8S_OPERATION_FAILED); } catch (Exception e) { log.error("扩缩容K8S Deployment失败,集群: {}, 命名空间: {}, Deployment: {}, 错误: {}",