Skip to content

Commit

Permalink
[#479] Remove redundadnt k8s properties changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillmakhonin committed Sep 28, 2018
1 parent 1fc8bfc commit 24faf3e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions legion/legion/k8s/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ def _read_data_from_dict(self, source_dict):
:param source_dict: dict with readed values
:type source_dict: dict[str, str]
:return: None
:return: bool -- is new data has been received
"""
data_has_been_changed = self._state != source_dict
self._state = source_dict
return data_has_been_changed

def _write_data_to_dict(self):
"""
Expand Down Expand Up @@ -333,21 +335,23 @@ def load(self):
"""
Load data from K8S
:return: None
:return: bool -- is new data has been received
"""
data_has_been_changed = False
try:
LOGGER.debug('Reading {} {!r} in namespace {!r}'.format(self.__class__.__name__,
self.k8s_name,
self.k8s_namespace_or_default))

config_map_object = self._read_k8s_resource()

self._read_data_from_dict(config_map_object.data)
data_has_been_changed = self._read_data_from_dict(config_map_object.data)
except Exception as load_exception:
self._read_k8s_resource_exception_handler(load_exception)

self._last_load_time = time.time()
self._saved = True
return data_has_been_changed

def _check_and_reload(self):
"""
Expand Down Expand Up @@ -446,9 +450,14 @@ def watch(self):
LOGGER.info('Creating watch for object {!r}'.format(self))
watch = self._build_k8s_resource_watch()
for (event_type, event_object) in watch.stream:
LOGGER.info('Watch got new event. Type = {}'.format(event_type))
self.load()
yield (event_type, self.data)
LOGGER.info('Watch got new event. Type = {!r}'.format(event_type))
data_has_been_changed = self.load()

if data_has_been_changed:
LOGGER.debug('Issuing properties change event {!r}'.format(event_type))
yield (event_type, self.data)
else:
LOGGER.warning('Ignoring new properties change event {!r} without real data update'.format(event_type))

def _call_callback(self):
"""
Expand Down Expand Up @@ -655,12 +664,15 @@ def _read_data_from_dict(self, source_dict):
:param source_dict: dict with readed values
:type source_dict: dict[str, str]
:return: None
:return: bool -- is new data has been received
"""
self._state = {
new_data = {
k: base64.b64decode(v.encode('ascii')).decode('utf-8')
for k, v in source_dict.items()
}
data_has_been_changed = self._state != new_data
self._state = new_data
return data_has_been_changed

def _write_data_to_dict(self):
"""
Expand Down

0 comments on commit 24faf3e

Please sign in to comment.