Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support both field and label selector in Kubernetes collector #56

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,6 +30,10 @@ public class Query {

private String podName;

private String fieldSelector;

private String labelSelector;

private Long clusterId;

private String config;
Expand Down
Expand Up @@ -55,6 +55,11 @@ public void afterPropertiesSet() throws Exception {

@Override
public CompletableFuture<List<Container>> collect(Query query) {
String fieldSelector = StrUtil.isBlank(query.getFieldSelector()) ?
String.format("metadata.name=%s", query.getPodName()) :
String.format("metadata.name=%s,%s", query.getPodName(), query.getFieldSelector());
String labelSelector = query.getLabelSelector();

CompletableFuture<List<Container>> future = new CompletableFuture<>();
CoreV1Api api;
try {
Expand All @@ -64,7 +69,8 @@ public CompletableFuture<List<Container>> collect(Query query) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getConfig().getBytes());
api = new CoreV1Api(Config.fromConfig(byteArrayInputStream));
}
api.listPodForAllNamespacesAsync(null, null, String.format("metadata.name=%s", query.getPodName()), null,

api.listPodForAllNamespacesAsync(null, null, fieldSelector, labelSelector,
null, null, null, null, null,
new ApiCallback<V1PodList>() {
@Override
Expand Down
Expand Up @@ -63,7 +63,7 @@ public CompletableFuture<List<Pod>> collect(Query query) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getConfig().getBytes());
api = new CoreV1Api(Config.fromConfig(byteArrayInputStream));
}
api.listPodForAllNamespacesAsync(null, null, null, null,
api.listPodForAllNamespacesAsync(null, null, query.getFieldSelector(), query.getLabelSelector(),
null, null, null, null, null,
new ApiCallback<V1PodList>() {
@Override
Expand Down
Expand Up @@ -88,6 +88,12 @@ public class CollectorTimer implements InitializingBean {
@Value("${chaos.collector.period}")
private Integer period;

@Value("${chaos.collector.search.fieldSelector}")
private String fieldSelector;

@Value("${chaos.collector.search.labelSelector}")
private String labelSelector;

@Autowired
private ApplicationContext applicationContext;

Expand Down Expand Up @@ -193,8 +199,10 @@ private void podCollect(PodCollector collector, Query query) {
q.setClusterId(query.getClusterId());
q.setConfig(query.getConfig());
q.setNodeName(node.getNodeName());
CompletableFuture<List<Pod>> future = collector.collect(q);
q.setFieldSelector(fieldSelector) ;
q.setLabelSelector(labelSelector);

CompletableFuture<List<Pod>> future = collector.collect(q);
QueryWrapper<DeviceDO> queryWrapper = QueryWrapperBuilder.build();
queryWrapper.lambda().eq(DeviceDO::getType, DeviceType.POD.getCode());
deviceMapper.update(DeviceDO.builder().lastPingTime(DateUtil.date()).build(), queryWrapper);
Expand Down Expand Up @@ -260,6 +268,8 @@ private void containerCollect(ContainerCollector collector, Query query) {
q.setClusterId(query.getClusterId());
q.setConfig(query.getConfig());
q.setPodName(devicePod.getPodName());
q.setFieldSelector(fieldSelector) ;
q.setLabelSelector(labelSelector);

CompletableFuture<List<Container>> future = collector.collect(q);
future.handle((containers, e) -> {
Expand Down
3 changes: 3 additions & 0 deletions chaosblade-box-web/src/main/resources/application.yml
Expand Up @@ -61,6 +61,9 @@ chaos:
enable: false
type: kube_api
period: 30
search:
fieldSelector:
labelSelector:
prometheus:
api:
metric:
Expand Down