Skip to main content

Kubernetes Asynchronous Python Client

Project description

Kubernetes Python Client

Build status PyPI version Docs codecov pypi supported versions Client Capabilities Client Support Level

Asynchronous (AsyncIO) client library for the Kubernetes API.

This library is created in the same way as the official https://github.com/kubernetes-client/python but uses asynchronous version of OpenAPI generator. My motivation is described here: https://github.com/kubernetes-client/python/pull/324

Installation

From PyPi directly:

pip install kubernetes_asyncio

It requires Python 3.10+

Example

To list all pods:

import asyncio
from kubernetes_asyncio import client, config
from kubernetes_asyncio.client.api_client import ApiClient


async def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    await config.load_kube_config()

    # use the context manager to close http sessions automatically
    async with ApiClient() as api:

        v1 = client.CoreV1Api(api)
        print("Listing pods with their IPs:")
        ret = await v1.list_pod_for_all_namespaces()

        for i in ret.items:
            print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)


if __name__ == '__main__':
    asyncio.run(main())

More examples, including asynchronous multiple watch or tailing logs from pods, can be found in examples/ folder.

Documentation

https://kubernetes-asyncio.readthedocs.io/

Microsoft Windows

In case this library is used against Kubernetes cluster using client-go credentials plugin, the default asyncio event loop is SelectorEventLoop. This event loop selector, however, does NOT support pipes and subprocesses, so exec_provider.py::ExecProvider is failing. In order to avoid failures the ProactorEventLoop has to be selected. The ProactorEventLoop can be enabled via WindowsProactorEventLoopPolicy.

Application's code needs to contain following code:

import asyncio

asyncio.set_event_loop_policy(
    asyncio.WindowsProactorEventLoopPolicy()
)

Versions

This library is versioned in the same way as the synchronous library. The schema version has been changed with version v18.20.0. Now, first two numbers from version are Kubernetes version (v.1.18.20). The last number is for changes in the library not directly connected with K8s.

Development

# Create and activate virtual environment
uv venv
source .venv/bin/activate

# Install dependencies
uv sync

# Run ruff
ruff check .
ruff format --diff .

# Run tests
pytest

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kubernetes_asyncio-36.1.0.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kubernetes_asyncio-36.1.0-py3-none-any.whl (3.0 MB view details)

Uploaded Python 3

File details

Details for the file kubernetes_asyncio-36.1.0.tar.gz.

File metadata

  • Download URL: kubernetes_asyncio-36.1.0.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kubernetes_asyncio-36.1.0.tar.gz
Algorithm Hash digest
SHA256 6d979d82e5ebe490bea298e7843732a2336173236bae28e200434889443d4443
MD5 14e25dff9605cac612a321597e13b285
BLAKE2b-256 60459e15f4268454636aee32d92ddeaa7128c71100308644bc79685292c1efcc

See more details on using hashes here.

Provenance

The following attestation bundles were made for kubernetes_asyncio-36.1.0.tar.gz:

Publisher: publish.yaml on tomplus/kubernetes_asyncio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kubernetes_asyncio-36.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kubernetes_asyncio-36.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d25915d1abff24fceda551a502208d986f674d72586297aa58bc7d55e7feaf3
MD5 1785d94791e211c827cf3618aa7e10a5
BLAKE2b-256 b2dc695601e3a6f08ca3d6035d300a944974c17084050591faec6e1de39e4a4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for kubernetes_asyncio-36.1.0-py3-none-any.whl:

Publisher: publish.yaml on tomplus/kubernetes_asyncio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page