Skip to content

Commit

Permalink
Merge pull request #918 from jsonwan/github_feature/k8s
Browse files Browse the repository at this point in the history
perf: 服务状态实例列表中忽略k8s产生的临时实例 #917
  • Loading branch information
wangyu096 committed May 10, 2022
2 parents 196a40e + 03a4631 commit 6ebacdc
Showing 1 changed file with 20 additions and 3 deletions.
Expand Up @@ -53,7 +53,6 @@ public class K8SServiceInfoProvider implements ServiceInfoProvider {
public final String KEY_HELM_NAMESPACE = "meta.helm.sh/release-namespace";
public final String KEY_JOB_MS_VERSION = "bk.job.image/tag";
public final String VERSION_UNKNOWN = "-";
public final String NAMESPACE_DEFAULT = "default";
public final String PHASE_RUNNING = "Running";
private final DiscoveryClient discoveryClient;

Expand All @@ -62,11 +61,17 @@ public K8SServiceInfoProvider(DiscoveryClient discoveryClient) {
log.debug("K8sServiceInfoServiceImpl inited");
}

/**
* 从服务实例元数据中提取命名空间
*
* @param serviceInstance 服务实例
* @return 命名空间
*/
private String getNameSpace(ServiceInstance serviceInstance) {
KubernetesServiceInstance k8sServiceInstance = (KubernetesServiceInstance) serviceInstance;
String namespace = k8sServiceInstance.getNamespace();
if (StringUtils.isNotBlank(namespace)) return namespace;
return serviceInstance.getMetadata().getOrDefault(KEY_HELM_NAMESPACE, NAMESPACE_DEFAULT);
return serviceInstance.getMetadata().getOrDefault(KEY_HELM_NAMESPACE, null);
}

private V1Pod findPodByUid(V1PodList podList, String uid) {
Expand All @@ -92,6 +97,12 @@ private Byte convertPodStatus(V1PodStatus podStatus) {
return ServiceInstanceInfoDTO.STATUS_UNKNOWN;
}

/**
* 通过K8s API获取服务实例(pod)状态等详细信息
*
* @param serviceInstance 服务实例
* @return 服务实例详细信息
*/
private ServiceInstanceInfoDTO getDetailFromK8s(
ServiceInstance serviceInstance
) {
Expand Down Expand Up @@ -139,6 +150,11 @@ private ServiceInstanceInfoDTO getDetailFromK8s(
return serviceInstanceInfoDTO;
}

/**
* 获取所有服务实例信息
*
* @return 服务实例信息
*/
@Override
public List<ServiceInstanceInfoDTO> listServiceInfo() {
List<String> serviceIdList = discoveryClient.getServices();
Expand All @@ -155,7 +171,8 @@ public List<ServiceInstanceInfoDTO> listServiceInfo() {
} else {
return serviceInstance.getServiceId().contains("job-");
}
}).map(this::getDetailFromK8s).collect(Collectors.toList());
}).filter(serviceInstance -> getNameSpace(serviceInstance) != null)
.map(this::getDetailFromK8s).collect(Collectors.toList());
}

}

0 comments on commit 6ebacdc

Please sign in to comment.