-
Notifications
You must be signed in to change notification settings - Fork 3.3k
add support Lease-based leader election (rather than ConfigMaps) #1877 #2314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rvlane
wants to merge
1
commit into
kubernetes-client:master
Choose a base branch
from
nokia:elector-lease
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
kubernetes/base/leaderelection/resourcelock/leaselock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Copyright 2021 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes.client.rest import ApiException | ||
from kubernetes import client | ||
from ..leaderelectionrecord import LeaderElectionRecord | ||
from datetime import datetime | ||
import logging | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
class LeaseLock: | ||
def __init__(self, name, namespace, identity): | ||
""" | ||
:param name: name of the lock | ||
:param namespace: namespace | ||
:param identity: A unique identifier that the candidate is using | ||
""" | ||
self.api_instance = client.CoordinationV1Api() | ||
|
||
# lease resource identity and reference | ||
self.name = name | ||
self.namespace = namespace | ||
self.lease_reference = None | ||
|
||
# identity of this candidate | ||
self.identity = str(identity) | ||
|
||
# get returns the election record from a Lease Annotation | ||
def get(self, name, namespace): | ||
""" | ||
:param name: Name of the lease object information to get | ||
:param namespace: Namespace in which the lease object is to be searched | ||
:return: 'True, election record' if object found else 'False, exception response' | ||
""" | ||
try: | ||
lease = self.api_instance.read_namespaced_lease(name, namespace) | ||
|
||
except ApiException as e: | ||
return False, e | ||
else: | ||
self.lease_reference = lease | ||
return True, self.election_record(lease) | ||
|
||
def create(self, name, namespace, election_record): | ||
""" | ||
:param electionRecord: Annotation string | ||
:param name: Name of the lease object to be created | ||
:param namespace: Namespace in which the lease object is to be created | ||
:return: 'True' if object is created else 'False' if failed | ||
""" | ||
body = client.V1Lease(metadata={"name": name}, | ||
spec=self.update_lease(election_record)) | ||
|
||
try: | ||
_ = self.api_instance.create_namespaced_lease(namespace, body, pretty=True) | ||
return True | ||
except ApiException as e: | ||
logging.info("Failed to create lock as {}".format(e)) | ||
return False | ||
|
||
def update(self, name, namespace, updated_record): | ||
""" | ||
:param name: name of the lock to be updated | ||
:param namespace: namespace the lock is in | ||
:param updated_record: the updated election record | ||
:return: True if update is successful False if it fails | ||
""" | ||
try: | ||
# update the Lease from the updated record | ||
self.lease_reference.spec = self.update_lease(updated_record, | ||
self.lease_reference.spec) | ||
|
||
_ = self.api_instance.replace_namespaced_lease(name=name, namespace=namespace, | ||
body=self.lease_reference) | ||
return True | ||
except ApiException as e: | ||
logging.info("Failed to update lock as {}".format(e)) | ||
return False | ||
|
||
def update_lease(self, leader_election_record, current_spec=None): | ||
# existing or new lease? | ||
spec = current_spec if current_spec else client.V1LeaseSpec() | ||
|
||
# lease configuration | ||
spec.holder_identity = leader_election_record.holder_identity | ||
spec.lease_duration_seconds = int(leader_election_record.lease_duration) | ||
spec.acquire_time = self.time_str_to_iso(leader_election_record.acquire_time) | ||
spec.renew_time = self.time_str_to_iso(leader_election_record.renew_time) | ||
|
||
return spec | ||
|
||
def election_record(self, lease): | ||
""" | ||
Get leader election record from Lease spec. | ||
""" | ||
leader_election_record = LeaderElectionRecord(None, None, None, None) | ||
|
||
if lease.spec and lease.spec.holder_identity: | ||
leader_election_record.holder_identity = lease.spec.holder_identity | ||
if lease.spec and lease.spec.lease_duration_seconds: | ||
leader_election_record.lease_duration = str(lease.spec.lease_duration_seconds) | ||
if lease.spec and lease.spec.acquire_time: | ||
leader_election_record.acquire_time = str(datetime.replace(lease.spec.acquire_time, tzinfo=None)) | ||
if lease.spec and lease.spec.renew_time: | ||
leader_election_record.renew_time = str(datetime.replace(lease.spec.renew_time, tzinfo=None)) | ||
|
||
return leader_election_record | ||
|
||
# conversion between kubernetes ISO formatted time and elector record time | ||
def time_str_to_iso(self, str_time): | ||
formats = ["%Y-%m-%d %H:%M:%S.%f%z", "%Y-%m-%d %H:%M:%S.%f"] | ||
for fmt in formats: | ||
try: | ||
return datetime.strptime(str_time, fmt).isoformat()+'Z' | ||
except ValueError: | ||
pass | ||
logging.error("Failed to parse time string: {}".format(str_time)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/from in the/from the
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do, thanks