-
Notifications
You must be signed in to change notification settings - Fork 93
/
KubeApiPodCollector.java
103 lines (91 loc) · 4.19 KB
/
KubeApiPodCollector.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
/*
* Copyright 1999-2021 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.chaosblade.box.collector.kubeapi;
import cn.hutool.core.util.StrUtil;
import com.alibaba.chaosblade.box.collector.CollectorStrategy;
import com.alibaba.chaosblade.box.collector.CollectorType;
import com.alibaba.chaosblade.box.collector.PodCollector;
import com.alibaba.chaosblade.box.collector.model.Pod;
import com.alibaba.chaosblade.box.collector.model.Query;
import io.kubernetes.client.openapi.ApiCallback;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* @author yefei
*/
@Component
@CollectorStrategy(CollectorType.KUBE_API)
public class KubeApiPodCollector implements PodCollector, InitializingBean {
private ApiClient client;
@Override
public void afterPropertiesSet() throws Exception {
client = Config.defaultClient();
}
@Override
public CompletableFuture<List<Pod>> collect(Query query) {
CompletableFuture<List<Pod>> future = new CompletableFuture<>();
CoreV1Api api;
try {
if (StrUtil.isBlank(query.getConfig())) {
api = new CoreV1Api(client);
} else {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getConfig().getBytes());
api = new CoreV1Api(Config.fromConfig(byteArrayInputStream));
}
api.listPodForAllNamespacesAsync(null, null, query.getFieldSelector(), query.getLabelSelector(),
null, null, null, null, null,
new ApiCallback<V1PodList>() {
@Override
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
future.completeExceptionally(e);
}
@Override
public void onSuccess(V1PodList result, int statusCode, Map<String, List<String>> responseHeaders) {
List<Pod> pods = result.getItems().stream()
.filter(v1Pod -> Objects.equals(v1Pod.getSpec().getNodeName(), query.getNodeName()))
.map(v1Pod ->
Pod.builder()
.name(v1Pod.getMetadata().getName())
.namespace(v1Pod.getMetadata().getNamespace())
.ip(v1Pod.getStatus().getPodIP())
.build()
).collect(Collectors.toList());
future.complete(pods);
}
@Override
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
}
@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
}
});
} catch (Exception e) {
future.completeExceptionally(e);
}
return future;
}
}