Crates.io | kube-lease-manager |
lib.rs | kube-lease-manager |
version | 0.5.0 |
source | src |
created_at | 2024-07-08 00:37:51.072544 |
updated_at | 2024-10-12 10:17:25.378694 |
description | Ergonomic and reliable leader election using Kubernetes Lease API. |
homepage | https://github.com/alex-karpenko/kube-lease-manager |
repository | https://github.com/alex-karpenko/kube-lease-manager |
max_upload_size | |
id | 1295229 |
size | 181,493 |
Ergonomic and reliable leader election using Kubernetes Lease API.
kube-lease-manager
is a high-level helper to facilitate leader election using
Lease Kubernetes resource.
It ensures that only a single instance of the lease managers holds the lock at any moment of time.
Some of the typical use cases:
LeaseManager
is a central part of the crate.
This is a convenient wrapper around a Kubernetes Lease
resource to manage all aspects of leader election process.Please visit crate's documentation to get details and more examples.
As mentioned above, kube-lease-manager
provides two possible ways to manage lease lock:
LeaseManager
instance and run its watch()
method.
It returns Tokio watch channel to watch on state changes
Besides that it runs an unattended background task
which permanently tries to lock lease if it's free and publish changed state to the channel.
The task finishes if the channel is closed.LeaseManager
instance and use its changed()
and release()
methods to control lock.
changed()
tries to lock lease as soon as it becomes free and returns actual lock state when it's changed.
Your responsibilities are:
changed()
running (it's a Future
) to ensure lock is refreshing while it's in use;release()
when you don't need the lock and want to make it free for others.First way ensures that lease is locked (has a holder) at any moment of time. Second makes possible to acquire and release lock when you need it.
The simplest example using first locking approach:
use kube::Client;
use kube_lease_manager::{LeaseManagerBuilder, Result};
use std::time::Duration;
#[tokio::main]
async fn main() {
// Use default Kube client
let client = Client::try_default().await?;
// Create the simplest LeaseManager with reasonable defaults using convenient builder.
// It uses Lease resource called `test-watch-lease`.
let manager = LeaseManagerBuilder::new(client, "test-watch-lease")
.build()
.await?;
let (mut channel, task) = manager.watch().await;
// Watch on the channel for lock state changes
tokio::select! {
_ = channel.changed() => {
let lock_state = *channel.borrow_and_update();
if lock_state {
// Do something useful as a leader
println!("Got a luck!");
}
}
_ = tokio::time::sleep(Duration::from_secs(10)) => {
println!("Unable to get lock during 10s");
}
}
// Explicitly close the control channel
drop(channel);
// Wait for the finish of the manager and get it back
let _manager = tokio::join!(task).0.unwrap()?;
Ok(())
}
Please visit crate's documentation to get more examples and usage details.
The author was inspired on this piece of work by two other crates that provide similar functionality: kubert and kube-leader-election. Both of them are great, thanks to the authors. But both have something missing for one of my projects. So it was a reason to create this one.
This project is licensed under the MIT license.