kr8s-org / kr8s

A batteries-included Python client library for Kubernetes that feels familiar for folks who already know how to use kubectl
https://kr8s.org
BSD 3-Clause "New" or "Revised" License
839 stars 45 forks source link

Possibility to set up a timeout at api object level. #408

Closed leelavg closed 5 months ago

leelavg commented 5 months ago

Which project are you requesting an enhancement for?

kr8s

What do you need?

It could happen that the clusters are in a far away land and might take a non trivial duration to carry the response. In that scenario it'll be better to have a configurable timeout at api object level which all the REST calls can take into consideration.

It is becoming a li'l tedious supplying timeout at call sites individually and I wasn't able to find any timeout available at api obj, pls do point if I'm wrong.

I did a quick search in outstanding/closed issues but didn't hit any link.

Caveats:

so pls consider at your own discretion. Thanks.

Edit: I only hit the timeout a couple of times in clusters not so far away, editing to be realistic.

jacobtomlinson commented 5 months ago

Making this configurable is definitely a good idea.

I expect the place to do it is when the httpx client is created as a timeout= kwarg can be passed here which will set the timeout for all requests. https://github.com/kr8s-org/kr8s/blob/802f18964dab89df36c6f87306683d9a9b43432e/kr8s/_api.py#L86-L90

I'd like to be a little thoughtful about the API though.

If we add it as a kwarg to kr8s.api() this could cause some issues with client caching because it uses the kwargs as the cache identifier, so in theory you could call it multiple times and get different clients.

import kr8s

api = kr8s.api(timeout=10)
api2 = kr8s.api(timeout=20)
# api2 is not a pointer to api, we probably don't want this

Maybe instead we could make it a property with a setter/getter to you explicitly handle it after the API is created. That way if it gets set we can call _create_session again in the setter to create a new httpx client with the updated timeout.

import kr8s

api = kr8s.api()
api.timeout = 10

It would also be possible to set this through any API object.

from kr8s.objects import Pod

po = Pod(...)
po.api.timeout = 10
leelavg commented 5 months ago

Ack, thanks for the info. In the latter case, if we just do api = kr8s.api(); api.timeout = X; then every object would inherit/use this client w/o explicitly passing the timeout, isn't it? If yes, it'd be great.

In my weeks usage I didn't create api explicitly and so just asking above.

jacobtomlinson commented 5 months ago

every object would inherit/use this client w/o explicitly passing the timeout

Yep that's right!