fix(运维监控):修复应用Pod列表找不到的问题

调整 podSelector 的匹配规则,不再仅依赖选择器的 Key 部分,而是保存并使用 POD 的完整选择器字段(如 "app=nginx")。在获取应用的 Labels 列表时,将该完整选择器通过 = 拆分为 Key 和 Value,再分别与 POD 的 Labels 进行精确匹配,从而提升选择器匹配的准确性与灵活性。
This commit is contained in:
OTTO 2025-06-30 16:33:19 +08:00
parent 66e1b9aca6
commit 12eecde9c4
3 changed files with 25 additions and 4 deletions

View File

@ -35,6 +35,9 @@ public class K8sApplication {
@ApiModelProperty(value = "Pod选择器")
private String podSelector;
@ApiModelProperty(value = "Docker镜像")
private String dockerImage;
@ApiModelProperty(value = "运行状态")
private K8sApplicationState state;

View File

@ -18,6 +18,8 @@ public class RancherApplicationVo {
RancherMetadataVo metadata;
// 状态
RancherApplicationStateVo status;
// 规格
JSONObject spec;
public K8sApplication toK8sApplication() {
@ -36,6 +38,22 @@ public class RancherApplicationVo {
target.setReadyReplicas(status.getReadyReplicas());
}
if (spec != null) {
JSONObject templateObj = spec.getJSONObject("template");
if (templateObj != null) {
JSONObject specObj = templateObj.getJSONObject("spec");
if (specObj != null) {
JSONArray containersArray = specObj.getJSONArray("containers");
if (containersArray != null && !containersArray.isEmpty()) {
JSONObject firstContainer = containersArray.getJSONObject(0);
if (firstContainer != null && firstContainer.containsKey("image")) {
target.setDockerImage(firstContainer.getString("image"));
}
}
}
}
}
if (metadata.getAnnotations() != null && metadata.getAnnotations().containsKey("mon-name")) {
target.setAlias(metadata.getAnnotations().getString("mon-name"));
}
@ -51,10 +69,10 @@ public class RancherApplicationVo {
JSONObject relationship = relationships.getJSONObject(i);
if (relationship.containsKey("toType") && "pod".equals(relationship.getString("toType"))) {
String selector = relationship.getString("selector");
if (StringUtils.isNotEmpty(selector) && selector.contains("app=")) {
String appName = selector.substring(selector.indexOf("=") + 1);
target.setPodSelector(appName);
if (StringUtils.isNotEmpty(selector) && selector.contains("=") && selector.split("=").length >= 2) {
target.setPodSelector(selector);
}
}
}
return target;

View File

@ -10,6 +10,6 @@ public class RancherServiceVo {
String id;
// 元数据
RancherMetadataVo metadata;
// 状态
// 规格
RancherServiceSpecVo spec;
}