-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathpvc_watch.py
More file actions
75 lines (57 loc) · 2.47 KB
/
pvc_watch.py
File metadata and controls
75 lines (57 loc) · 2.47 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
import os
import pint
from kubernetes import client, config, watch
def main():
# setup the namespace
ns = os.getenv("K8S_NAMESPACE")
if ns is None:
ns = ""
# use package pint to handle volume quantities
unit = pint.UnitRegistry()
unit.define('gibi = 2**30 = Gi')
max_claims = unit.Quantity("150Gi")
total_claims = unit.Quantity("0Gi")
# configure client
config.load_kube_config()
api = client.CoreV1Api()
# Print PVC list
pvcs = api.list_namespaced_persistent_volume_claim(namespace=ns, watch=False)
print("")
print("---- PVCs ---")
print("%-16s\t%-40s\t%-6s" % ("Name", "Volume", "Size"))
for pvc in pvcs.items:
print("%-16s\t%-40s\t%-6s" %
(pvc.metadata.name, pvc.spec.volume_name, pvc.spec.resources.requests['storage']))
print("")
# setup watch
w = watch.Watch()
for item in w.stream(api.list_namespaced_persistent_volume_claim, namespace=ns, timeout_seconds=0):
pvc = item['object']
# parse PVC events
# new PVC added
if item['type'] == 'ADDED':
size = pvc.spec.resources.requests['storage']
claimQty = unit.Quantity(size)
total_claims = total_claims + claimQty
print("PVC Added: %s; size %s" % (pvc.metadata.name, size))
if total_claims >= max_claims:
print("---------------------------------------------")
print("WARNING: claim overage reached; max %s; at %s" % (max_claims, total_claims))
print("**** Trigger over capacity action ***")
print("---------------------------------------------")
# PVC is removed
if item['type'] == 'DELETED':
size = pvc.spec.resources.requests['storage']
claimQty = unit.Quantity(size)
total_claims = total_claims - claimQty
print("PVC Deleted: %s; size %s" % (pvc.metadata.name, size))
if total_claims <= max_claims:
print("---------------------------------------------")
print("INFO: claim usage normal; max %s; at %s" % (max_claims, total_claims))
print("---------------------------------------------")
# PVC is UPDATED
if item['type'] == "MODIFIED":
print("MODIFIED: %s" % (pvc.metadata.name))
print("INFO: total PVC at %4.1f%% capacity" % ((total_claims/max_claims)*100))
if __name__ == '__main__':
main()