Kubernetes API Interaction

This document outlines the core interaction methods used by the Python client for interacting with the Kubernetes API. These methods provide the foundation for managing Kubernetes resources and understanding their behavior.

Key Concepts

  • API Groups: Represent a logical grouping of related Kubernetes resources, e.g., apps, core, networking.
  • API Versions: Within each group, specific versions of the API can exist, e.g., v1, v1beta1.
  • Resources: These represent the Kubernetes objects managed by the API, e.g., pod, deployment, service.

Interaction Methods

The Python client offers various methods for interacting with the Kubernetes API, including:

CRUD Operations (Create, Read, Update, Delete)

  • Create: These methods send a POST request to the API server to create a new resource, e.g., create_namespaced_pod (found in kubernetes/client/api/core_v1_api.py).
  • Read: These methods use a GET request to retrieve an existing resource, e.g., read_namespaced_pod (found in kubernetes/client/api/core_v1_api.py).
  • Update: These methods utilize PUT or PATCH requests to modify an existing resource, e.g., replace_namespaced_pod (found in kubernetes/client/api/core_v1_api.py).
  • Delete: These methods send a DELETE request to remove a resource, e.g., delete_namespaced_pod (found in kubernetes/client/api/core_v1_api.py).

Other Operations

  • List: Used to retrieve a collection of resources, e.g., list_namespaced_pod (found in kubernetes/client/api/core_v1_api.py).
  • Connect: Methods for establishing connections to resources, e.g., connect_get_namespaced_pod_exec (found in kubernetes/client/api/core_v1_api.py). This enables interacting with resources through channels like exec, port-forwarding, and proxy.

Example Usage

from kubernetes import client, config
          
          # Load Kubernetes configuration
          config.load_kube_config()
          
          # Create a CoreV1Api instance
          api = client.CoreV1Api()
          
          # Create a Pod
          pod_manifest = {
              'apiVersion': 'v1',
              'kind': 'Pod',
              'metadata': {
                  'name': 'my-pod',
                  'namespace': 'default',
              },
              'spec': {
                  'containers': [
                      {
                          'name': 'nginx',
                          'image': 'nginx:1.14.2'
                      }
                  ]
              }
          }
          
          api.create_namespaced_pod(namespace='default', body=pod_manifest)
          
          # Get a Pod
          pod = api.read_namespaced_pod(name='my-pod', namespace='default')
          
          # Delete a Pod
          api.delete_namespaced_pod(name='my-pod', namespace='default')
          

References

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.

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/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/e2e_test/test_yaml/ - This directory contains YAML files used in the end-to-end tests.

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

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