### Python Example: Get API Resources Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/SchedulingV1alpha2Api.md Demonstrates how to retrieve a list of available API resources using the Kubernetes Python client. This example includes setup for API key authentication. ```python from __future__ import print_function import time import kubernetes.client from kubernetes.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.client.Configuration( host = "http://localhost" ) # The kubernetes.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an open api client # api_client = kubernetes.client.ApiClient(configuration) # api = kubernetes.client.CoreV1Api(api_client) try: # get available resources api_response = kubernetes.client.ApiClient(configuration).call_api( '/api/v1/api-resources', 'GET', path_params={}, query_params={}, header_params={}, body={}, response_type='V1APIResourceList', auth_settings=['BearerToken']) pprint(api_response) except ApiException as e: print("Exception when calling CoreV1Api->get_api_resources: %s\n" % e) ``` -------------------------------- ### Initialize NetworkingV1beta1Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/NetworkingV1beta1Api.md Demonstrates how to initialize the NetworkingV1beta1Api client with a configuration. ```python with kubernetes.client.ApiClient(configuration) as api_client: api_instance = kubernetes.client.NetworkingV1beta1Api(api_client) ``` -------------------------------- ### Initialize and Use ResourceV1beta2Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/ResourceV1beta2Api.md Demonstrates how to set up an ApiClient and instantiate the ResourceV1beta2Api for asynchronous operations. This is the starting point for interacting with the API. ```python with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.ResourceV1beta2Api(api_client) ``` -------------------------------- ### Install Kubernetes Python Client Source: https://github.com/kubernetes-client/python/blob/master/examples/README.md Install the Kubernetes Python client using pip. This is a prerequisite for running the provided examples. ```bash pip install kubernetes ``` -------------------------------- ### Initialize Kubernetes API Client Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/StoragemigrationV1beta1Api.md Demonstrates how to initialize the Kubernetes API client and create an instance of the StoragemigrationV1beta1Api. ```python from kubernetes import client, config config.load_kube_config() # Enter a context with an instance of the API kubernetes.client with client.ApiClient() as api_client: # Create an instance of the API class api_instance = client.StoragemigrationV1beta1Api(api_client) print(api_instance) ``` -------------------------------- ### Get API Resources Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AutoscalingV2Api.md Retrieves a list of available API resources using the AutoscalingV2 API. This is a basic example demonstrating API client initialization and a simple GET request. ```python from __future__ import print_function import time import kubernetes.client from kubernetes.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.client.Configuration( host = "http://localhost" ) # Enter a context with an instance of the API kubernetes.client with kubernetes.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.client.AutoscalingV2Api(api_client) try: api_response = api_instance.get_api_resources() pprint(api_response) except ApiException as e: print("Exception when calling AutoscalingV2Api->get_api_resources: %s\n" % e) ``` -------------------------------- ### Initialize Kubernetes API Client and Create AdmissionregistrationV1beta1Api Instance Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/AdmissionregistrationV1beta1Api.md Demonstrates how to set up the asynchronous Kubernetes client and instantiate the AdmissionregistrationV1beta1Api. This is the starting point for making calls to the AdmissionregistrationV1beta1 API. ```python import kubernetes.aio.client import kubernetes.client # Assuming 'configuration' is already set up # configuration = kubernetes.client.Configuration() async with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.AdmissionregistrationV1beta1Api(api_client) # Now you can use api_instance to make calls, e.g.: # response = await api_instance.list_validating_webhook_configuration() # print(response) pass ``` -------------------------------- ### Patch ClusterRoleBinding Example Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/RbacAuthorizationV1Api.md Demonstrates how to partially update a specified ClusterRoleBinding using the Kubernetes Python client. This example utilizes BearerToken authentication and shows the necessary setup for API key configuration. ```python from __future__ import print_function import time import kubernetes.client from kubernetes.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.client.Configuration( host = "http://localhost" ) # The kubernetes.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an existing ClusterRoleBinding, or create a new ClusterRoleBinding # to test the API. For example, you can use the following JSON object to create a ClusterRoleBinding: # { # "apiVersion": "rbac.authorization.k8s.io/v1", # "kind": "ClusterRoleBinding", # "metadata": { # "name": "my-cluster-role-binding" # }, # "subjects": [ # { # "kind": "User", # "name": "my-user", # "apiGroup": "rbac.authorization.k8s.io" # } # ], # "roleRef": { # "kind": "ClusterRole", # "name": "my-cluster-role", # "apiGroup": "rbac.authorization.k8s.io" # } # } # Assuming you have a ClusterRoleBinding object named 'my-cluster-role-binding' # and a partial update body defined as 'body'. # For example: # body = { # "subjects": [ # { # "kind": "User", # "name": "another-user", # "apiGroup": "rbac.authorization.k8s.io" # } # ] # } try: # Create an instance of the API class api_instance = kubernetes.client.RbacAuthorizationV1Api(kubernetes.client.ApiClient(configuration)) name = 'my-cluster-role-binding' # str | name of the ClusterRoleBinding body = { ``` -------------------------------- ### Connect to Node Proxy with Path (GET) Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/CoreV1Api.md This example demonstrates establishing a GET connection to a node's proxy, including a specific path. It requires API key authentication and proper configuration of the ApiClient. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an instance of the API kubernetes.aio.client with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.CoreV1Api(api_client) name = 'name_example' # str | name of the NodeProxyOptions path = 'path_example' # str | path to the resource path2 = 'path_example' # str | Path is the URL path to use for the current proxy request to node. (optional) try: api_response = api_instance.connect_get_node_proxy_with_path(name, path, path2=path2) pprint(api_response) except ApiException as e: print("Exception when calling CoreV1Api->connect_get_node_proxy_with_path: %s\n" % e) ``` -------------------------------- ### List Resources with ResourceV1beta2Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/ResourceV1beta2Api.md Demonstrates how to instantiate the ResourceV1beta2Api and list resources. It shows how to use parameters like allow_watch_bookmarks, continue, field_selector, label_selector, limit, pretty, resource_version, and resource_version_match for filtering and pagination. ```python from kubernetes import client, config # Assuming configuration is loaded, e.g., config.load_kube_config() # configuration = config.Configuration() # Enter a context with an instance of the API kubernetes.client with client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = client.ResourceV1beta2Api(api_client) allow_watch_bookmarks = True # bool | allowWatchBookmarks requests watch events with type "BOOKMARK". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. (optional) _continue = '_continue_example' # str | The continue option should be set when retrieving more results from the server. Since this value is server defined, kubernetes.clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the kubernetes.client needs a consistent list, it must restart their list without the continue field. Otherwise, the kubernetes.client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the "next key". This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. (optional) field_selector = 'field_selector_example' # str | A selector to restrict the list of returned objects by their fields. Defaults to everything. (optional) label_selector = 'label_selector_example' # str | A selector to restrict the list of returned objects by their labels. Defaults to everything. (optional) limit = 56 # int | limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and kubernetes.clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, kubernetes.clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a kubernetes.client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. (optional) pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). (optional) resource_version = 'resource_version_example' # str | resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) resource_version_match = 'resource_version_match_example' # str | resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) try: # Example for a specific list method, adjust as needed for actual API calls # api_response = api_instance.list_namespaced_pod(limit=limit, _continue=_continue, field_selector=field_selector, label_selector=label_selector, pretty=pretty, resource_version=resource_version, resource_version_match=resource_version_match, allow_watch_bookmarks=allow_watch_bookmarks) # print(api_response) pass except client.ApiException as e: print("Exception when calling ResourceV1beta2Api->list_namespaced_pod: %s\n" % e) ``` -------------------------------- ### List AdmissionregistrationV1Api Resources with Options Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AdmissionregistrationV1Api.md Demonstrates how to instantiate the AdmissionregistrationV1Api and list resources using various query parameters like pretty printing, watch bookmarks, continue token, field/label selectors, limit, and resource versions. ```python from kubernetes import client, config # Load Kubernetes configuration config.load_kube_config() # Enter a context with an instance of the API kubernetes.client with client.ApiClient() as api_client: # Create an instance of the API class api_instance = client.AdmissionregistrationV1Api(api_client) pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). (optional) allow_watch_bookmarks = True # bool | allowWatchBookmarks requests watch events with type "BOOKMARK". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. (optional) _continue = '_continue_example' # str | The continue option should be set when retrieving more results from the server. Since this value is server defined, kubernetes.clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the kubernetes.client needs a consistent list, it must restart their list without the continue field. Otherwise, the kubernetes.client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the "next key". This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. (optional) field_selector = 'field_selector_example' # str | A selector to restrict the list of returned objects by their fields. Defaults to everything. (optional) label_selector = 'label_selector_example' # str | A selector to restrict the list of returned objects by their labels. Defaults to everything. (optional) limit = 56 # int | limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and kubernetes.clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, kubernetes.clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a kubernetes.client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. (optional) resource_version = 'resource_version_example' # str | resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) resource_version_match = 'resource_version_match_example' # str | resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) # Example of calling the list method (specific method depends on the resource) # For example, to list ValidatingWebhookConfigurations: # api_response = api_instance.list_validating_webhook_configuration( # pretty=pretty, # allow_watch_bookmarks=allow_watch_bookmarks, # _continue=_continue, # field_selector=field_selector, # label_selector=label_selector, # limit=limit, # resource_version=resource_version, # resource_version_match=resource_version_match # ) # print(api_response) pass ``` -------------------------------- ### Rule Scoped to a String Value Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/V1ValidationRule.md Example of a validation rule for a string, checking if it starts with a specific prefix. ```yaml rule: self.startsWith('kube') ``` -------------------------------- ### List AdmissionregistrationV1Api Resources with Options Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AdmissionregistrationV1Api.md Demonstrates how to instantiate the AdmissionregistrationV1Api and list resources using various query parameters like pretty printing, watch bookmarks, continue token, field/label selectors, limit, and resource versions. ```python from kubernetes import client, config # Load Kubernetes configuration config.load_kube_config() # Enter a context with an instance of the API kubernetes.client with client.ApiClient() as api_client: # Create an instance of the API class api_instance = client.AdmissionregistrationV1Api(api_client) pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). (optional) allow_watch_bookmarks = True # bool | allowWatchBookmarks requests watch events with type "BOOKMARK". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored. (optional) _continue = '_continue_example' # str | The continue option should be set when retrieving more results from the server. Since this value is server defined, kubernetes.clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the kubernetes.client needs a consistent list, it must restart their list without the continue field. Otherwise, the kubernetes.client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the "next key". This field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications. (optional) field_selector = 'field_selector_example' # str | A selector to restrict the list of returned objects by their fields. Defaults to everything. (optional) label_selector = 'label_selector_example' # str | A selector to restrict the list of returned objects by their labels. Defaults to everything. (optional) limit = 56 # int | limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and kubernetes.clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, kubernetes.clients may assume that no more results are available. This field is not supported if watch is true. The server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a kubernetes.client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned. (optional) resource_version = 'resource_version_example' # str | resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) resource_version_match = 'resource_version_match_example' # str | resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details. Defaults to unset (optional) # Example of calling the list method (specific method depends on the resource being listed) # For example, to list ValidatingWebhookConfigurations: # api_response = api_instance.list_validating_webhook_configuration( # pretty=pretty, # allow_watch_bookmarks=allow_watch_bookmarks, # _continue=_continue, # field_selector=field_selector, # label_selector=label_selector, # limit=limit, # resource_version=resource_version, # resource_version_match=resource_version_match # ) # print(api_response) pass ``` -------------------------------- ### Run Leader Election Example Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/base/leaderelection/README.md Execute this command in multiple terminals to start leader election processes. Each process will attempt to become the leader. ```bash python example.py ``` -------------------------------- ### Get API Resources Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/CertificatesV1alpha1Api.md Retrieves a list of available API resources. This example demonstrates setting up the API client with BearerToken authentication and making the call. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an instance of the API kubernetes.aio.client with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.CertificatesV1alpha1Api(api_client) try: api_response = api_instance.get_api_resources() pprint(api_response) except ApiException as e: print("Exception when calling CertificatesV1alpha1Api->get_api_resources: %s\n" % e) ``` -------------------------------- ### Initialize StorageV1Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/StorageV1Api.md Demonstrates how to initialize the StorageV1Api client within a context manager. ```python with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.StorageV1Api(api_client) ``` -------------------------------- ### Read Namespace Example Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/CoreV1Api.md Demonstrates how to read a specific Kubernetes Namespace using the CoreV1Api. Includes setup for API key authentication and error handling. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an instance of the API kubernetes.aio.client with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.CoreV1Api(api_client) name = 'name_example' # str | name of the Namespace pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget). (optional) try: api_response = api_instance.read_namespace(name, pretty=pretty) pprint(api_response) except ApiException as e: print("Exception when calling CoreV1Api->read_namespace: %s\n" % e) ``` -------------------------------- ### Initialize ResourceV1beta2Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/ResourceV1beta2Api.md Demonstrates how to initialize the ResourceV1beta2Api with an ApiClient. This is the standard setup for using the API. ```python import kubernetes.aio.client # Assuming 'configuration' is already set up with kubernetes.aio.client.ApiClient(configuration) as api_client: api_instance = kubernetes.aio.client.ResourceV1beta2Api(api_client) # Now you can use api_instance to make calls ``` -------------------------------- ### Install Kubernetes Python Client from Source Source: https://github.com/kubernetes-client/python/blob/master/README.md Clone the repository and install the client using pip. ```shell git clone --recursive https://github.com/kubernetes-client/python.git cd python python -m pip install --upgrade . ``` -------------------------------- ### Get Available API Resources Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AdmissionregistrationV1alpha1Api.md Retrieves a list of available API resources using the AdmissionregistrationV1alpha1Api. This example demonstrates Bearer Token authentication and error handling. ```python from __future__ import print_function import time import kubernetes.client from kubernetes.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.client.Configuration( host = "http://localhost" ) # The kubernetes.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an instance of the API kubernetes.client with kubernetes.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.client.AdmissionregistrationV1alpha1Api(api_client) try: api_response = api_instance.get_api_resources() pprint(api_response) except ApiException as e: print("Exception when calling AdmissionregistrationV1alpha1Api->get_api_resources: %s\n" % e) ``` -------------------------------- ### Get Available API Resources Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/CoordinationV1beta1Api.md Retrieves a list of available API resources from the Kubernetes API server. This example demonstrates using Bearer Token authentication. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an open client session to call the API async with kubernetes.aio.client.ApiClient(configuration) as api_client: api = kubernetes.aio.client.CoordinationV1beta1Api(api_client) try: print("Calling get_api_resources...") response = await api.get_api_resources() pprint(response) print("get_api_resources returned: %s" % response) except ApiException as e: print("Exception when calling CoordinationV1beta1Api->get_api_resources: %s\n" % e) ``` -------------------------------- ### Initialize StoragemigrationV1beta1Api Client Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/StoragemigrationV1beta1Api.md Demonstrates how to initialize the StoragemigrationV1beta1Api client within an ApiClient context. This is the standard way to begin interacting with the API. ```python with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.StoragemigrationV1beta1Api(api_client) ``` -------------------------------- ### Initialize ResourceV1beta2Api Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/ResourceV1beta2Api.md Demonstrates how to initialize the ResourceV1beta2Api client within a context manager. ```python with kubernetes.client.ApiClient(configuration) as api_client: api_instance = kubernetes.client.ResourceV1beta2Api(api_client) ``` -------------------------------- ### Get API Group Information Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/CoordinationApi.md Retrieves information about the coordination API group. This example shows how to configure the client with API key authentication and make the call. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an instance of the API kubernetes.aio.client with kubernetes.aio.client.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = kubernetes.aio.client.CoordinationApi(api_client) try: api_response = api_instance.get_api_group() pprint(api_response) except ApiException as e: print("Exception when calling CoordinationApi->get_api_group: %s\n" % e) ``` -------------------------------- ### Delete StorageClass Example Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/docs/StorageV1Api.md Demonstrates how to delete a StorageClass using the Kubernetes Python client. It shows the setup for API key authentication and making the delete request. ```python from __future__ import print_function import time import kubernetes.aio.client from kubernetes.aio.client.rest import ApiException from pprint import pprint # Defining the host is optional and defaults to http://localhost # See configuration.py for a list of all supported configuration parameters. configuration = kubernetes.aio.client.Configuration( host = "http://localhost" ) # The kubernetes.aio.client must configure the authentication and authorization parameters # in accordance with the API server security policy. # Examples for each auth method are provided below, use the example that # satisfies your auth use case. # Configure API key authorization: BearerToken configuration.api_key['BearerToken'] = 'YOUR_API_KEY' # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed # configuration.api_key_prefix['BearerToken'] = 'Bearer' # Enter a context with an open api client api_client = kubernetes.aio.client.ApiClient(configuration) # create an instance of the API class api_instance = kubernetes.aio.client.StorageV1Api(api_client) # name of the StorageClass name = "name_example" try: # delete a StorageClass api_response = await api_instance.delete_storage_class(name) pprint(api_response) except ApiException as e: print("Exception when calling StorageV1Api->delete_storage_class: %s\n" % e) ``` -------------------------------- ### Install Kubernetes AIO Client via Setuptools Source: https://github.com/kubernetes-client/python/blob/master/kubernetes/aio/README.md Install the Python package using Setuptools. This method is suitable for local installations or when building from source. ```sh python setup.py install --user ```