#kubernetes #election #consensus #lease #coordination #api-bindings

kube-coordinate

Kubernetes leader election using the coordination.k8s.io API

6 releases

0.2.1 Mar 6, 2024
0.2.0 Mar 6, 2024
0.1.3 Dec 11, 2023

#168 in Asynchronous

Download history 7117/week @ 2024-07-18 6022/week @ 2024-07-25 6591/week @ 2024-08-01 5061/week @ 2024-08-08 6435/week @ 2024-08-15 5910/week @ 2024-08-22 6526/week @ 2024-08-29 7002/week @ 2024-09-05 3471/week @ 2024-09-12 5676/week @ 2024-09-19 7211/week @ 2024-09-26 7727/week @ 2024-10-03 6519/week @ 2024-10-10 10154/week @ 2024-10-17 7787/week @ 2024-10-24 7347/week @ 2024-10-31

33,256 downloads per month

MIT/Apache

33KB
423 lines

Kube Coordinate

docs.rs Build Status

Kubernetes leader election using the coordination.k8s.io API.

Kube Coordinate builds upon the kube-rs ecosystem, and implements a streamlined version of the client-go leader election system. This implementation uses only Lease objects for coordination.

// Spawn a leader elector task, and get a handle to the state channel.
let handle = LeaderElector::spawn(/* ... */);
let state_chan = handle.state();

// Before taking action as a leader, just check the channel to ensure
// the lease is currently held by this process.
if state_chan.borrow().is_leader() {
    // Only perform leader actions if in leader state.
}

// Or, for a more sophisticated pattern, watch the state channel for changes,
// and use it to drive your application's state machine.
let state_stream = tokio_stream::wrappers::WatchStream::new(state_chan);
loop {
    tokio::select! {
        Some(state) = state_stream.next() => match state {
            LeaderState::Leader => (), // Leader tasks.
            _ => (), // Non-leader tasks.
        },
    }
}

Install

cargo add kube-coordinate

License

kube-coordinate is licensed under the terms of the MIT License or the Apache License 2.0, at your choosing.

Dependencies

~64MB
~1M SLoC