Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.5k
Add health check to watch.stream for silent connection drops#2527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Urvashi0109
wants to merge
2
commits into
kubernetes-client:masterChoose a base branch
from
Urvashi0109:Fix-Added-Watch-Health-Check
base:master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Uh oh!
There was an error while loading. Please reload this page.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,9 @@ | ||
| import json | ||
| import pydoc | ||
| import sys | ||
| import time | ||
| from urllib3.exceptions import ProtocolError, ReadTimeoutError | ||
| from kubernetes import client | ||
| @@ -154,6 +157,14 @@ def stream(self, func, *args, **kwargs): | ||
| :param func: The API function pointer. Any parameter to the function | ||
| can be passed after this parameter. | ||
| :param int _health_check_interval: Optional. Number of seconds to wait | ||
| for data before assuming the connection has been silently dropped | ||
| (e.g., during a control plane upgrade). When set to a value > 0, | ||
| the watch will automatically detect silent connection drops and | ||
| reconnect using the last known resource_version. Default is 0 | ||
| (disabled). This is useful for long-running watches that need to | ||
| survive cluster upgrades. | ||
| :return: Event object with these keys: | ||
| 'type': The type of event such as "ADDED", "DELETED", etc. | ||
| 'raw_object': a dict representing the watched object. | ||
| @@ -172,6 +183,11 @@ def stream(self, func, *args, **kwargs): | ||
| ... | ||
| if should_stop: | ||
| watch.stop() | ||
| Example with health check (survives control plane upgrades): | ||
| for e in watch.stream(v1.list_namespace, _health_check_interval=30): | ||
| # If no data received for 30s, watch reconnects automatically | ||
| print(e['type'], e['object'].metadata.name) | ||
| """ | ||
| self._stop = False | ||
| @@ -187,6 +203,20 @@ def stream(self, func, *args, **kwargs): | ||
| disable_retries = ('timeout_seconds' in kwargs) | ||
| retry_after_410 = False | ||
| deserialize = kwargs.pop('deserialize', True) | ||
| # Health check interval: when > 0, sets a read timeout on the | ||
| # HTTP connection so that silent connection drops (e.g., during | ||
| # control plane upgrades) are detected and the watch reconnects. | ||
| # A value of 0 (default) disables this feature. | ||
| health_check_interval = kwargs.pop('_health_check_interval', 0) | ||
| # If health check is enabled and user hasn't set an explicit | ||
| # request timeout, use the health check interval as the read | ||
| # timeout. This causes urllib3 to raise ReadTimeoutError if no | ||
| # data arrives within the interval, which we catch below. | ||
| if health_check_interval > 0 and '_request_timeout' not in kwargs: | ||
| kwargs['_request_timeout'] = (None, health_check_interval) | ||
| while True: | ||
| resp = func(*args, **kwargs) | ||
| try: | ||
| @@ -217,12 +247,26 @@ def stream(self, func, *args, **kwargs): | ||
| retry_after_410 = False | ||
| yield event | ||
| else: | ||
| if line: | ||
| if line: | ||
| yield line # Normal non-empty line | ||
| else: | ||
| yield '' # Only yield one empty line | ||
| else: | ||
| yield '' # Only yield one empty line | ||
| if self._stop: | ||
| break | ||
| except (ReadTimeoutError, ProtocolError) as e: | ||
| # Only treat a read timeout / protocol error as a silent connection drop | ||
| should_retry = ( | ||
| health_check_interval > 0 | ||
| and not disable_retries | ||
| and watch_arg == "watch" | ||
| and self.resource_version is not None | ||
| ) | ||
| if should_retry: | ||
| # Add a small sleep to avoid a tight reconnect loop | ||
| # in case the endpoint is hard-down or errors immediately. | ||
| time.sleep(min(1.0, health_check_interval)) | ||
| else: | ||
Urvashi0109 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| raise | ||
Urvashi0109 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| finally: | ||
| resp.close() | ||
| resp.release_conn() | ||
| @@ -232,4 +276,4 @@ def stream(self, func, *args, **kwargs): | ||
| self._stop = True | ||
| if self._stop or disable_retries: | ||
| break | ||
| break | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.