From f0a73c8824902626c23815aec442b5aa60fa72b6 Mon Sep 17 00:00:00 2001 From: Raj Bhargav <72274012+p172913@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:15:44 +0530 Subject: [PATCH] Update watch.py Changes made in unmarshal_event for not having issues with empty lines. --- kubernetes/base/watch/watch.py | 45 +++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/kubernetes/base/watch/watch.py b/kubernetes/base/watch/watch.py index 43c6ba2c4..22c8e9084 100644 --- a/kubernetes/base/watch/watch.py +++ b/kubernetes/base/watch/watch.py @@ -82,7 +82,7 @@ def iter_resp_lines(resp): last_was_empty = False # Reset empty-line flag else: if not last_was_empty: - yield '\n' # Only print one empty line + yield '' # Only print one empty line last_was_empty = True # Mark that we handled an empty line next_newline = buffer.find(b'\n') @@ -113,24 +113,29 @@ class Watch(object): return 'watch' def unmarshal_event(self, data, return_type): - js = json.loads(data) - js['raw_object'] = js['object'] - # BOOKMARK event is treated the same as ERROR for a quick fix of - # decoding exception - # TODO: make use of the resource_version in BOOKMARK event for more - # efficient WATCH - if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK': - obj = SimpleNamespace(data=json.dumps(js['raw_object'])) - js['object'] = self._api_client.deserialize(obj, return_type) - if hasattr(js['object'], 'metadata'): - self.resource_version = js['object'].metadata.resource_version - # For custom objects that we don't have model defined, json - # deserialization results in dictionary - elif (isinstance(js['object'], dict) and 'metadata' in js['object'] - and 'resourceVersion' in js['object']['metadata']): - self.resource_version = js['object']['metadata'][ - 'resourceVersion'] - return js + if not data or data.isspace(): + return None + try: + js = json.loads(data) + js['raw_object'] = js['object'] + # BOOKMARK event is treated the same as ERROR for a quick fix of + # decoding exception + # TODO: make use of the resource_version in BOOKMARK event for more + # efficient WATCH + if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK': + obj = SimpleNamespace(data=json.dumps(js['raw_object'])) + js['object'] = self._api_client.deserialize(obj, return_type) + if hasattr(js['object'], 'metadata'): + self.resource_version = js['object'].metadata.resource_version + # For custom objects that we don't have model defined, json + # deserialization results in dictionary + elif (isinstance(js['object'], dict) and 'metadata' in js['object'] + and 'resourceVersion' in js['object']['metadata']): + self.resource_version = js['object']['metadata'][ + 'resourceVersion'] + return js + except json.JSONDecodeError: + return None def stream(self, func, *args, **kwargs): """Watch an API resource and stream the result back via a generator. @@ -209,7 +214,7 @@ class Watch(object): yield line # Normal non-empty line last_was_empty = False elif not last_was_empty: - yield '/n' # Only yield one empty line + yield '' # Only yield one empty line last_was_empty = True if self._stop: break