Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb0c3f4

Browse files
committed
add support Lease-based leader election (rather than ConfigMaps)#1877
Signed-off-by: Lane Richard <rick.lane@nokia.com>
1 parent98a5be8 commitbb0c3f4

File tree

4 files changed

+139
-7
lines changed

4 files changed

+139
-7
lines changed

‎kubernetes/base/leaderelection/example.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
fromkubernetesimportclient,config
1717
fromkubernetes.leaderelectionimportleaderelection
1818
fromkubernetes.leaderelection.resourcelock.configmaplockimportConfigMapLock
19+
fromkubernetes.leaderelection.resourcelock.leaselockimportLeaseLock
1920
fromkubernetes.leaderelectionimportelectionconfig
2021

2122

@@ -42,10 +43,13 @@ def example_func():
4243
# A user can choose not to provide any callbacks for what to do when a candidate fails to lead - onStoppedLeading()
4344
# In that case, a default callback function will be used
4445

46+
# Choose the type of lock mechanism to use
47+
lock_object=LeaseLock(lock_name,lock_namespace,candidate_id)
48+
#lock_object = ConfigMapLock(lock_name, lock_namespace, candidate_id)
49+
4550
# Create config
46-
config=electionconfig.Config(ConfigMapLock(lock_name,lock_namespace,candidate_id),lease_duration=17,
47-
renew_deadline=15,retry_period=5,onstarted_leading=example_func,
48-
onstopped_leading=None)
51+
config=electionconfig.Config(lock_object,lease_duration=17,renew_deadline=15,retry_period=5,
52+
onstarted_leading=example_func,onstopped_leading=None)
4953

5054
# Enter leader election
5155
leaderelection.LeaderElection(config).run()

‎kubernetes/base/leaderelection/leaderelectionrecord.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
classLeaderElectionRecord:
17-
#Annotation usedin the lock object
17+
#Lease configuration fromin the lock object.
1818
def__init__(self,holder_identity,lease_duration,acquire_time,renew_time):
1919
self.holder_identity=holder_identity
2020
self.lease_duration=lease_duration

‎kubernetes/base/leaderelection/resourcelock/configmaplock.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
# limitations under the License.
1414

1515
fromkubernetes.client.restimportApiException
16-
fromkubernetesimportclient,config
17-
fromkubernetes.client.api_clientimportApiClient
16+
fromkubernetesimportclient
1817
from ..leaderelectionrecordimportLeaderElectionRecord
1918
importjson
2019
importlogging
@@ -126,4 +125,4 @@ def get_lock_dict(self, leader_election_record):
126125
self.lock_record['acquireTime']=leader_election_record.acquire_time
127126
self.lock_record['renewTime']=leader_election_record.renew_time
128127

129-
returnself.lock_record
128+
returnself.lock_record
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Copyright 2021 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
fromkubernetes.client.restimportApiException
16+
fromkubernetesimportclient
17+
from ..leaderelectionrecordimportLeaderElectionRecord
18+
fromdatetimeimportdatetime
19+
importlogging
20+
logging.basicConfig(level=logging.INFO)
21+
22+
23+
classLeaseLock:
24+
def__init__(self,name,namespace,identity):
25+
"""
26+
:param name: name of the lock
27+
:param namespace: namespace
28+
:param identity: A unique identifier that the candidate is using
29+
"""
30+
self.api_instance=client.CoordinationV1Api()
31+
32+
# lease resource identity and reference
33+
self.name=name
34+
self.namespace=namespace
35+
self.lease_reference=None
36+
37+
# identity of this candidate
38+
self.identity=str(identity)
39+
40+
# get returns the election record from a Lease Annotation
41+
defget(self,name,namespace):
42+
"""
43+
:param name: Name of the lease object information to get
44+
:param namespace: Namespace in which the lease object is to be searched
45+
:return: 'True, election record' if object found else 'False, exception response'
46+
"""
47+
try:
48+
lease=self.api_instance.read_namespaced_lease(name,namespace)
49+
50+
exceptApiExceptionase:
51+
returnFalse,e
52+
else:
53+
self.lease_reference=lease
54+
returnTrue,self.election_record(lease)
55+
56+
defcreate(self,name,namespace,election_record):
57+
"""
58+
:param electionRecord: Annotation string
59+
:param name: Name of the lease object to be created
60+
:param namespace: Namespace in which the lease object is to be created
61+
:return: 'True' if object is created else 'False' if failed
62+
"""
63+
body=client.V1Lease(metadata={"name":name},
64+
spec=self.update_lease(election_record))
65+
66+
try:
67+
_=self.api_instance.create_namespaced_lease(namespace,body,pretty=True)
68+
returnTrue
69+
exceptApiExceptionase:
70+
logging.info("Failed to create lock as {}".format(e))
71+
returnFalse
72+
73+
defupdate(self,name,namespace,updated_record):
74+
"""
75+
:param name: name of the lock to be updated
76+
:param namespace: namespace the lock is in
77+
:param updated_record: the updated election record
78+
:return: True if update is successful False if it fails
79+
"""
80+
try:
81+
# update the Lease from the updated record
82+
self.lease_reference.spec=self.update_lease(updated_record,
83+
self.lease_reference.spec)
84+
85+
_=self.api_instance.replace_namespaced_lease(name=name,namespace=namespace,
86+
body=self.lease_reference)
87+
returnTrue
88+
exceptApiExceptionase:
89+
logging.info("Failed to update lock as {}".format(e))
90+
returnFalse
91+
92+
defupdate_lease(self,leader_election_record,current_spec=None):
93+
# existing or new lease?
94+
spec=current_specifcurrent_specelseclient.V1LeaseSpec()
95+
96+
# lease configuration
97+
spec.holder_identity=leader_election_record.holder_identity
98+
spec.lease_duration_seconds=int(leader_election_record.lease_duration)
99+
spec.acquire_time=self.time_str_to_iso(leader_election_record.acquire_time)
100+
spec.renew_time=self.time_str_to_iso(leader_election_record.renew_time)
101+
102+
returnspec
103+
104+
defelection_record(self,lease):
105+
"""
106+
Get leader election record from Lease spec.
107+
"""
108+
leader_election_record=LeaderElectionRecord(None,None,None,None)
109+
110+
iflease.specandlease.spec.holder_identity:
111+
leader_election_record.holder_identity=lease.spec.holder_identity
112+
iflease.specandlease.spec.lease_duration_seconds:
113+
leader_election_record.lease_duration=str(lease.spec.lease_duration_seconds)
114+
iflease.specandlease.spec.acquire_time:
115+
leader_election_record.acquire_time=str(datetime.replace(lease.spec.acquire_time,tzinfo=None))
116+
iflease.specandlease.spec.renew_time:
117+
leader_election_record.renew_time=str(datetime.replace(lease.spec.renew_time,tzinfo=None))
118+
119+
returnleader_election_record
120+
121+
# conversion between kubernetes ISO formatted time and elector record time
122+
deftime_str_to_iso(self,str_time):
123+
formats= ["%Y-%m-%d %H:%M:%S.%f%z","%Y-%m-%d %H:%M:%S.%f"]
124+
forfmtinformats:
125+
try:
126+
returndatetime.strptime(str_time,fmt).isoformat()+'Z'
127+
exceptValueError:
128+
pass
129+
logging.error("Failed to parse time string: {}".format(str_time))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp