-
Notifications
You must be signed in to change notification settings - Fork 196
/
K8SServiceInfoProvider.java
178 lines (165 loc) · 7.76 KB
/
K8SServiceInfoProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
package com.tencent.bk.job.common.k8s.provider;
import com.tencent.bk.job.common.discovery.ServiceInfoProvider;
import com.tencent.bk.job.common.discovery.model.ServiceInstanceInfoDTO;
import com.tencent.bk.job.common.util.json.JsonUtils;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.openapi.models.V1PodStatus;
import io.kubernetes.client.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
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 PHASE_RUNNING = "Running";
private final DiscoveryClient discoveryClient;
public K8SServiceInfoProvider(DiscoveryClient discoveryClient) {
this.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, null);
}
private V1Pod findPodByUid(V1PodList podList, String uid) {
if (podList == null) return null;
for (V1Pod pod : podList.getItems()) {
V1ObjectMeta metaData = pod.getMetadata();
if (metaData != null && uid.equals(metaData.getUid())) {
return pod;
}
}
return null;
}
private Byte convertPodStatus(V1PodStatus podStatus) {
if (podStatus == null) return ServiceInstanceInfoDTO.STATUS_UNKNOWN;
String phase = podStatus.getPhase();
if (StringUtils.isNotBlank(phase)) {
if (phase.equals(PHASE_RUNNING))
return ServiceInstanceInfoDTO.STATUS_OK;
else
return ServiceInstanceInfoDTO.STATUS_ERROR;
}
return ServiceInstanceInfoDTO.STATUS_UNKNOWN;
}
/**
* 通过K8s API获取服务实例(pod)状态等详细信息
*
* @param serviceInstance 服务实例
* @return 服务实例详细信息
*/
private ServiceInstanceInfoDTO getDetailFromK8s(
ServiceInstance serviceInstance
) {
ServiceInstanceInfoDTO serviceInstanceInfoDTO = new ServiceInstanceInfoDTO();
serviceInstanceInfoDTO.setServiceName(serviceInstance.getServiceId());
serviceInstanceInfoDTO.setIp(serviceInstance.getHost());
serviceInstanceInfoDTO.setPort(serviceInstance.getPort());
String namespace = getNameSpace(serviceInstance);
V1PodList podList;
try {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
podList = api.listNamespacedPod(
namespace, null, null, null,
null, null, null, null,
null, null, null);
} catch (ApiException | IOException e) {
log.error("Fail to get pod info from k8s API", e);
serviceInstanceInfoDTO.setName(serviceInstance.getInstanceId());
serviceInstanceInfoDTO.setVersion(VERSION_UNKNOWN);
serviceInstanceInfoDTO.setStatusCode(convertPodStatus(null));
serviceInstanceInfoDTO.setStatusMessage("Fail to get pod info from k8s");
return serviceInstanceInfoDTO;
}
V1Pod pod = findPodByUid(podList, serviceInstance.getInstanceId());
V1ObjectMeta metaData = pod.getMetadata();
if (metaData != null) {
serviceInstanceInfoDTO.setName(metaData.getName());
}
if (metaData != null && metaData.getLabels() != null) {
String version = metaData.getLabels().getOrDefault(KEY_JOB_MS_VERSION, VERSION_UNKNOWN);
serviceInstanceInfoDTO.setVersion(version);
log.debug("namespace={},version={}", namespace, version);
}
V1PodStatus podStatus = pod.getStatus();
if (podStatus != null) {
serviceInstanceInfoDTO.setStatusCode(convertPodStatus(podStatus));
serviceInstanceInfoDTO.setStatusMessage(podStatus.getReason());
}
if (log.isDebugEnabled()) {
log.debug("podStatus={}", JsonUtils.toJson(podStatus));
}
return serviceInstanceInfoDTO;
}
/**
* 获取所有服务实例信息
*
* @return 服务实例信息
*/
@Override
public List<ServiceInstanceInfoDTO> listServiceInfo() {
List<String> serviceIdList = discoveryClient.getServices();
List<ServiceInstance> serviceInstanceList = new ArrayList<>();
for (String serviceId : serviceIdList) {
serviceInstanceList.addAll(discoveryClient.getInstances(serviceId));
}
for (ServiceInstance serviceInstance : serviceInstanceList) {
log.debug("serviceInstance={}", JsonUtils.toJson(serviceInstance));
}
return serviceInstanceList.parallelStream().filter(serviceInstance -> {
if (serviceInstance.getServiceId().contains("job-gateway-management")) {
return false;
} else {
return serviceInstance.getServiceId().contains("job-");
}
}).filter(serviceInstance -> getNameSpace(serviceInstance) != null)
.map(this::getDetailFromK8s).collect(Collectors.toList());
}
}