- .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
The ConfigMapLock
class implements a leader election mechanism using Kubernetes ConfigMaps. It provides methods for creating, getting, and updating ConfigMaps with leader election records.
Initialization (__init__
)
- Takes the
name
of the lock,namespace
where it resides, and a uniqueidentity
of the candidate. - Initializes an instance of the
CoreV1Api
from the Kubernetes client for interacting with ConfigMaps. - Sets the
leader_electionrecord_annotationkey
used for storing leader election information in ConfigMap annotations. - Initializes a dictionary
lock_record
to store the leader election record data.
Get (get
)
- Takes the
name
andnamespace
of the ConfigMap to read. - Attempts to read the ConfigMap using the
read_namespaced_config_map
method. - Checks for the presence of annotations in the ConfigMap:
- If no annotations exist, it creates a new annotation with the
leader_electionrecord_annotationkey
and an empty value. - If the
leader_electionrecord_annotationkey
is not present, it adds it with an empty value. - If the annotation exists, it extracts the leader election record JSON string from the annotation and parses it using
json.loads
. - It then converts the parsed JSON data into a
LeaderElectionRecord
object. - Stores the retrieved ConfigMap object in the
configmap_reference
for future updates. - Returns
True
and theLeaderElectionRecord
object if successful, otherwise returnsFalse
and the exception caught.
Create (create
)
- Takes the
name
,namespace
, and anelection_record
(aLeaderElectionRecord
object) as arguments. - Creates a new
V1ConfigMap
object with the provided name, namespace, and an annotation containing the encoded JSON string of theelection_record
. - Attempts to create the ConfigMap using the
create_namespaced_config_map
method. - Returns
True
if successful, otherwiseFalse
and logs the exception.
Update (update
)
- Takes the
name
,namespace
, and anupdated_record
(aLeaderElectionRecord
object) as arguments. - Encodes the
updated_record
as JSON and updates theleader_electionrecord_annotationkey
in theconfigmap_reference
object. - Attempts to update the ConfigMap using the
replace_namespaced_config_map
method. - Returns
True
if successful, otherwiseFalse
and logs the exception.
Helper Methods
get_lock_object
: Converts a JSON-parsed leader election record (dict) into aLeaderElectionRecord
object.get_lock_dict
: Converts aLeaderElectionRecord
object into a dictionary representation suitable for encoding into JSON.
Overall
This class provides a convenient way to implement leader election in Kubernetes using ConfigMaps. It handles creating, reading, and updating ConfigMaps, as well as encoding and decoding leader election records. The helper methods simplify the process of converting between JSON data and LeaderElectionRecord
objects.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph