-
Notifications
You must be signed in to change notification settings - Fork 1.9k
in_kubernetes_events: bound watch connection lifetime #12105
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
edsiper
wants to merge
2
commits into
master
Choose a base branch
from
kubernetes-events-hang-12019
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.
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
Jump to
Jump to file
Failed to load files.
Loading
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
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
138 changes: 138 additions & 0 deletions
138
tests/integration/scenarios/in_kubernetes_events/tests/test_in_kubernetes_events_001.py
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import contextlib | ||
| import http.server | ||
| import json | ||
| import os | ||
| import threading | ||
| import time | ||
|
|
||
| from utils.test_service import FluentBitTestService | ||
|
|
||
|
|
||
| class _KubeApiServer(http.server.ThreadingHTTPServer): | ||
| daemon_threads = True | ||
|
|
||
| def __init__(self, server_address, handler_class): | ||
| super().__init__(server_address, handler_class) | ||
| self.lock = threading.Lock() | ||
| self.stop_event = threading.Event() | ||
| self.list_requests = 0 | ||
| self.watch_requests = 0 | ||
| self.watch_paths = [] | ||
|
|
||
|
|
||
| class _KubeApiHandler(http.server.BaseHTTPRequestHandler): | ||
| protocol_version = "HTTP/1.1" | ||
|
|
||
| def do_GET(self): | ||
| if "watch=1" in self.path: | ||
| with self.server.lock: | ||
| self.server.watch_requests += 1 | ||
| self.server.watch_paths.append(self.path) | ||
|
|
||
| self.send_response(200) | ||
| self.send_header("Content-Type", "application/json") | ||
| self.send_header("Transfer-Encoding", "chunked") | ||
| self.end_headers() | ||
| self.wfile.flush() | ||
| self.server.stop_event.wait(timeout=30) | ||
| try: | ||
| self.wfile.write(b"0\r\n\r\n") | ||
| self.wfile.flush() | ||
| except (BrokenPipeError, ConnectionResetError): | ||
| pass | ||
| return | ||
|
|
||
| with self.server.lock: | ||
| self.server.list_requests += 1 | ||
| resource_version = self.server.list_requests | ||
|
|
||
| payload = json.dumps( | ||
| { | ||
| "kind": "EventList", | ||
| "apiVersion": "v1", | ||
| "metadata": {"resourceVersion": str(resource_version)}, | ||
| "items": [], | ||
| } | ||
| ).encode("utf-8") | ||
| self.send_response(200) | ||
| self.send_header("Content-Type", "application/json") | ||
| self.send_header("Content-Length", str(len(payload))) | ||
| self.end_headers() | ||
| self.wfile.write(payload) | ||
| self.wfile.flush() | ||
|
|
||
| def log_message(self, fmt, *args): | ||
| return | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _run_kube_api_server(): | ||
| server = _KubeApiServer(("127.0.0.1", 0), _KubeApiHandler) | ||
| thread = threading.Thread(target=server.serve_forever, daemon=True) | ||
| thread.start() | ||
|
|
||
| try: | ||
| yield server | ||
| finally: | ||
| server.stop_event.set() | ||
| server.shutdown() | ||
| server.server_close() | ||
| thread.join() | ||
|
|
||
|
|
||
| def _write_config(tmp_path, kube_api_port): | ||
| token_file = tmp_path / "token" | ||
| token_file.write_text("test-token", encoding="utf-8") | ||
| config_file = tmp_path / "kubernetes_events_watch_timeout.conf" | ||
| config_file.write_text( | ||
| "\n".join( | ||
| [ | ||
| "[SERVICE]", | ||
| " Flush 1", | ||
| " Grace 1", | ||
| " Log_Level info", | ||
| " HTTP_Server On", | ||
| " HTTP_Port ${FLUENT_BIT_HTTP_MONITORING_PORT}", | ||
| "", | ||
| "[INPUT]", | ||
| " Name kubernetes_events", | ||
| f" Kube_URL http://127.0.0.1:{kube_api_port}", | ||
| f" Kube_Token_File {token_file}", | ||
| " tls Off", | ||
| " Interval_Sec 2", | ||
| " Interval_NSec 0", | ||
| " Kube_Watch_Timeout 1s", | ||
| "", | ||
| "[OUTPUT]", | ||
| " Name null", | ||
| " Match *", | ||
| ] | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
| return config_file | ||
|
|
||
|
|
||
| def test_kubernetes_events_reconnects_stalled_watch(tmp_path): | ||
| with _run_kube_api_server() as kube_api_server: | ||
| config_file = _write_config(tmp_path, kube_api_server.server_address[1]) | ||
| service = FluentBitTestService(os.fspath(config_file)) | ||
| service.start() | ||
|
|
||
| try: | ||
| service.wait_for_condition( | ||
| lambda: kube_api_server.watch_requests | ||
| if kube_api_server.watch_requests >= 2 | ||
| else None, | ||
| timeout=10, | ||
| interval=0.25, | ||
| description="a reconnected Kubernetes event watch", | ||
| ) | ||
| kube_api_server.stop_event.set() | ||
| time.sleep(0.5) | ||
| finally: | ||
| service.stop() | ||
|
|
||
| assert kube_api_server.list_requests >= 2 | ||
| assert kube_api_server.watch_requests >= 2 | ||
| assert all("timeoutSeconds=1" in path for path in kube_api_server.watch_paths) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new default positive watch timeout, every watch request asks the API server to close after 600s, which makes the next collector cycle perform a full list again. In DB-less configurations, watched events are not used to advance
ctx->last_resource_version(it is only updated from list metadata incheck_and_init_stream()), so any events delivered by the watch and still retained by Kubernetes have resource versions greater than the stale filter and are emitted a second time on each forced relist. Please update the tracked resource version from watched events or avoid enabling periodic relists by default.Useful? React with 👍 / 👎.