All Projects → olitheolix → aiokubernetes

olitheolix / aiokubernetes

Licence: Apache-2.0 license
Asynchronous Kubernetes Client

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to aiokubernetes

Aiohttp admin
admin interface for aiohttp application http://aiohttp-admin.readthedocs.io
Stars: ✭ 207 (+696.15%)
Mutual labels:  aiohttp
fastapi-tdd-docker
Test-Driven Development with FastAPI and Docker
Stars: ✭ 89 (+242.31%)
Mutual labels:  async-python
cb4
Joint Online Judge
Stars: ✭ 20 (-23.08%)
Mutual labels:  aiohttp
Python Mocket
a socket mock framework - for all kinds of socket animals, web-clients included
Stars: ✭ 209 (+703.85%)
Mutual labels:  aiohttp
Tokio
Asyncio event loop written in Rust language
Stars: ✭ 236 (+807.69%)
Mutual labels:  aiohttp
Asyncpg
A fast PostgreSQL Database Client Library for Python/asyncio.
Stars: ✭ 5,216 (+19961.54%)
Mutual labels:  async-python
Ok ip proxy pool
🍿爬虫代理IP池(proxy pool) python🍟一个还ok的IP代理池
Stars: ✭ 196 (+653.85%)
Mutual labels:  aiohttp
fastAPI-aiohttp-example
How to use and test fastAPI with an aiohttp client
Stars: ✭ 69 (+165.38%)
Mutual labels:  aiohttp
aiounittest
Test python asyncio-based code with ease.
Stars: ✭ 53 (+103.85%)
Mutual labels:  async-python
revolt.py
Python wrapper for https://revolt.chat
Stars: ✭ 58 (+123.08%)
Mutual labels:  aiohttp
Arsenic
Async WebDriver implementation for asyncio and asyncio-compatible frameworks
Stars: ✭ 209 (+703.85%)
Mutual labels:  aiohttp
Gidgethub
An async GitHub API library for Python
Stars: ✭ 226 (+769.23%)
Mutual labels:  aiohttp
Uvloop
Ultra fast asyncio event loop.
Stars: ✭ 8,246 (+31615.38%)
Mutual labels:  async-python
Create Aio App
The boilerplate for aiohttp. Quick setup for your asynchronous web service.
Stars: ✭ 207 (+696.15%)
Mutual labels:  aiohttp
cashews
Cache with async power
Stars: ✭ 204 (+684.62%)
Mutual labels:  aiohttp
Aiohttp Devtools
dev tools for aiohttp
Stars: ✭ 202 (+676.92%)
Mutual labels:  aiohttp
Twisted
Event-driven networking engine written in Python.
Stars: ✭ 4,442 (+16984.62%)
Mutual labels:  async-python
CoubDownloader
A simple downloader for coub.com
Stars: ✭ 64 (+146.15%)
Mutual labels:  aiohttp
Playing-with-Asyncio
This series shows you the basics of how to use the Asyncio Library in Python.
Stars: ✭ 37 (+42.31%)
Mutual labels:  aiohttp
tws async
Make the Python IB API from Interactive Brokers run inside an event loop
Stars: ✭ 79 (+203.85%)
Mutual labels:  async-python

Build Status PyPI version pypi supported versions

Sync and Async Kubernetes Client

See the documentation and examples for usage instructions.

Key Features

Getting Started

pip install aiokubernetes

List all Pods

import asyncio
import aiokubernetes as k8s

async def main():
    # Load default kubeconfig file and create an aiohttp client instance.
    config = k8s.utils.load_config(warn=False)
    client = k8s.clients.get_aiohttp(config)
    proxy = k8s.api_proxy.Proxy(config)

    # Ask Kubernetes for all Pods.
    cargs = k8s.api.CoreV1Api(proxy).list_namespace(watch=False)
    ret = await client.request(**cargs)

    # Ensure the API call to Kubernetes did succeed.
    assert ret.status == 200

    # Optional: wrap the JSon response into a Swagger generated Python object.
    obj = k8s.swagger.unpack(await ret.read())

    # Print the pod names.
    for i in obj.items:
        print(f"{i.metadata.namespace} {i.metadata.name}")

    # Close all pending client connections.
    await client.close()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

Mix Synchronous and Asynchronous Clients To List Pods

The following example lists pods, just like the previous one. The difference is that it does so twice, once with the asynchronous AioHttp library and once with the synchronous Requests library. The example does not use any convenience function to configure the SSL certificates for the (a)sync client. This is to emphasise that aiokubernetes does not add any secret sauce to use the Kubernetes API because Kubernetes is, after all, just that: an API. aiokubernetes conveniently compiles the necessary headers, request body and URL for the selected resource endpoint. The rest is up to you.

import asyncio

import aiohttp
import requests
import ssl

import aiokubernetes as k8s


