Python client library for interacting with Kubernetes clusters through the Kubernetes API
—
Configuration and authentication management for connecting to Kubernetes clusters. Supports loading configuration from kubeconfig files, in-cluster service accounts, or programmatic configuration with various authentication methods.
Load cluster configuration from kubeconfig files with support for multiple contexts and custom configuration locations.
def load_kube_config(
config_file: str = None,
context: str = None,
client_configuration = None,
persist_config: bool = True
) -> None:
"""
Load configuration from kubeconfig file.
Parameters:
- config_file: Path to kubeconfig file (default: ~/.kube/config)
- context: Context name to use from kubeconfig
- client_configuration: Configuration object to populate
- persist_config: Whether to persist config in global client
"""Load configuration when running inside a Kubernetes pod using service account tokens and cluster information.
def load_incluster_config() -> None:
"""
Load in-cluster configuration from service account.
Uses environment variables and mounted service account files.
"""List and manage kubeconfig contexts for working with multiple clusters.
def list_kube_config_contexts(config_file: str = None) -> tuple:
"""
List available kubeconfig contexts.
Parameters:
- config_file: Path to kubeconfig file
Returns:
Tuple of (contexts, active_context)
"""Create API clients with specific configuration without affecting global settings.
def new_client_from_config(
config_file: str = None,
context: str = None,
persist_config: bool = True
) -> ApiClient:
"""
Create new ApiClient from kubeconfig.
Parameters:
- config_file: Path to kubeconfig file
- context: Context name to use
- persist_config: Whether to persist config globally
Returns:
Configured ApiClient instance
"""
def new_client_from_config_dict(
config_dict: dict,
context: str = None,
persist_config: bool = True
) -> ApiClient:
"""
Create new ApiClient from configuration dictionary.
Parameters:
- config_dict: Configuration as dictionary
- context: Context name to use
- persist_config: Whether to persist config globally
Returns:
Configured ApiClient instance
"""Convenience function that tries multiple configuration methods automatically.
def load_config(**kwargs) -> None:
"""
Automatically load configuration using multiple methods.
Tries kubeconfig, then in-cluster config as fallback.
Parameters:
- **kwargs: Arguments passed to load_kube_config or load_incluster_config
"""class Configuration:
host: str
api_key: dict
api_key_prefix: dict
username: str
password: str
ssl_ca_cert: str
cert_file: str
key_file: str
verify_ssl: bool
ssl_context: ssl.SSLContext
proxy: str
proxy_headers: dict
connection_pool_maxsize: int
cert_reqs: str
ca_certs: str
retries: int
timeout: intclass ApiClient:
def __init__(self, configuration: Configuration = None, header_name: str = None, header_value: str = None): ...
def call_api(self, resource_path: str, method: str, **kwargs): ...
def request(self, method: str, url: str, **kwargs): ...
def select_header_accept(self, accepts: list) -> str: ...
def select_header_content_type(self, content_types: list) -> str: ...KUBE_CONFIG_DEFAULT_LOCATION: str # ~/.kube/configclass ConfigException(Exception):
"""Configuration-related errors."""
passfrom kubernetes import client, config
# Load from default kubeconfig location
config.load_kube_config()
# Load from specific file
config.load_kube_config(config_file="/path/to/kubeconfig")
# Load specific context
config.load_kube_config(context="my-cluster-context")
# Use the configuration
v1 = client.CoreV1Api()
pods = v1.list_namespaced_pod(namespace="default")from kubernetes import client, config
# When running inside a Kubernetes pod
config.load_incluster_config()
v1 = client.CoreV1Api()
# Client is now configured to use the pod's service accountfrom kubernetes import client, config
# Create client for production cluster
prod_client = config.new_client_from_config(
config_file="/path/to/prod-kubeconfig",
context="production",
persist_config=False
)
prod_v1 = client.CoreV1Api(api_client=prod_client)
# Create client for development cluster
dev_client = config.new_client_from_config(
config_file="/path/to/dev-kubeconfig",
context="development",
persist_config=False
)
dev_v1 = client.CoreV1Api(api_client=dev_client)from kubernetes import config
# List available contexts
contexts, active_context = config.list_kube_config_contexts()
print(f"Active context: {active_context['name']}")
print("Available contexts:")
for context in contexts:
print(f" - {context['name']}")Install with Tessl CLI
npx tessl i tessl/pypi-kubernetes