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():
65
65
lease_duration = 17 ,
66
66
renew_deadline = 15 ,
67
67
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 ,
70
72
)
71
73
72
74
# Enter leader election
Original file line number Diff line number Diff line change 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from collections .abc import Coroutine # noqa:F401
15
+ from collections .abc import Callable , Coroutine # noqa:F401
16
16
17
17
18
18
class Config :
19
19
# Validate config, exit if an error is detected
20
20
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.
24
28
def __init__ (
25
29
self ,
26
30
lock ,
27
31
lease_duration ,
28
32
renew_deadline ,
29
33
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
32
36
):
33
37
self .jitter_factor = 1.2
34
38
Original file line number Diff line number Diff line change 14
14
15
15
import asyncio
16
16
import datetime
17
+ import inspect
17
18
import json
18
19
import logging
19
20
import sys
@@ -55,7 +56,13 @@ async def run(self):
55
56
"%s successfully acquired lease" , self .election_config .lock .identity
56
57
)
57
58
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 )
59
66
60
67
await self .renew_loop ()
61
68
@@ -68,7 +75,11 @@ async def run(self):
68
75
# preserved in order to continue to provide an interface similar to
69
76
# the one provided by `kubernetes-client/python`.
70
77
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
+ )
72
83
73
84
async def acquire (self ):
74
85
# Follower
You can’t perform that action at this time.
0 commit comments