Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for asyncio #323

Open
tomplus opened this issue Aug 18, 2017 · 42 comments
Open

support for asyncio #323

tomplus opened this issue Aug 18, 2017 · 42 comments
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@tomplus
Copy link
Member

tomplus commented Aug 18, 2017

Hi guys.

Do you consider providing this client library with support for asyncio ?

Regards,
Tomasz Prus

(copy of kubernetes-client/python-base#27)

@tomplus
Copy link
Member Author

tomplus commented Aug 19, 2017

I've prepared the Pull request where I show how callbacks can be used with asyncio. Let me know what you think about it. Thanks.

@achimnol
Copy link

achimnol commented Jan 21, 2018

I just experimented with Swagger v2.3.1 which supports asyncio+aiohttp based client library generation, so that we could use a "native" async interface instead of interpolating callbacks to futures.

What I have done to make it working, starting from the kubernetes-client/gen repository:

  • Set SWAGGER_CODEGEN_COMMIT in openapi/python.sh to "v2.3.1"
  • Add <library>asyncio</library> under openapi/python.xml's <configuration> element.
  • Run the generator script as instructed.
    • my config was:
      export KUBERNETES_BRANCH="release-1.8"
      export CLIENT_VERSION="1.0"  # just a random value
      export PACKAGE_NAME="kubernetes"
  • After generation, restructure the generated client library to match the structure guide
    — UPDATE: these are the "small changes" in @tomplus comment in feat: add examples for asynchronous usage (callback, asycio) #324.
    • Relocate the generated kubernetes python package to kubernetes/client.
    • Add python-base as kubernetes/base git submodule.
    • Create symbolic links kubernetes/stream, kubernetes/config, kubernetes/watch like this repo
    • Create kubernetes/__init__.py like this repo
    • Replace all absolute imports to use relative imports (e.g., from kubernetes.api_client.apps_v1beta1_deployment import AppsV1beta1Deployment in the generated code to from .apps_v1beta1_deployment import AppsV1beta1Deployment)
    • Merge requirements of python-base and the generated library
    • pip install -e . inside a virtualenv
    • Fix up circular import errors related to V1beta1JSONSchemaProps
    • NOTE: I think find and sed commands in the generator's python.sh script should do the above changes automatically, but it didn't work out of the box. So I did manually.
  • Comment out ssl_verify argument in kubernetes/client/api_client.py since aiohttp forbids passing both ssl_context and ssl_verify option (the later is included in the former)

Then I could run the following example:

import asyncio
from kubernetes import client, config

loop = asyncio.get_event_loop()

async def do():
    config.load_kube_config()
    v1 = client.CoreV1Api()
    ret = await v1.list_pod_for_all_namespaces(watch=False)
    for i in ret.items:
        print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

loop.run_until_complete(do())
loop.close()

So the majority of APIs can be converted to use asyncio + aiohttp without much efforts, thanks to Swagger. I hope my "manual" fix-ups could be automated somehow!
Still, the python-base streaming wrappers should be rewritten from scratch.

@seeker89
Copy link

seeker89 commented Feb 1, 2018

asyncio+aiohttp support would be really appreciated !

@EvgenyKhaliper
Copy link

I use aiohttp as a server which accesses the python-client so it will be very benefitial to me as well.

@hanikesn
Copy link

Here's how you can do an async watch with the using client (minus error handling):

class Connector(object):
    def __init__(self, q: asyncio.Queue, loop: asyncio.AbstractEventLoop, timeout: int = 30):
        client_config = type.__call__(Configuration)
        config.load_kube_config(client_configuration=client_config, persist_config=False)
        client = ApiClient(configuration=client_config)
        self.__configuration = client_config
        self.__v1 = CoreV1Api(client)
        self.__extv1beta1 = ExtensionsV1beta1Api(client)
        self.__queue = q
        self.__loop = loop

    async def start(self):
        async with aiohttp.ClientSession() as session:
            await asyncio.gather(
                self.__watch(session=session, api=self.__v1, url='/api/v1/endpoints', kind='V1Endpoints'),
                self.__watch(session=session, api=self.__v1, url='/api/v1/services', kind='V1Service'),
                self.__watch(session=session, api=self.__extv1beta1, url='/apis/extensions/v1beta1/ingresses', kind='V1beta1Ingress'),
                loop=self.__loop)

    async def __watch(self, session: aiohttp.ClientSession, api, url: str, kind: str):
        params = {'watch': 'true'}
        async with session.get(self.__configuration.host + url, params=params,
                               headers=self.__configuration.api_key) as response:
            while True:
                chunk = await response.content.readline()
                if not chunk:
                    break
                j = json.loads(chunk)
                j['object'] = api.api_client._ApiClient__deserialize(data=j['object'], klass=kind)
                await self.add_event(j)

@tomplus
Copy link
Member Author

tomplus commented May 20, 2018

At last I’ve prepared the new library kubernetes_asyncio which is based on this Python client, but uses the asyncio generator from swagger-codegen. I've added a script to create this client to kubernetes-client/gen repo in this PR kubernetes-client/gen#60. I've also decided to incorporate functionality from sub-repository kubernetes-client/python-base because a lot of changes according to asyncio were needed.

Please take a look, thanks.

@olitheolix
Copy link

Inspired by @tomplus I have followed in his footsteps and created aiokubernetes.

Unlike kubernetes_asyncio, it is not backwards compatible. The user visible changes are actually minor but allowed me to remove unused Python 2.x code paths and drop Python 3.5 in favor of Python 3.7 (perhaps relevant for #558).

I added documentation and usage examples and would be grateful for any feedback - thank you.

@roycaihw
Copy link
Member

Please see #324 (comment) for discussion about experimenting asyncio library in this repo.

@olitheolix we need to be backwards compatible for this package. I haven't seen reasons for dropping Python 3.5 (and even strong needs for fully supporting 3.7) yet. What has been changed?

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 24, 2019
@mitar
Copy link
Contributor

mitar commented Apr 24, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 24, 2019
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 23, 2019
@mitar
Copy link
Contributor

mitar commented Jul 23, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 23, 2019
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 21, 2019
@mitar
Copy link
Contributor

mitar commented Oct 22, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 22, 2019
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 20, 2020
@mitar
Copy link
Contributor

mitar commented Jan 20, 2020

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 20, 2020
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@zegelin
Copy link

zegelin commented Jan 15, 2021

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 15, 2021
@mecampbellsoup
Copy link
Contributor

Just curious but, @tomplus what was your motivation for doing this?

Can't sync libraries be used drop-in inside an async application/context?

@hanikesn
Copy link

I'm not Tom, but I used kubernetes_asyncio in the past to build a highly stateful nginx plus controller based on watches, which requires quite a bit of coordination between different parts of the system. Having a native async library makes this task a lot easier.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 17, 2021
@mitar
Copy link
Contributor

mitar commented Jun 17, 2021

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 17, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 15, 2021
@remram44
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 15, 2021
@cstruct
Copy link

cstruct commented Oct 20, 2021

Just curious but, @tomplus what was your motivation for doing this?

Can't sync libraries be used drop-in inside an async application/context?

Calling blocking sync functions from a async function blocks the entire event loop which is unacceptable in most cases. @mecampbellsoup

@cstruct
Copy link

cstruct commented Oct 20, 2021

@roycaihw since Python 2.X and 3.5 has reached EOL (Python 3.6 reaches EOL 23 December this year) is there something still blocking this?

@gaby
Copy link

gaby commented Dec 30, 2021

Any updates on this?

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 30, 2022
@mindw
Copy link

mindw commented Mar 30, 2022

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 30, 2022
@roycaihw
Copy link
Member

/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Mar 30, 2022
@takeda
Copy link

takeda commented Dec 4, 2022

So what's the state of this? Is there a goal to merge @tomplus changes or implement a new solution?

@abi-jey
Copy link

abi-jey commented Mar 30, 2023

I would also love to see this become a possibility, what needs to be done for this change? Would it be possible for anyone to do a wrap-up of what is needed for native asyncio support?

@remram44
Copy link

kubernetes_asyncio works great

@des1redState
Copy link

des1redState commented Sep 13, 2023

The attached PR seems to have gone stale and closed. People are gonna really struggle to develop WebSocket-based K8s integrations in Python without this, obviously among other use-cases.

@abi-jey
Copy link

abi-jey commented Sep 13, 2023

Why not use the dropin replacement kebernetes-asyncio? @des1redState

@takeda
Copy link

takeda commented Sep 14, 2023

@realabja, LOL ironically @tomplus who opened this ticket is the author of kubernetes-asyncio.

It would be much easier for everyone if such support was builtin, I'm quite sure it is a lot of work maintaining a separate version that constantly needs to catch up with the official client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests