Skip to content

Commit 3a75fbd

Browse files
committed
deleted unused code, updated state changes & reading in controller namespace
Signed-off-by: iripiri <[email protected]>
1 parent 879ae8e commit 3a75fbd

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

villas/controller/components/managers/kubernetes.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,34 @@ class KubernetesManager(Manager):
2222
def __init__(self, **args):
2323
super().__init__(**args)
2424

25-
self.thread_stop = threading.Event()
26-
27-
self.pod_watcher_thread = threading.Thread(
28-
target=self._run_pod_watcher)
29-
self.job_watcher_thread = threading.Thread(
30-
target=self._run_job_watcher)
31-
self.event_watcher_thread = threading.Thread(
32-
target=self._run_event_watcher)
33-
3425
if os.environ.get('KUBECONFIG'):
3526
k8s.config.load_kube_config()
3627
else:
3728
k8s.config.load_incluster_config()
3829

39-
self.namespace = args.get('namespace', 'default')
30+
# the namespace in which to create the jobs
31+
# and to watch for events
32+
self.namespace = os.environ.get('NAMESPACE')
33+
if self.namespace:
34+
self.namespace = ''.join([self.namespace, '-controller'])
35+
else:
36+
self.namespace = 'villas-controller'
4037

41-
self.my_namespace = os.environ.get('NAMESPACE')
38+
self._check_namespace(self.namespace)
39+
40+
# name and UID of the pod in which this controller is running
41+
# used in kubernetes simulator to set the owner reference
4242
self.my_pod_name = os.environ.get('POD_NAME')
4343
self.my_pod_uid = os.environ.get('POD_UID')
4444

45-
self._check_namespace(self.namespace)
45+
self.thread_stop = threading.Event()
4646

47-
# self.pod_watcher_thread.start()
48-
# self.job_watcher_thread.start()
47+
self.event_watcher_thread = threading.Thread(
48+
target=self._run_event_watcher)
4949
self.event_watcher_thread.setDaemon(True)
5050
self.event_watcher_thread.start()
5151

52+
5253
def _check_namespace(self, ns):
5354
c = k8s.client.CoreV1Api()
5455

@@ -59,28 +60,6 @@ def _check_namespace(self, ns):
5960

6061
raise RuntimeError(f'Namespace {ns} does not exist')
6162

62-
def _run_pod_watcher(self):
63-
w = k8s.watch.Watch()
64-
c = k8s.client.CoreV1Api()
65-
66-
for sts in w.stream(c.list_namespaced_pod,
67-
namespace=self.namespace):
68-
stso = sts.get('object')
69-
typ = sts.get('type')
70-
71-
self.logger.info('%s Pod: %s', typ, stso.metadata.name)
72-
73-
def _run_job_watcher(self):
74-
w = k8s.watch.Watch()
75-
b = k8s.client.BatchV1Api()
76-
77-
for sts in w.stream(b.list_namespaced_job,
78-
namespace=self.namespace):
79-
stso = sts.get('object')
80-
typ = sts.get('type')
81-
82-
self.logger.info('%s Job: %s', typ, stso.metadata.name)
83-
8463
def _run_event_watcher(self):
8564
while not self.thread_stop.is_set():
8665
w = k8s.watch.Watch()
@@ -107,6 +86,10 @@ def _run_event_watcher(self):
10786

10887
if _match(comp.job.metadata.name,
10988
eo.involved_object.name):
89+
if comp._state == 'stopping':
90+
# incoming events are old repetitions
91+
continue
92+
11093
if eo.reason == 'Completed':
11194
comp.change_state('stopping', True)
11295
elif eo.reason == 'Started':

villas/controller/components/simulators/kubernetes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ def start(self, payload):
194194
self.properties['job_name'] = self.job.metadata.name
195195
self.properties['namespace'] = self.manager.namespace
196196

197-
def stop(self, payload):
197+
def stop(self, message):
198+
self.change_state('stopping', True)
198199
self._delete_job()
199-
200200
self.change_state('idle')
201201

202202
def _send_signal(self, sig):
@@ -227,6 +227,7 @@ def resume(self, payload):
227227
self.change_state('running')
228228

229229
def reset(self, payload):
230+
self.change_state('resetting', True)
230231
self._delete_job()
231232
super().reset(payload)
232233

0 commit comments

Comments
 (0)