Kubernetes Type Stubs

22.6.0.post1 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Kubernetes Python client and list all pods across all namespaces, benefiting from the type stubs for static analysis. It attempts to load configuration from a kubeconfig file (respecting KUBECONFIG environment variable) or falls back to in-cluster configuration.

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

view raw JSON →