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
799 stars 43 forks source link

Provide a leader election module #278

Open multani opened 8 months ago

multani commented 8 months ago

Which project are you requesting an enhancement for?

kr8s

What do you need?

Would it be possible to provide an out of the box module that implement leader election directly from kr8s?

The official Kubernetes client for Python provide some kind of implementation directly in the library: https://github.com/kubernetes-client/python/blob/master/kubernetes/base/leaderelection/ and there are well-known pattern to implement leader election using only core primitives of Kubernetes.

If this is something that kr8s would be willing to provide, I would be interested to contribute the feature :)

jacobtomlinson commented 8 months ago

Thanks for raising this @multani. This sounds like a really great feature to add to kr8s. I've never used the leader election modules in the other Kubernetes clients, but I think kubernetes is the only Python library that implements it so this would be another nice differentiating factor for kr8s.

Looking at the example in kubernetes I think we can definitely improve on the API too. Before you dive into code it would be great to have a discussion about the API here so we can find something that is readable and feels pythonic. We would also need to think about any differences there might be between a sync and async API as we will need to provide both.

jacobtomlinson commented 8 months ago

It also looks like the official Python client uses the previous control-plane.alpha.kubernetes.io/leader annotation on a ConfigMap approach to leadership election. This has been superseded by Lease resources in the coordination.k8s.io/v1 API which is beta in Kubernetes 1.26+. Our minimum supported version is 1.26 so we are fine to go ahead and use this.

multani commented 8 months ago

It looks like the official Python client implementation is more like a "demo" rather than a production ready implementation, but I think it's a good idea to offer this mechanism out of the box.

Looking at the example in kubernetes I think we can definitely improve on the API too. Before you dive into code it would be great to have a discussion about the API here so we can find something that is readable and feels pythonic. We would also need to think about any differences there might be between a sync and async API as we will need to provide both.

I'm interested to take a look at this and try to sketch up something that would be nice to use and would offer both sync and async APIs.

I did a similar experiment on top of Consul's lock/session APIs several months ago, I can come back in a couple of days with some ideas how it could look like using Kubernetes mechanisms instead :+1:

jacobtomlinson commented 8 months ago

I can come back in a couple of days with some ideas how it could look like using Kubernetes mechanisms instead

Sounds great!

jacobtomlinson commented 6 months ago

@multani I assume you got busy, it happens! I'm going to drop the assignment and add the "help wanted" label in case anyone else wants to pick it up.

multani commented 6 months ago

@jacobtomlinson yes, that's fair :bow: I still plan to take a look, but it took a bit longer than expected and I've been busy with other things.

If someone else wants to move this forward, that would also be awesome!