graph LR
LeaderElectionManager["LeaderElectionManager"]
ResourceLockInterface["ResourceLockInterface"]
ConfigMapResourceLock["ConfigMapResourceLock"]
LeaseResourceLock["LeaseResourceLock"]
LeaderElectionRecord["LeaderElectionRecord"]
KubernetesCoreV1APIClient["KubernetesCoreV1APIClient"]
V1ConfigMapModel["V1ConfigMapModel"]
KubernetesCoordinationV1APIClient["KubernetesCoordinationV1APIClient"]
V1LeaseModel["V1LeaseModel"]
LeaderElectionTest["LeaderElectionTest"]
LeaderElectionManager -- "manages leadership using" --> ResourceLockInterface
ConfigMapResourceLock -- "implements" --> ResourceLockInterface
LeaseResourceLock -- "implements" --> ResourceLockInterface
ConfigMapResourceLock -- "interacts with" --> KubernetesCoreV1APIClient
ConfigMapResourceLock -- "uses" --> V1ConfigMapModel
LeaseResourceLock -- "interacts with" --> KubernetesCoordinationV1APIClient
LeaseResourceLock -- "uses" --> V1LeaseModel
ResourceLockInterface -- "stores state in" --> LeaderElectionRecord
LeaderElectionTest -- "tests" --> LeaderElectionManager
LeaderElectionTest -- "uses mock for" --> ResourceLockInterface
This component provides a robust implementation of leader election logic, crucial for building highly available and fault-tolerant applications in Kubernetes. It uses Kubernetes resources (like ConfigMaps or Leases) as distributed locks to ensure only one instance of an application performs a critical task at a time.
Manages the leader election process, including acquiring and renewing the leadership lease. It orchestrates the election logic and interacts with a resource lock to maintain leadership.
Related Classes/Methods:
kubernetes.base.leaderelection.leaderelection.LeaderElection(40:191)kubernetes.base.leaderelection.leaderelection.LeaderElection:run(55:67)kubernetes.base.leaderelection.leaderelection.LeaderElection:acquire(69:80)kubernetes.base.leaderelection.leaderelection.LeaderElection:renew_loop(82:105)kubernetes.base.leaderelection.leaderelection.LeaderElection:try_acquire_or_renew(107:176)kubernetes.base.leaderelection.leaderelection.LeaderElection:update_lock(178:191)
Defines the abstract interface for resource lock mechanisms used in leader election. Concrete implementations provide methods to get, create, and update the leader election record.
Related Classes/Methods:
kubernetes.base.leaderelection.resourcelock.resourcelock.ResourceLock(full file reference)
Implements the resource lock mechanism using Kubernetes ConfigMaps. It provides methods to get, create, and update the leader election record stored in a ConfigMap, acting as the persistent storage for leadership status.
Related Classes/Methods:
kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock(24:129)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:__init__(25:42)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:get(45:72)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:create(74:90)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:update(92:107)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:get_lock_object(109:121)kubernetes.base.leaderelection.resourcelock.configmaplock.ConfigMapLock:get_lock_dict(123:129)
Implements the resource lock mechanism using Kubernetes Leases. It provides methods to get, create, and update the leader election record stored in a Lease object.
Related Classes/Methods:
kubernetes.base.leaderelection.resourcelock.leaselock.LeaseLock(full file reference)
Represents the data structure used to store the leader election state, including holder identity, lease duration, and acquire/renew times.
Related Classes/Methods:
Provides an interface to interact with the Kubernetes Core V1 API server, specifically for managing ConfigMap objects.
Related Classes/Methods:
kubernetes.client.api.core_v1_api.CoreV1Api(27:30454)kubernetes.client.api.core_v1_api.CoreV1Api.read_namespaced_config_map(23206:23231)kubernetes.client.api.core_v1_api.CoreV1Api.create_namespaced_config_map(6613:6641)kubernetes.client.api.core_v1_api.CoreV1Api.replace_namespaced_config_map(26844:26873)
Represents the Kubernetes V1ConfigMap object model, used for data serialization and deserialization when interacting with the Kubernetes API.
Related Classes/Methods:
Provides an interface to interact with the Kubernetes Coordination V1 API server, specifically for managing Lease objects.
Related Classes/Methods:
Represents the Kubernetes V1Lease object model, used for data serialization and deserialization when interacting with the Kubernetes API.
Related Classes/Methods:
Contains unit tests for the leader election mechanism, simulating different scenarios of leader election and verifying the behavior of the LeaderElection and ResourceLock components.
Related Classes/Methods:
kubernetes.base.leaderelection.leaderelection_test.LeaderElectionTest(28:205)kubernetes.base.leaderelection.leaderelection_test.LeaderElectionTest:test_simple_leader_election(29:60)kubernetes.base.leaderelection.leaderelection_test.LeaderElectionTest:test_leader_election(62:137)kubernetes.base.leaderelection.leaderelection_test.LeaderElectionTest:test_Leader_election_with_renew_deadline(152:195)kubernetes.base.leaderelection.leaderelection_test.MockResourceLock(208:266)kubernetes.base.leaderelection.leaderelection_test.LeaderElectionTest.assert_history(197:205)