$ show kr8s-org/kr8s
kr8s is a Python client library for the Kubernetes API that provides both synchronous and asynchronous interfaces, designed to be familiar to users of kubectl. The core implementation is async-first, with sync wrappers generated via an anyio-based bridging layer, ensuring a single source of truth for all logic. The library includes built-in classes for common Kubernetes resources (Pod, Service, Deployment, Node, Job, etc.), dynamic class creation for custom resources, and support for exec, port forwarding, watching, and client caching. Authentication is handled through kubeconfig files, service accounts, exec plugins, OIDC, and direct tokens, all abstracted behind a unified KubeAuth class. The architecture is modular: the main entry points are `kr8s` (sync) and `kr8s.asyncio` (async), with the heavy lifting in `kr8s/_api.py`, `kr8s/_objects.py`, and several utility modules. The library is extensively tested with pytest and Kind clusters, and includes a full featured CLI example (`kubectl-ng`) that reimplements kubectl commands using kr8s, rich, and typer. Documentation is comprehensive, covering installation, authentication, object operations, and guides for building operators. Quality is solid: clean separation of concerns, thoughtful caching (vendored asyncache), robust error handling, and a well-documented API. The primary weakness is the monolithic `_objects.py` (1000+ lines) and the manual sync/async wrapping, which can introduce maintenance overhead. Overall, kr8s is a professionally maintained, production-ready Kubernetes client that stands out for its simplicity and extensibility.
The LLM identified 5 files worth highlighting for reuse:
Provides a generic pattern for wrapping async functions and generators into sync counterparts using anyio.BlockingPortal, running on a daemon background thread. The `as_sync_func`, `as_sync_generator`, and `sync` decorator are self-contained and could be reused in any Python library that needs to offer both sync and async APIs from an async core.
Implements KubeAuth, a unified authentication handler for Kubernetes that supports kubeconfig files, service account tokens, exec plugins, OIDC, client certificates, and bearer tokens. It parses kubeconfig contexts and constructs the appropriate auth flow, caching tokens as needed. This is a reusable component for any Kubernetes-oriented Python tool, as it encapsulates all major auth strategies in one class.
Provides KubeConfig and KubeConfigSet classes for reading, writing, and manipulating kubeconfig files. Supports context switching, namespace management, and arbitrary field access via JSON pointers (RFC 6901). The design is clean and could be extracted as a standalone library for kubeconfig manipulation without requiring the full kr8s client.
Contains generic data utility functions such as `list_dict_unpack`, `dict_list_pack`, `dot_to_nested_dict`, `dict_to_selector`, `xdict`, `sort_versions`, and `escape_rfc6901`. These are pure, well-tested functions that solve common data manipulation problems (e.g., converting dot-separated strings to nested dicts, parsing Kubernetes selectors, semantic version sorting) and can be reused independently.
A vendored copy of the asyncache library providing `cached` and `cachedmethod` decorators for async functions, built on top of cachetools. The implementation is compact and well-tested; the vendoring ensures no external dependency drift. The pattern demonstrates how to support both sync and async caching in a consistent manner.