1.30 k8s pods查询
This commit is contained in:
parent
c440a91782
commit
7471571e8c
@ -49,6 +49,9 @@ public class K8sDeploymentDTO extends BaseDTO {
|
|||||||
@Schema(description = "K8S中的更新时间")
|
@Schema(description = "K8S中的更新时间")
|
||||||
private LocalDateTime k8sUpdateTime;
|
private LocalDateTime k8sUpdateTime;
|
||||||
|
|
||||||
|
@Schema(description = "K8s资源版本号,用于增量同步")
|
||||||
|
private String resourceVersion;
|
||||||
|
|
||||||
@Schema(description = "YAML配置")
|
@Schema(description = "YAML配置")
|
||||||
private String yamlConfig;
|
private String yamlConfig;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,4 +60,11 @@ public class K8sDeployment extends Entity<Long> {
|
|||||||
|
|
||||||
@Column(name = "yaml_config", columnDefinition = "TEXT")
|
@Column(name = "yaml_config", columnDefinition = "TEXT")
|
||||||
private String yamlConfig;
|
private String yamlConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* K8s资源版本号,用于增量同步判断资源是否变化
|
||||||
|
* 对应K8s的metadata.resourceVersion字段
|
||||||
|
*/
|
||||||
|
@Column(name = "resource_version", length = 50)
|
||||||
|
private String resourceVersion;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -119,6 +119,21 @@ public interface IK8sServiceIntegration extends IExternalSystemIntegration {
|
|||||||
*/
|
*/
|
||||||
Integer calculateTotalRestartCount(ExternalSystem externalSystem, String namespace, String deploymentName);
|
Integer calculateTotalRestartCount(ExternalSystem externalSystem, String namespace, String deploymentName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量计算namespace下所有Deployment的重启次数
|
||||||
|
* 性能优化:一次性查询所有Pod,在内存中按Deployment分组计算
|
||||||
|
*
|
||||||
|
* @param externalSystem K8S系统配置
|
||||||
|
* @param namespace 命名空间名称
|
||||||
|
* @param deployments Deployment列表
|
||||||
|
* @return Map<deploymentName, restartCount>
|
||||||
|
*/
|
||||||
|
java.util.Map<String, Integer> batchCalculateRestartCounts(
|
||||||
|
ExternalSystem externalSystem,
|
||||||
|
String namespace,
|
||||||
|
List<K8sDeploymentResponse> deployments
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取系统类型
|
* 获取系统类型
|
||||||
*
|
*
|
||||||
|
|||||||
@ -229,6 +229,9 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 填充resourceVersion用于增量同步
|
||||||
|
response.setResourceVersion(deployment.getMetadata().getResourceVersion());
|
||||||
|
|
||||||
// 序列化为YAML配置
|
// 序列化为YAML配置
|
||||||
try {
|
try {
|
||||||
response.setYamlConfig(Yaml.dump(deployment));
|
response.setYamlConfig(Yaml.dump(deployment));
|
||||||
@ -303,6 +306,9 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 填充resourceVersion用于增量同步
|
||||||
|
response.setResourceVersion(deployment.getMetadata().getResourceVersion());
|
||||||
|
|
||||||
// 序列化为YAML配置
|
// 序列化为YAML配置
|
||||||
try {
|
try {
|
||||||
response.setYamlConfig(Yaml.dump(deployment));
|
response.setYamlConfig(Yaml.dump(deployment));
|
||||||
@ -763,6 +769,96 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量计算namespace下所有Deployment的重启次数
|
||||||
|
* 性能优化:一次性查询所有Pod,在内存中按Deployment分组计算
|
||||||
|
*
|
||||||
|
* @param externalSystem K8s集群
|
||||||
|
* @param namespace 命名空间
|
||||||
|
* @param deployments Deployment列表
|
||||||
|
* @return Map<deploymentName, restartCount>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, Integer> batchCalculateRestartCounts(
|
||||||
|
ExternalSystem externalSystem,
|
||||||
|
String namespace,
|
||||||
|
List<K8sDeploymentResponse> deployments) {
|
||||||
|
|
||||||
|
log.debug("批量计算Deployment重启次数,集群: {}, 命名空间: {}, Deployment数量: {}",
|
||||||
|
externalSystem.getName(), namespace, deployments.size());
|
||||||
|
|
||||||
|
Map<String, Integer> restartCountMap = new java.util.HashMap<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 一次性查询namespace下所有Pod
|
||||||
|
List<K8sPodResponse> allPods = listPods(externalSystem, namespace);
|
||||||
|
|
||||||
|
if (allPods.isEmpty()) {
|
||||||
|
log.debug("命名空间 {} 下没有Pod", namespace);
|
||||||
|
return restartCountMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 为每个Deployment计算重启次数
|
||||||
|
for (K8sDeploymentResponse deployment : deployments) {
|
||||||
|
try {
|
||||||
|
Map<String, String> selector = deployment.getSelector();
|
||||||
|
if (selector == null || selector.isEmpty()) {
|
||||||
|
log.debug("Deployment {} 没有selector,跳过", deployment.getName());
|
||||||
|
restartCountMap.put(deployment.getName(), 0);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 在内存中过滤匹配该Deployment的Pod
|
||||||
|
int totalRestartCount = allPods.stream()
|
||||||
|
.filter(pod -> matchesSelector(pod.getLabels(), selector))
|
||||||
|
.mapToInt(pod -> pod.getRestartCount() != null ? pod.getRestartCount() : 0)
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
restartCountMap.put(deployment.getName(), totalRestartCount);
|
||||||
|
log.debug("Deployment {} 的重启次数: {}", deployment.getName(), totalRestartCount);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("计算Deployment {} 重启次数失败: {}", deployment.getName(), e.getMessage());
|
||||||
|
restartCountMap.put(deployment.getName(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("批量计算完成,成功计算 {} 个Deployment的重启次数", restartCountMap.size());
|
||||||
|
return restartCountMap;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("批量计算重启次数失败,集群: {}, 命名空间: {}, 错误: {}",
|
||||||
|
externalSystem.getName(), namespace, e.getMessage(), e);
|
||||||
|
// 失败时返回空Map,不影响同步流程
|
||||||
|
return restartCountMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断Pod的labels是否匹配Deployment的selector
|
||||||
|
*
|
||||||
|
* @param podLabels Pod的labels
|
||||||
|
* @param selector Deployment的selector
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
private boolean matchesSelector(Map<String, String> podLabels, Map<String, String> selector) {
|
||||||
|
if (podLabels == null || podLabels.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// selector中的所有label都必须在Pod的labels中存在且值相等
|
||||||
|
for (Map.Entry<String, String> entry : selector.entrySet()) {
|
||||||
|
String selectorValue = entry.getValue();
|
||||||
|
String podValue = podLabels.get(entry.getKey());
|
||||||
|
|
||||||
|
if (podValue == null || !podValue.equals(selectorValue)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建K8S ApiClient(内部实现,不使用缓存)
|
* 创建K8S ApiClient(内部实现,不使用缓存)
|
||||||
*
|
*
|
||||||
|
|||||||
@ -19,4 +19,10 @@ public class K8sDeploymentResponse {
|
|||||||
private LocalDateTime creationTimestamp;
|
private LocalDateTime creationTimestamp;
|
||||||
private LocalDateTime lastUpdateTime;
|
private LocalDateTime lastUpdateTime;
|
||||||
private String yamlConfig;
|
private String yamlConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* K8s资源版本号,用于增量同步判断资源是否变化
|
||||||
|
* 每次资源被修改,resourceVersion都会改变
|
||||||
|
*/
|
||||||
|
private String resourceVersion;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,19 +83,37 @@ public class K8sDeploymentServiceImpl extends BaseServiceImpl<K8sDeployment, K8s
|
|||||||
.map(K8sDeploymentResponse::getName)
|
.map(K8sDeploymentResponse::getName)
|
||||||
.collect(java.util.stream.Collectors.toSet());
|
.collect(java.util.stream.Collectors.toSet());
|
||||||
|
|
||||||
|
// 性能优化:批量计算所有Deployment的重启次数(一次K8s API调用)
|
||||||
|
Map<String, Integer> restartCountMap = k8sServiceIntegration.batchCalculateRestartCounts(
|
||||||
|
externalSystem, namespace.getNamespaceName(), deploymentResponses
|
||||||
|
);
|
||||||
|
|
||||||
List<K8sDeployment> deploymentsToSave = new ArrayList<>();
|
List<K8sDeployment> deploymentsToSave = new ArrayList<>();
|
||||||
|
int unchangedCount = 0; // 统计未变化的资源数量
|
||||||
|
|
||||||
// 1. 更新/新增K8S中存在的资源
|
// 1. 更新/新增K8S中存在的资源
|
||||||
for (K8sDeploymentResponse response : deploymentResponses) {
|
for (K8sDeploymentResponse response : deploymentResponses) {
|
||||||
K8sDeployment deployment = existingMap.get(response.getName());
|
K8sDeployment deployment = existingMap.get(response.getName());
|
||||||
|
|
||||||
|
// 增量同步优化:如果资源未变化(resourceVersion相同),跳过
|
||||||
|
if (deployment != null &&
|
||||||
|
response.getResourceVersion() != null &&
|
||||||
|
response.getResourceVersion().equals(deployment.getResourceVersion())) {
|
||||||
|
unchangedCount++;
|
||||||
|
log.debug("Deployment {} 未变化,跳过更新", response.getName());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (deployment == null) {
|
if (deployment == null) {
|
||||||
deployment = new K8sDeployment();
|
deployment = new K8sDeployment();
|
||||||
deployment.setExternalSystemId(externalSystem.getId());
|
deployment.setExternalSystemId(externalSystem.getId());
|
||||||
deployment.setNamespaceId(namespace.getId());
|
deployment.setNamespaceId(namespace.getId());
|
||||||
deployment.setDeploymentName(response.getName());
|
deployment.setDeploymentName(response.getName());
|
||||||
|
log.debug("新增Deployment: {}", response.getName());
|
||||||
} else {
|
} else {
|
||||||
// 如果之前被软删除,恢复它
|
// 如果之前被软删除,恢复它
|
||||||
deployment.setDeleted(false);
|
deployment.setDeleted(false);
|
||||||
|
log.debug("更新Deployment: {}", response.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
deployment.setReplicas(response.getReplicas());
|
deployment.setReplicas(response.getReplicas());
|
||||||
@ -108,11 +126,12 @@ public class K8sDeploymentServiceImpl extends BaseServiceImpl<K8sDeployment, K8s
|
|||||||
deployment.setK8sUpdateTime(response.getLastUpdateTime());
|
deployment.setK8sUpdateTime(response.getLastUpdateTime());
|
||||||
deployment.setYamlConfig(response.getYamlConfig());
|
deployment.setYamlConfig(response.getYamlConfig());
|
||||||
|
|
||||||
// 计算并缓存总重启次数
|
// 更新resourceVersion字段为新的K8s resourceVersion
|
||||||
Integer totalRestartCount = k8sServiceIntegration.calculateTotalRestartCount(
|
deployment.setResourceVersion(response.getResourceVersion());
|
||||||
externalSystem, namespace.getNamespaceName(), response.getName()
|
|
||||||
);
|
// 从批量计算结果中获取重启次数
|
||||||
deployment.setTotalRestartCount(totalRestartCount);
|
Integer totalRestartCount = restartCountMap.get(response.getName());
|
||||||
|
deployment.setTotalRestartCount(totalRestartCount != null ? totalRestartCount : 0);
|
||||||
|
|
||||||
deploymentsToSave.add(deployment);
|
deploymentsToSave.add(deployment);
|
||||||
}
|
}
|
||||||
@ -127,11 +146,15 @@ public class K8sDeploymentServiceImpl extends BaseServiceImpl<K8sDeployment, K8s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 性能优化:批量保存
|
// 性能优化:批量保存
|
||||||
k8sDeploymentRepository.saveAll(deploymentsToSave);
|
if (!deploymentsToSave.isEmpty()) {
|
||||||
|
k8sDeploymentRepository.saveAll(deploymentsToSave);
|
||||||
|
}
|
||||||
|
|
||||||
int syncCount = (int) deploymentResponses.size();
|
int syncCount = (int) deploymentResponses.size();
|
||||||
|
|
||||||
log.info("K8S Deployment同步完成,集群: {}, Namespace: {}, 同步数量: {}",
|
log.info("K8S Deployment同步完成,集群: {}, Namespace: {}, 总数: {}, 更新: {}, 未变化: {}",
|
||||||
externalSystem.getName(), namespace.getNamespaceName(), syncCount);
|
externalSystem.getName(), namespace.getNamespaceName(), syncCount,
|
||||||
|
deploymentsToSave.size(), unchangedCount);
|
||||||
return syncCount;
|
return syncCount;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@ -1500,6 +1500,7 @@ CREATE TABLE deploy_k8s_deployment
|
|||||||
labels JSON NULL COMMENT '标签',
|
labels JSON NULL COMMENT '标签',
|
||||||
selector JSON NULL COMMENT '选择器',
|
selector JSON NULL COMMENT '选择器',
|
||||||
yaml_config TEXT NULL COMMENT '完整的YAML配置',
|
yaml_config TEXT NULL COMMENT '完整的YAML配置',
|
||||||
|
resource_version VARCHAR(50) NULL COMMENT 'K8s资源版本号,用于增量同步',
|
||||||
|
|
||||||
k8s_create_time DATETIME NULL COMMENT 'K8S中的创建时间',
|
k8s_create_time DATETIME NULL COMMENT 'K8S中的创建时间',
|
||||||
k8s_update_time DATETIME NULL COMMENT 'K8S中的更新时间',
|
k8s_update_time DATETIME NULL COMMENT 'K8S中的更新时间',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user