Skip to content

Commit

Permalink
Fix sonarlint reports.
Browse files Browse the repository at this point in the history
Incorrect type hint.
Computing complexity for the watch function.
  • Loading branch information
perj committed Jan 18, 2024
1 parent cbaab8b commit d4d1730
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion k8s/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
import logging
from collections import namedtuple
from typing import Optional

import requests
import requests.packages.urllib3 as urllib3
Expand Down Expand Up @@ -180,7 +181,7 @@ def _watch_list_url(cls, namespace):
return url

@classmethod
def _parse_watch_event(cls, line) -> WatchBaseEvent:
def _parse_watch_event(cls, line) -> Optional[WatchBaseEvent]:
"""
Parse a line from the watch stream into a WatchEvent or WatchBookmark.
Raises APIServerError if the line is an error event.
Expand Down
21 changes: 13 additions & 8 deletions k8s/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,22 @@ def watch(self, namespace=None):
namespace=namespace, resource_version=last_seen_resource_version, allow_bookmarks=True
):
last_seen_resource_version = event.resource_version
if not event.has_object():
continue
o = event.object
key = (o.metadata.name, o.metadata.namespace)
if self._seen.get(key) == o.metadata.resourceVersion and event.type != WatchEvent.DELETED:
continue
self._seen[key] = o.metadata.resourceVersion
yield event
if self._should_yield(event):
yield event
except APIServerError as e:
# A 410 response indicates our resourceVersion is too old, and we need to do a new quorum read.
if e.api_error["code"] == 410:
last_seen_resource_version = None
else:
raise

def _should_yield(self, event) -> bool:
"""Check if this is a new event, and if so, mark it as seen"""
if not event.has_object():
return False
o = event.object
key = (o.metadata.name, o.metadata.namespace)
if self._seen.get(key) == o.metadata.resourceVersion and event.type != WatchEvent.DELETED:
return False
self._seen[key] = o.metadata.resourceVersion
return True

0 comments on commit d4d1730

Please sign in to comment.