- .github
- devel
- doc
-
examples
-
dynamic-client
-
notebooks
-
watch
-
yaml_dir
- README.md
- __init__.py
- annotate_deployment.py
- api_discovery.py
- apply_from_dict.py
- apply_from_directory.py
- apply_from_single_file.py
- cluster_scoped_custom_object.py
- cronjob_crud.py
- deployment_create.py
- deployment_crud.py
- duration-gep2257.py
- in_cluster_config.py
- ingress_create.py
- job_crud.py
- multiple_clusters.py
- namespaced_custom_object.py
- node_labels.py
- out_of_cluster_config.py
- pick_kube_config_context.py
- pod_config_list.py
- pod_exec.py
- pod_portforward.py
- remote_cluster.py
- rollout-daemonset.py
- rollout-statefulset.py
-
dynamic-client
- kubernetes
- scripts
- .gitignore
- CHANGELOG.md
- CONTRIBUTING.md
- LICENSE
- MANIFEST.in
- OWNERS
- README.md
- SECURITY_CONTACTS
- code-of-conduct.md
- codecov.yml
- requirements.txt
- setup.cfg
- setup.py
- test-requirements.txt
- tox.ini
Explanation
This code implements a leader election mechanism using Kubernetes annotations.
The LeaderElection
class is responsible for managing the election process. It uses a lock object (a Kubernetes resource like a Deployment or a Pod) to store the leader information. The lock object is updated with an annotation containing the current leader’s information, including the leader’s identity and lease duration.
Here’s a breakdown of the code:
Initialization:
Takes
election_config
as an argument. This configuration contains information about the lock object, lease duration, retry period, and callback functions.observed_record
: Stores the latest observed leader information from the lock.observed_time_milliseconds
: Tracks the last time the lock was updated.run()
: The main entry point for leader election.Calls
acquire()
to try to acquire the lock (become leader).If successful, calls the
onstarted_leading()
callback function.Starts a thread to continuously renew the lease using the
renew_loop()
function.If the lease renewal fails, calls the
onstopped_leading()
callback function.acquire()
: Attempts to acquire the lock.Continuously retries acquiring the lock until successful.
Calls
try_acquire_or_renew()
to attempt to acquire or renew the lease.renew_loop()
: Continuously renews the lease to maintain leadership.Uses a
renew_deadline
to ensure the lease is renewed before it expires.Calls
try_acquire_or_renew()
to attempt to renew the lease.If the lease renewal fails, exits the loop and returns.
try_acquire_or_renew()
:Retrieves the current leader information from the lock.
If no lock exists, attempts to create a new lock with this candidate as the leader.
If a lock exists, checks if the current candidate is already the leader. If not, it tries to update the lock with this candidate as the leader if the current lease has expired.
If the candidate is already the leader, it updates the
renew_time
to keep the lease active.update_lock()
: Updates the lock with the providedleader_election_record
.This method is responsible for applying the annotation to the lock object.
Overall, the code provides a robust mechanism for leader election using Kubernetes annotations. It includes features such as lease renewals, retries, and callback functions to manage transitions between leadership states.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph