Calls

This section describes how to interact with the Kubernetes API to create, update, delete, and retrieve resources.

Watch

Use watch to subscribe to changes in Kubernetes resources, receiving a stream of add, update, and remove notifications.

from kubernetes import client, watch
          
          v1 = client.CoreV1Api()
          w = watch.Watch()
          for event in w.stream(v1.list_namespace, resource_version=1127):
              # Handle events: ADDED, DELETED, MODIFIED
              print(f"Event Type: {event['type']}")
              print(f"Object: {event['object']}")
              if should_stop:
                  w.stop()
          

Source: kubernetes/base/watch/watch.py

Timeout

Set timeout_seconds to limit the duration of a list or watch call, regardless of activity. This parameter helps avoid indefinite waiting.

from kubernetes import client
          
          v1 = client.CoreV1Api()
          namespaces = v1.list_namespace(timeout_seconds=5)
          print(namespaces)
          

Source: kubernetes/docs/CoreV1Api.md

Examples

  • Listing Namespaces with Timeout:
from kubernetes import client, watch
          
          v1 = client.CoreV1Api()
          namespaces = v1.list_namespace(timeout_seconds=10)  # Limit call to 10 seconds
          for namespace in namespaces.items:
              print(f"Namespace: {namespace.metadata.name}")
          
  • Watching for Changes in Pods:
from kubernetes import client, watch
          
          v1 = client.CoreV1Api()
          w = watch.Watch()
          for event in w.stream(v1.list_pod_for_all_namespaces, watch=True, resource_version='123456789'): 
              # Start watching from a specific resource version
              print(f"Event Type: {event['type']}")
              print(f"Object: {event['object']}")
              if should_stop:
                  w.stop()
          
  • Getting a Specific Pod with Timeout:
from kubernetes import client
          
          v1 = client.CoreV1Api()
          pod = v1.read_namespaced_pod(name='my-pod', namespace='my-namespace', timeout_seconds=5)
          print(f"Pod Name: {pod.metadata.name}")
          

Top-Level Directory Explanations

doc/ - This directory contains documentation files for the project.

doc/source/ - This directory contains the source files for the documentation.

examples/ - This directory contains example usage of the Kubernetes client library.

examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.

examples/watch/ - This directory contains examples of using the watch function to monitor Kubernetes resources.

kubernetes/ - This directory contains the main Kubernetes client library.

kubernetes/base/ - This directory contains the base Kubernetes client library.

kubernetes/base/config/ - This directory contains configuration files for the base library.

kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.

kubernetes/base/hack/ - This directory contains hack files for the base library.

kubernetes/base/leaderelection/ - This directory contains the leader election implementation for the base library.

kubernetes/base/stream/ - This directory contains the stream implementation for the base library.

kubernetes/base/watch/ - This directory contains the watch implementation for the base library.

kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.

kubernetes/client/api/ - This directory contains the API definitions for the client library.

kubernetes/client/models/ - This directory contains the data models used by the client library.

kubernetes/e2e_test/ - This directory contains end-to-end tests for the Kubernetes client library.

kubernetes/e2e_test/test_yaml/ - This directory contains YAML files used in the end-to-end tests.

kubernetes/test/ - This directory contains unit tests for the Kubernetes client library.

kubernetes/utils/ - This directory contains utility functions for the Kubernetes client library.

scripts/ - This directory contains scripts used in the development and build process.