-
Notifications
You must be signed in to change notification settings - Fork 13
/
PodGroupService.java
53 lines (43 loc) · 1.47 KB
/
PodGroupService.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
package io.fabric8.demos.tests.mockserver;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.StatusDetails;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PodGroupService {
private final KubernetesClient kubernetesClient;
private final Map<String, String> matchLabels;
public PodGroupService(KubernetesClient client, Map<String, String> matchLabels) {
this.kubernetesClient = client;
this.matchLabels = matchLabels;
}
public PodList list() {
return kubernetesClient.pods().withLabels(matchLabels).list();
}
public int size() {
return list().getItems().size();
}
public void addToGroup(Pod newPod) {
Map<String, String> newLabels = new HashMap<>();
Map<String, String> oldLabels = newPod.getMetadata().getLabels();
newLabels.putAll(oldLabels);
newLabels.putAll(matchLabels);
newPod.getMetadata().setLabels(newLabels);
kubernetesClient.pods().resource(newPod).create();
}
public List<StatusDetails> delete() {
return kubernetesClient.pods()
.withLabels(matchLabels)
.withGracePeriod(0L)
.delete();
}
public Watch watch(Watcher<Pod> watcher) {
return kubernetesClient.pods()
.withLabels(matchLabels)
.watch(watcher);
}
}