Kubernetes Type Stubs
Type stubs for the Kubernetes Python API client. This library provides static type checking for code interacting with the `kubernetes` client library. The version numbers of `kubernetes-stubs` generally track the upstream `kubernetes` client library. As of version 22.6.0.post1, it provides type definitions mirroring the API of the corresponding Kubernetes client version. Contributions and bug fixes are welcomed.
Warnings
- gotcha The `kubernetes-stubs` library can sometimes lag behind new releases of the official `kubernetes` client library, leading to missing type definitions for the latest API versions or newly added fields. Consider using forks like `kubernetes-stubs-elephant-fork` if you need more up-to-date stubs.
- gotcha The generated stubs may not always be complete or entirely accurate for all edge cases or dynamically generated Kubernetes resources (like Custom Resource Definitions). For more comprehensive type checking for CRDs or client API functions, consider using `kubernetes-typed` with its `mypy` plugin.
- gotcha The underlying Kubernetes API frequently deprecates older API versions and fields. While `kubernetes-stubs` tracks the Python client, using deprecated APIs in your code can still lead to runtime warnings or failures in a Kubernetes cluster, even if the stubs permit them. Always refer to the official Kubernetes API documentation for deprecation policies.
Install
-
pip install kubernetes-stubs
Imports
- client
from kubernetes import client
- config
from kubernetes import config
Quickstart
import os
from kubernetes import client, config
try:
# Try to load kubeconfig from default location (e.g., ~/.kube/config)
config.load_kube_config(config_file=os.environ.get('KUBECONFIG'))
except config.config_exception.ConfigException:
# Fallback to in-cluster config if KUBECONFIG is not set or file not found
config.load_incluster_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")