kr8s icon indicating copy to clipboard operation
kr8s copied to clipboard

Provide a leader election module

Open multani opened this issue 1 year ago • 6 comments

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 :)

multani avatar Jan 08 '24 21:01 multani

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 avatar Jan 09 '24 11:01 jacobtomlinson

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.

jacobtomlinson avatar Jan 09 '24 11:01 jacobtomlinson

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:

multani avatar Jan 09 '24 14:01 multani

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

Sounds great!

jacobtomlinson avatar Jan 09 '24 14:01 jacobtomlinson

@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.

jacobtomlinson avatar Mar 13 '24 14:03 jacobtomlinson

@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!

multani avatar Mar 13 '24 14:03 multani