-
Notifications
You must be signed in to change notification settings - Fork 5
/
swarmkubecache.py
executable file
·51 lines (40 loc) · 1.69 KB
/
swarmkubecache.py
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
import subprocess
import json
import time
from threading import Event
class SwarmKubeCache:
"""
Periodically fetch swarm-related kube-api objects and store them in a cache.
This gives swarm agents an in-memory cache of the kube-api objects, to avoid each agent
blasting the kube-api endpoint with requests (and consuming a lot of memory, CPU, and network
resources in the process).
"""
def __init__(self, done: Event):
self.cache = {
"agentclusterinstalls": {},
"baremetalhosts": {},
"infraenvs": {},
}
self.done = done
def get_infraenv(self, name, namespace):
return self.cache["infraenvs"].get(f"{namespace}/{name}", None)
def get_agent_cluster_install(self, name, namespace):
return self.cache["agentclusterinstalls"].get(f"{namespace}/{name}", None)
def get_baremetalhost(self, name, namespace):
return self.cache["baremetalhosts"].get(f"{namespace}/{name}", None)
def cache_api_type(self, api_type):
"""
Cache all the kube-api objects of a given type.
"""
result = json.loads(subprocess.check_output(["oc", "get", api_type, "-A", "-ojson"]).decode("utf-8"))
for api_object in result["items"]:
self.cache[api_type][f"{api_object['metadata']['namespace']}/{api_object['metadata']['name']}"] = api_object
def monitor(self):
while not self.done.is_set():
for api_type in self.cache:
try:
self.cache_api_type(api_type)
except Exception:
# API is imperfect, this is okay, just try again later
pass
time.sleep(5)