Skip to content

Commit a1a36b1

Browse files
authored
Merge pull request kubernetes-client#1997 from venukarnati92/master
Dynamic Client Request Timeout Example
2 parents 13988e4 + 655b78c commit a1a36b1

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2023 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+
This example demonstrates the following:
17+
- Creation of a k8s configmap using dynamic-client
18+
- Setting the request timeout which is time duration in seconds
19+
"""
20+
21+
from kubernetes import config, dynamic
22+
from kubernetes.client import api_client
23+
24+
25+
def main():
26+
# Creating a dynamic client
27+
client = dynamic.DynamicClient(
28+
api_client.ApiClient(configuration=config.load_kube_config())
29+
)
30+
31+
# fetching the configmap api
32+
api = client.resources.get(api_version="v1", kind="ConfigMap")
33+
34+
configmap_name = "request-timeout-test-configmap"
35+
36+
configmap_manifest = {
37+
"kind": "ConfigMap",
38+
"apiVersion": "v1",
39+
"metadata": {
40+
"name": configmap_name,
41+
"labels": {
42+
"foo": "bar",
43+
},
44+
},
45+
"data": {
46+
"config.json": '{"command":"/usr/bin/mysqld_safe"}',
47+
"frontend.cnf": "[mysqld]\nbind-address = 10.0.0.3\n",
48+
},
49+
}
50+
51+
# Creating configmap `request-timeout-test-configmap` in the `default` namespace
52+
# Client-side timeout to 60 seconds
53+
54+
configmap = api.create(body=configmap_manifest, namespace="default", _request_time=60)
55+
56+
print("\n[INFO] configmap `request-timeout-test-configmap` created\n")
57+
58+
# Listing the configmaps in the `default` namespace
59+
# Client-side timeout to 60 seconds
60+
61+
configmap_list = api.get(
62+
name=configmap_name, namespace="default", label_selector="foo=bar", _request_time=60
63+
)
64+
65+
print("NAME:\n%s\n" % (configmap_list.metadata.name))
66+
print("DATA:\n%s\n" % (configmap_list.data))
67+
68+
69+
if __name__ == "__main__":
70+
main()

examples/watch/timeout-settings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ There are two inputs available in the client, that could be used to set connecti
5656
***Refer***
5757
- *[https://github.com/kubernetes-client/python/blob/v17.17.0/kubernetes/client/api_client.py#L336-L339](https://github.com/kubernetes-client/python/blob/v17.17.0/kubernetes/client/api_client.py#L336-L339)*
5858

59+
***Example***
60+
- *[request_timeout.py](../dynamic-client/request_timeout.py)*
61+
5962
- In case of network outage, leading to dropping all packets with no RST/FIN, the timeout value (in seconds) determined by the `request_timeout` argument, would be the time duration for how long the client will wait before dropping the connection.
6063

6164
- When the timeout happens, an exception will be raised, for eg. ~

0 commit comments

Comments
 (0)