|
| 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() |
0 commit comments