async def main():
    # Load default kubeconfig file and create an aiohttp client instance.
    config = k8s.utils.load_config(warn=False)

    # --- Compile the API request parameters to list all Pods in the cluster ---

    # `cargs` is a dict with keys for 'data' (body), 'headers', 'method' (eg
    # GET, POST), and 'url'. This encompasses the necessary information _any_
    # REST client would need to successfully list the K8s namespaces.
    cargs = k8s.api.CoreV1Api(k8s.api_proxy.Proxy(config)).list_namespace(watch=False)

    # --- Setup the AioHttp client ---
    connector = aiohttp.TCPConnector(
        limit=4,
        ssl_context=ssl.create_default_context(cafile=config.ssl_ca_cert),
        verify_ssl=True,
    )
    client_aiohttp = aiohttp.ClientSession(connector=connector)

    # --- Setup the Requests client ---
    client_requests = requests.Session()
    client_requests.verify = config.ssl_ca_cert

    # --- Ask with asynchronous client ---
    ret = await client_aiohttp.request(**cargs)
    assert ret.status == 200
    text_async = await ret.text()
    await client_aiohttp.close()

    # --- Ask with synchronous client ---
    ret = client_requests.request(**cargs, verify=config.ssl_ca_cert)
    assert ret.status_code == 200
    text_sync = ret.text

    # --- Verify that both clients returned the same data ---
    assert text_sync == text_async

    # Optional: wrap the raw (byte encoded) response into a Swagger object.
    obj = k8s.swagger.unpack(text_sync.encode('utf8'))

    # Print the pod names.
    for i in obj.items:
        print(f"{i.metadata.namespace} {i.metadata.name}")


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

Concurrently Watch Multiple Resources

Watch several Kubernetes resources for 5 seconds then shut down gracefully.

import asyncio

import aiokubernetes as k8s


async def watch_resource(client, cargs):
    """Consume and print the events as they stream in."""

    # Use helper class to consume the K8s events via an async iterator.
    watch = k8s.watch.AioHttpClientWatch(client.request(**cargs))
    async for event in watch:
        print(f"{event.name} {event.obj.kind} {event.obj.metadata.name}")


async def main():
    # Load default kubeconfig file and create an aiohttp client instance.
    config = k8s.utils.load_config(warn=False)
    client = k8s.clients.get_aiohttp(config)
    proxy = k8s.api_proxy.Proxy(config)

    # Namespaces and Pods are in the K8s Core API, Deployments in an extension (unless you run a very recent K8s version).
    corev1 = k8s.CoreV1Api(proxy)
    extv1beta = k8s.ExtensionsV1beta1Api(proxy)

    # Compile the necessary request parameters (headers, body, parameters, ...)
    # for the API calls we want to make. We also specify watch=True because we
    # want to listen to changes.
    cargs_ns = corev1.list_namespace(timeout_seconds=5, watch=True)
    cargs_pods = corev1.list_pod_for_all_namespaces(timeout_seconds=5, watch=True)
    cargs_deploy = extv1beta.list_deployment_for_all_namespaces(timeout_seconds=5, watch=True) # noqa

    # Define and dispatch the tasks.
    tasks = [
        watch_resource(client, cargs_ns),
        watch_resource(client, cargs_pods),
        watch_resource(client, cargs_deploy),
    ]
    await asyncio.gather(*tasks)

    # Close all pending connections.
    await client.close()


if __name__ == '__main__':
    # Setup event loop and start the program.
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.ensure_future(main()))
    loop.close()

Source code

git clone https://github.com/olitheolix/aiokubernetes.git
cd aiokubernetes
pip install -r requirements.txt
pip install -r test-requirements.txt

Feel free to submit pull requests and issues.

Unit Tests

We use pytest:

# Run tests sequentially.
pytest

# Run tests in 8 parallel processes.
pytest -n8

CI Tests

Travis runs all tests in Docker containers (see .travis for details).

You can run these containerised tests locally as well to ensure Travis on Github will approve your build before you push it.

docker run -ti -v`pwd`:/src python:3.6-alpine3.7 /bin/ash -c "cd /src; ash scripts/run-ci-tests.sh"
docker run -ti -v`pwd`:/src python:3.7-alpine3.7 /bin/ash -c "cd /src; ash scripts/run-ci-tests.sh"

Motivation

Like Python Kubernetes and kubernetes_asyncio, this client library uses the Swagger-generated Api- and model classes. Unlike those two libraries, it only supports Python 3.6+. All Python 2.x code paths are gone, as is the dependency on six.

Also unlike kubernetes_asyncio, aiokubernetes is not backwards compatible with the synchronous Python Kubernetes client. The differences are, however, minor from a user's point of view (see the examples/).

The primary difference is the increased emphasis on "Explicit is better than implicit" (Zen of Python). To that end, aiokubernetes never creates implicit HTTP session objects (the user must supply ApiClient instances) and the library always returns the native HTTP response object.

The HTTP response object is particularly useful for debugging because it provides trivial access to Url, method, path, headers, status code and raw response data before it was de-serialised into a Swagger class.

The plan is to simplify the new ApiClient further and cover it with tests along the way because the auto-generated api_client.py and rest.py classes have none, unfortunately.

License

aiokubernetes is licensed under the Apache 2 license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].