Retrieving Lists of Resources
The Kubernetes Python client offers methods for retrieving lists of resources. These methods are designed to be efficient and flexible, allowing you to retrieve specific sets of resources based on your needs.
Limiting the Number of Results
You can use the limit
parameter to control the maximum number of items returned in a list operation.
Parameter: limit
(int)
Description: Specifies the 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. The continue
field should only be used 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, you can assume that no more results are available. This field is not supported if watch
is true.
Example:
from kubernetes import client, config
# Load the Kubernetes configuration
config.load_kube_config()
# Create an instance of the API client
api = client.NetworkingV1Api()
# Retrieve a list of Ingress resources with a limit of 5
ingresses = api.list_namespaced_ingress(namespace="default", limit=5)
# Print the number of Ingress resources retrieved
print(f"Number of Ingress resources: {len(ingresses.items)}")
Source:
Pagination with _continue
The _continue
parameter is used to retrieve the next set of results in a paginated list operation.
Parameter: _continue
(str)
Description: Specifies the continuation token for paginated results.
Example:
from kubernetes import client, config
# Load the Kubernetes configuration
config.load_kube_config()
# Create an instance of the API client
api = client.NetworkingV1Api()
# Retrieve the first page of Ingress resources
ingresses = api.list_namespaced_ingress(namespace="default", limit=5)
# Check if there are more results
if ingresses.metadata.continue_:
# Retrieve the next page of results using the continuation token
next_page = api.list_namespaced_ingress(namespace="default", limit=5, _continue=ingresses.metadata.continue_)
# Process the next page of results
print(f"Number of Ingress resources on the next page: {len(next_page.items)}")
Source:
Filtering Resources
You can filter the list of resources using the field_selector
and label_selector
parameters.
Parameter: field_selector
(str)
Description: A selector to restrict the list of returned objects by their fields. Defaults to everything.
Parameter: label_selector
(str)
Description: A selector to restrict the list of returned objects by their labels. Defaults to everything.
Example:
from kubernetes import client, config
# Load the Kubernetes configuration
config.load_kube_config()
# Create an instance of the API client
api = client.NetworkingV1Api()
# Retrieve a list of Ingress resources with a specific label
ingresses = api.list_namespaced_ingress(namespace="default", label_selector="app=my-app")
# Print the number of Ingress resources retrieved
print(f"Number of Ingress resources with 'app=my-app' label: {len(ingresses.items)}")
Source:
Other Parameters
The list methods accept several other parameters to fine-tune your resource retrieval.
Parameter: resource_version
(str)
Description: When specified with a watch call, shows changes that occur after that particular version of a resource. Defaults to changes from the beginning of history.
Parameter: resource_version_match
(str)
Description: resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended to use resourceVersionMatch field for list calls with resourceVersion to ensure that you are seeing the latest state of the resources. Possible enum values: [“Include”, “Exact”, “None”]
Parameter: send_initial_events
(bool)
Description: If true, returned list will include resource that matches the resourceVersion.
Parameter: timeout_seconds
(int)
Description: Timeout for the list/watch call.
Parameter: watch
(bool)
Description: Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.
Source:
Accessing List Items
List methods return an object representing the list of resources. This object provides access to the individual items in the list.
Example:
from kubernetes import client, config
# Load the Kubernetes configuration
config.load_kube_config()
# Create an instance of the API client
api = client.NetworkingV1Api()
# Retrieve a list of Ingress resources
ingresses = api.list_namespaced_ingress(namespace="default")
# Iterate over the list items
for ingress in ingresses.items:
# Print the name of each Ingress resource
print(ingress.metadata.name)
Source:
Documentation
For detailed information on each parameter and available resource lists, refer to the documentation for the specific API client.
Example:
- kubernetes/docs/NetworkingV1Api.md
- kubernetes/docs/CoordinationV1alpha1Api.md
- kubernetes/docs/NetworkingV1beta1Api.md
## Top-Level Directory Explanations
<a class='local-link directory-link' data-ref="doc/" href="#doc/">doc/</a> - This directory contains documentation files for the project.
<a class='local-link directory-link' data-ref="doc/source/" href="#doc/source/">doc/source/</a> - This directory contains the source files for the documentation.
<a class='local-link directory-link' data-ref="examples/" href="#examples/">examples/</a> - This directory contains example usage of the Kubernetes client library.
<a class='local-link directory-link' data-ref="examples/dynamic-client/" href="#examples/dynamic-client/">examples/dynamic-client/</a> - This directory contains examples of using the dynamic client to interact with Kubernetes.
<a class='local-link directory-link' data-ref="examples/watch/" href="#examples/watch/">examples/watch/</a> - This directory contains examples of using the watch function to monitor Kubernetes resources.
<a class='local-link directory-link' data-ref="kubernetes/" href="#kubernetes/">kubernetes/</a> - This directory contains the main Kubernetes client library.
<a class='local-link directory-link' data-ref="kubernetes/base/" href="#kubernetes/base/">kubernetes/base/</a> - This directory contains the base Kubernetes client library.
<a class='local-link directory-link' data-ref="kubernetes/base/config/" href="#kubernetes/base/config/">kubernetes/base/config/</a> - This directory contains configuration files for the base library.
<a class='local-link directory-link' data-ref="kubernetes/base/dynamic/" href="#kubernetes/base/dynamic/">kubernetes/base/dynamic/</a> - This directory contains the dynamic client implementation for the base library.
<a class='local-link directory-link' data-ref="kubernetes/base/hack/" href="#kubernetes/base/hack/">kubernetes/base/hack/</a> - This directory contains hack files for the base library.
<a class='local-link directory-link' data-ref="kubernetes/base/leaderelection/" href="#kubernetes/base/leaderelection/">kubernetes/base/leaderelection/</a> - This directory contains the leader election implementation for the base library.
<a class='local-link directory-link' data-ref="kubernetes/base/stream/" href="#kubernetes/base/stream/">kubernetes/base/stream/</a> - This directory contains the stream implementation for the base library.
<a class='local-link directory-link' data-ref="kubernetes/base/watch/" href="#kubernetes/base/watch/">kubernetes/base/watch/</a> - This directory contains the watch implementation for the base library.
<a class='local-link directory-link' data-ref="kubernetes/client/" href="#kubernetes/client/">kubernetes/client/</a> - This directory contains the top-level client for the Kubernetes client library.
<a class='local-link directory-link' data-ref="kubernetes/client/api/" href="#kubernetes/client/api/">kubernetes/client/api/</a> - This directory contains the API definitions for the client library.
<a class='local-link directory-link' data-ref="kubernetes/client/models/" href="#kubernetes/client/models/">kubernetes/client/models/</a> - This directory contains the data models used by the client library.
<a class='local-link directory-link' data-ref="kubernetes/e2e_test/" href="#kubernetes/e2e_test/">kubernetes/e2e_test/</a> - This directory contains end-to-end tests for the Kubernetes client library.
<a class='local-link directory-link' data-ref="kubernetes/e2e_test/test_yaml/" href="#kubernetes/e2e_test/test_yaml/">kubernetes/e2e_test/test_yaml/</a> - This directory contains YAML files used in the end-to-end tests.
<a class='local-link directory-link' data-ref="kubernetes/utils/" href="#kubernetes/utils/">kubernetes/utils/</a> - This directory contains utility functions for the Kubernetes client library.
<a class='local-link directory-link' data-ref="scripts/" href="#scripts/">scripts/</a> - This directory contains scripts used in the development and build process.