File tree Expand file tree Collapse file tree 3 files changed +27
-10
lines changed
kubernetes_asyncio/leaderelection Expand file tree Collapse file tree 3 files changed +27
-10
lines changed Original file line number Diff line number Diff line change @@ -65,8 +65,10 @@ async def example_end_func():
6565 lease_duration = 17 ,
6666 renew_deadline = 15 ,
6767 retry_period = 5 ,
68- onstarted_leading = example_start_func (),
69- onstopped_leading = example_end_func (),
68+ # Coroutines are also accepted, to facilitate providing context
69+ # (e.g. passing apic)
70+ onstarted_leading = example_start_func ,
71+ onstopped_leading = example_end_func ,
7072 )
7173
7274 # Enter leader election
Original file line number Diff line number Diff line change 1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from collections .abc import Coroutine # noqa:F401
15+ from collections .abc import Callable , Coroutine # noqa:F401
1616
1717
1818class Config :
1919 # Validate config, exit if an error is detected
2020
21- # onstarted_leading and onstopped_leading are defined as coroutines rather
22- # than callables in order to faciliate passing context. For example, this
23- # allows the ApiClient used by the leader election to be shared and reused.
21+ # onstarted_leading and onstopped_leading accept either coroutines or
22+ # coroutine functions. Coroutines faciliate passing context, but coroutine
23+ # functions can be simpler when passing context is not required.
24+ #
25+ # One example of when passing context is helpful is sharing the ApiClient
26+ # used by the leader election, which can then be used for subsequent
27+ # Kubernetes API operations upon onstopped_leading or onstopped_leading.
2428 def __init__ (
2529 self ,
2630 lock ,
2731 lease_duration ,
2832 renew_deadline ,
2933 retry_period ,
30- onstarted_leading , # type: Coroutine
31- onstopped_leading = None , # type: Coroutine | None
34+ onstarted_leading , # type: Coroutine | Callable[[], Coroutine]
35+ onstopped_leading = None , # type: Coroutine | Callable[[], Coroutine] | None
3236 ):
3337 self .jitter_factor = 1.2
3438
Original file line number Diff line number Diff line change 1414
1515import asyncio
1616import datetime
17+ import inspect
1718import json
1819import logging
1920import sys
@@ -55,7 +56,13 @@ async def run(self):
5556 "%s successfully acquired lease" , self .election_config .lock .identity
5657 )
5758
58- task = asyncio .create_task (self .election_config .onstarted_leading )
59+ onstarted_leading_coroutine = (
60+ self .election_config .onstarted_leading
61+ if inspect .iscoroutine (self .election_config .onstarted_leading )
62+ else self .election_config .onstarted_leading ()
63+ )
64+
65+ task = asyncio .create_task (onstarted_leading_coroutine )
5966
6067 await self .renew_loop ()
6168
@@ -68,7 +75,11 @@ async def run(self):
6875 # preserved in order to continue to provide an interface similar to
6976 # the one provided by `kubernetes-client/python`.
7077 if self .election_config .onstopped_leading is not None :
71- await self .election_config .onstopped_leading
78+ await (
79+ self .election_config .onstopped_leading
80+ if inspect .iscoroutine (self .election_config .onstopped_leading )
81+ else self .election_config .onstopped_leading ()
82+ )
7283
7384 async def acquire (self ):
7485 # Follower
You can’t perform that action at this time.
0 commit comments