CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-kubernetes

Python client library for interacting with Kubernetes clusters through the Kubernetes API

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration and Authentication

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.

Capabilities

Kubeconfig Loading

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
    """

In-Cluster Configuration

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.
    """

Context Management

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)
    """

Client Creation

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
    """

Automatic Configuration

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
    """

Configuration Classes

Configuration

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: int

ApiClient

class 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: ...

Constants

KUBE_CONFIG_DEFAULT_LOCATION: str  # ~/.kube/config

Exception Types

class ConfigException(Exception):
    """Configuration-related errors."""
    pass

Usage Examples

Basic Configuration Loading

from 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")

In-Cluster Configuration

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 account

Multiple Clients with Different Configurations

from 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)

Context Management

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

docs

application-workloads.md

autoscaling.md

configuration.md

core-resources.md

custom-resources.md

dynamic-client.md

index.md

leader-election.md

networking.md

rbac-security.md

resource-watching.md

storage.md

streaming-operations.md

utilities.md

tile.json