forked from vladimirvivien/k8s-client-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPVCWatch.java
More file actions
141 lines (125 loc) · 6.2 KB
/
PVCWatch.java
File metadata and controls
141 lines (125 loc) · 6.2 KB
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
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1PersistentVolumeClaim;
import io.kubernetes.client.models.V1PersistentVolumeClaimList;
import io.kubernetes.client.Pair;
import io.kubernetes.client.auth.*;
import io.kubernetes.client.util.*;
import io.kubernetes.client.custom.Quantity;
import io.kubernetes.client.custom.Quantity.Format;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.math.BigDecimal;
public class PVCWatch {
public static void main(String[] args) throws IOException{
// K8S_NAMESPACE defaults to empty
String namespace = System.getenv("K8S_NAMESPACE");
if (namespace == null || "".equals(namespace)) {
namespace = "default";
}
Quantity maxClaims = Quantity.fromString("150Gi");
Quantity totalClaims = Quantity.fromString("0Gi");
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
//client.setDebugging(true);
String apiServer = client.getBasePath();
System.out.format("%nconnecting to API server %s %n%n", apiServer);
client.getHttpClient().setReadTimeout(0, TimeUnit.SECONDS);
CoreV1Api api = new CoreV1Api(client);
V1PersistentVolumeClaimList list = null;
try{
list = api.listNamespacedPersistentVolumeClaim(namespace, null, null, null, null, null, null, null, null, null);
}catch(ApiException apie){
System.err.println("Exception when calling CoreV1Api#listNamespacedPersistentVolumeClaim");
apie.printStackTrace();
System.exit(1);
}
PVCWatch.printPVCs(list);
// parse watched events
System.out.format("%n----- PVC Watch (max total claims: %s) -----", maxClaims.toSuffixedString());
try {
Watch<V1PersistentVolumeClaim> watch = Watch.createWatch(
client,
api.listNamespacedPersistentVolumeClaimCall(
namespace, null, null, null, null, null, null, null, null, Boolean.TRUE, null, null),
new TypeToken<Watch.Response<V1PersistentVolumeClaim>>() {
}.getType()
);
// let's watch for total PVC sizes
for (Watch.Response<V1PersistentVolumeClaim> item : watch) {
V1PersistentVolumeClaim pvc = item.object;
String claimSize = null;
Quantity claimQuant = null;
BigDecimal totalNum = null;
switch (item.type){
case "ADDED":
claimSize = pvc.getSpec().getResources().getRequests().get("storage");
claimQuant = Quantity.fromString(claimSize);
totalNum = totalClaims.getNumber().add(claimQuant.getNumber());
totalClaims = new Quantity(totalNum, Format.BINARY_SI);
System.out.format(
"%nADDED: PVC %s added, size %s",
pvc.getMetadata().getName(), claimSize
);
// claim size overage ?
if (totalClaims.getNumber().compareTo(maxClaims.getNumber()) >= 1) {
System.out.format(
"%nWARNING: claim overage reached: max %s, at %s",
maxClaims.toSuffixedString(), totalClaims.toSuffixedString()
);
System.out.format("%n*** Trigger over capacity action ***");
}
break;
case "MODIFIED":
System.out.format("%nMODIFIED: PVC %s", pvc.getMetadata().getName());
break;
case "DELETED":
claimSize = pvc.getSpec().getResources().getRequests().get("storage");
claimQuant = Quantity.fromString(claimSize);
totalNum = totalClaims.getNumber().subtract(claimQuant.getNumber());
totalClaims = new Quantity(totalNum, Format.BINARY_SI);
System.out.format(
"%nDELETED: PVC %s removed, size %s",
pvc.getMetadata().getName(), claimSize
);
// size back to normal ?
if (totalClaims.getNumber().compareTo(maxClaims.getNumber()) <= 0) {
System.out.format(
"%nINFO: claim usage normal: max %s, at %s",
maxClaims.toSuffixedString(), totalClaims.toSuffixedString()
);
}
break;
}
System.out.format(
"%nINFO: Total PVC is at %4.1f%% capacity (%s/%s)",
(totalClaims.getNumber().floatValue()/maxClaims.getNumber().floatValue()) * 100,
totalClaims.toSuffixedString(),
maxClaims.toSuffixedString()
);
}
}catch(ApiException apie) {
System.err.println("Exception watching PersistentVolumeClaims");
apie.printStackTrace();
System.exit(1);
}
}
public static void printPVCs(V1PersistentVolumeClaimList list){
System.out.println("----- PVCs ----");
String template = "%-16s\t%-40s\t%-6s%n";
System.out.format(template,"Name", "Volume", "Size");
for (V1PersistentVolumeClaim item : list.getItems()) {
String name = item.getMetadata().getName();
String volumeName = item.getSpec().getVolumeName();
String size = item.getSpec().getResources().getRequests().get("storage");
System.out.format(template,name, volumeName, size);
}
}
}