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 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 complicated examples, like asynchronous multiple watch or tail logs from pods, you can find in examples/ folder.

Documentation

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

Compatibility

This library is generated in the same way as the official Kubernetes Python Library. It uses swagger-codegen and the same concepts like streaming, watching or reading configuration. Because of an early stage of this library some differences still exist:

synchronous library kubernetes-client/python this library
authentication method gcp-token, azure-token, user-token, oidc-token, user-password, in-cluster gcp-token (only via gcloud command), user-token, oidc-token, user-password, in-cluster
streaming data via websocket from PODs bidirectional read-only is already implemented

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-35.0.1.tar.gz (1.3 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-35.0.1-py3-none-any.whl (2.9 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kubernetes_asyncio-35.0.1.tar.gz
Algorithm Hash digest
SHA256 975870e3097b647c265a59b9175ab0841f0de06cd2162268273ca210b1fa672e
MD5 776efbd42c35eabddaa0c54720b56743
BLAKE2b-256 67b9f3b9fb2d3ef4550918b83c328dc720a58f65bc66732d9438e06469573ad1

See more details on using hashes here.

Provenance

The following attestation bundles were made for kubernetes_asyncio-35.0.1.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-35.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kubernetes_asyncio-35.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 244ef45943e89c5c5104276a646bfcbf1a9dc3d060876c2094aa601e932f1c03
MD5 e878e09feeb1310b62ef786af1887e5d
BLAKE2b-256 36b3a8917d253763095fb8dcaaefc6a135ed31abbd13f681e78752e226e252fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for kubernetes_asyncio-35.0.1-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