Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0945c8b

Browse filesBrowse files
committed
Add an example of watch recovery using resource_version and bookmarks
1 parent 16ffec4 commit 0945c8b
Copy full SHA for 0945c8b

File tree

1 file changed

+78
-0
lines changed
Filter options

1 file changed

+78
-0
lines changed

‎examples/watch/watch_recovery.py

Copy file name to clipboard
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright 2025 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Uses watch to print a stream of Pod events from the default namespace.
17+
The allow_watch_bookmarks flag is set to True, so the API server can send
18+
BOOKMARK events.
19+
20+
If the connection to the API server is lost, the script will reconnect and
21+
resume watching from the most recently received resource version.
22+
23+
For more information, see:
24+
- https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes
25+
- https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-watch
26+
"""
27+
28+
import urllib3
29+
30+
from kubernetes import config
31+
from kubernetes.client import api_client
32+
from kubernetes.client.exceptions import ApiException
33+
from kubernetes.dynamic.client import DynamicClient
34+
35+
NAMESPACE = "default"
36+
37+
38+
def main():
39+
# Configs can be set in Configuration class directly or using helper
40+
# utility. If no argument provided, the config will be loaded from
41+
# default location.
42+
config.load_kube_config()
43+
client = DynamicClient(api_client.ApiClient())
44+
api = client.resources.get(api_version="v1", kind="Pod")
45+
46+
# Setting resource_version=None means the server will send synthetic
47+
# ADDED events for all resources that exist when the watch starts.
48+
resource_version = None
49+
while True:
50+
try:
51+
for event in api.watch(
52+
namespace=NAMESPACE,
53+
resource_version=resource_version,
54+
allow_watch_bookmarks=True,
55+
):
56+
# Remember the last resourceVersion we saw, so we can resume
57+
# watching from there if the connection is lost.
58+
resource_version = event['object'].metadata.resourceVersion
59+
60+
print("Event: %s %s %s" % (
61+
resource_version,
62+
event['type'],
63+
event['object'].metadata.name,
64+
))
65+
66+
except ApiException as err:
67+
if err.status == 410:
68+
print("ERROR: The requested resource version is no longer available.")
69+
resource_version = None
70+
else:
71+
raise
72+
73+
except urllib3.exceptions.ProtocolError:
74+
print("Lost connection to the k8s API server. Reconnecting...")
75+
76+
77+
if __name__ == "__main__":
78+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.