Client Structure

The kubernetes.client package contains a variety of API objects that allow interaction with the Kubernetes API.

The CoreV1Api object, along with similar objects for other API versions and resources, are generated using the OpenAPI Generator project (https://github.com/openapitools/openapi-generator). This object provides methods for interacting with the Kubernetes API, including:

  • Creating new resources
  • Retrieving existing resources
  • Updating existing resources
  • Deleting existing resources
  • Listing resources based on various criteria

API Clients for different versions and resources:

The kubernetes.client package includes objects for interacting with various API versions and resources.

For example, you can find API objects for:

  • CoreV1Api: Kubernetes core objects like Pods, Services, and Namespaces.
  • AppsV1Api: Objects for managing deployments, replica sets, and stateful sets.
  • BatchV1Api: Objects for managing jobs and cron jobs.

Customization:

You can customize your client behavior by setting configuration options, such as:

  • host: The address of the Kubernetes API server.
  • api_key: The API key to use for authentication.
  • ssl_ca_cert: The path to a CA certificate file.
  • verify_ssl: Whether to verify the SSL certificate of the API server.

Examples:

Here are some example code snippets showing how to use the client objects:

Creating a pod:

from kubernetes import client, config
          from pprint import pprint
          
          # Configure API client
          config.load_kube_config()
          api_instance = client.CoreV1Api()
          
          # Create pod object
          pod_manifest = {
              'apiVersion': 'v1',
              'kind': 'Pod',
              'metadata': {
                  'name': 'my-pod',
                  'labels': {
                      'app': 'my-app'
                  }
              },
              'spec': {
                  'containers': [
                      {
                          'name': 'my-container',
                          'image': 'nginx:latest',
                      }
                  ]
              }
          }
          
          # Create pod
          api_response = api_instance.create_namespaced_pod(namespace='default', body=pod_manifest)
          pprint(api_response)
          

Getting a pod by name:

from kubernetes import client, config
          from pprint import pprint
          
          # Configure API client
          config.load_kube_config()
          api_instance = client.CoreV1Api()
          
          # Get pod by name
          try:
              api_response = api_instance.read_namespaced_pod(name='my-pod', namespace='default')
              pprint(api_response)
          except ApiException as e:
              print("Exception when calling CoreV1Api->read_namespaced_pod: %s\n" % e)
          

Deleting a pod:

from kubernetes import client, config
          from pprint import pprint
          
          # Configure API client
          config.load_kube_config()
          api_instance = client.CoreV1Api()
          
          # Delete pod
          api_response = api_instance.delete_namespaced_pod(name='my-pod', namespace='default', body=client.V1DeleteOptions())
          pprint(api_response)
          

Listing all pods:

from kubernetes import client, config
          from pprint import pprint
          
          # Configure API client
          config.load_kube_config()
          api_instance = client.CoreV1Api()
          
          # List all pods
          api_response = api_instance.list_namespaced_pod(namespace='default')
          pprint(api_response.items)
          

These examples show how to use the CoreV1Api object to interact with Kubernetes Pods. You can find similar examples for other objects and API versions in the examples directory (tree/master/examples).

For more detailed documentation, refer to the official Kubernetes Python client documentation (https://github.com/kubernetes-client/python/).

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.

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/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/apis/ - This directory contains the API implementations 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/test/ - This directory contains unit tests for the Kubernetes client library.

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