1.30 k8s pods查询

This commit is contained in:
dengqichen 2025-12-13 21:57:16 +08:00
parent e5f4fee51d
commit 2ce817b978

View File

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