@@ -26,44 +26,19 @@ public class LeaderElectionManager {
26
26
private CompletableFuture <?> leaderElectionFuture ;
27
27
private ConfigurationService configurationService ;
28
28
private ExecutorServiceManager executorServiceManager ;
29
+ private KubernetesClient kubernetesClient ;
29
30
30
- public LeaderElectionManager (ControllerManager controllerManager ,
31
+ public LeaderElectionManager (KubernetesClient kubernetesClient ,
32
+ ControllerManager controllerManager ,
31
33
ConfigurationService configurationService , ExecutorServiceManager executorServiceManager ) {
34
+ this .kubernetesClient = kubernetesClient ;
32
35
this .controllerManager = controllerManager ;
33
36
this .configurationService = configurationService ;
34
37
this .executorServiceManager = executorServiceManager ;
35
38
}
36
39
37
- public void init (LeaderElectionConfiguration config , KubernetesClient client ) {
38
- this .identity = identity (config );
39
- final var leaseNamespace =
40
- config .getLeaseNamespace ().orElseGet (
41
- () -> configurationService .getClientConfiguration ().getNamespace ());
42
- if (leaseNamespace == null ) {
43
- final var message =
44
- "Lease namespace is not set and cannot be inferred. Leader election cannot continue." ;
45
- log .error (message );
46
- throw new IllegalArgumentException (message );
47
- }
48
- final var lock = new LeaseLock (leaseNamespace , config .getLeaseName (), identity );
49
- // releaseOnCancel is not used in the underlying implementation
50
- leaderElector =
51
- new LeaderElectorBuilder (
52
- client , executorServiceManager .reconcileExecutorService ())
53
- .withConfig (
54
- new LeaderElectionConfig (
55
- lock ,
56
- config .getLeaseDuration (),
57
- config .getRenewDeadline (),
58
- config .getRetryPeriod (),
59
- leaderCallbacks (),
60
- true ,
61
- config .getLeaseName ()))
62
- .build ();
63
- }
64
-
65
40
public boolean isLeaderElectionEnabled () {
66
- return leaderElector != null ;
41
+ return configurationService . getLeaderElectionConfiguration (). isPresent () ;
67
42
}
68
43
69
44
private LeaderCallbacks leaderCallbacks () {
@@ -95,6 +70,7 @@ private String identity(LeaderElectionConfiguration config) {
95
70
96
71
public void start () {
97
72
if (isLeaderElectionEnabled ()) {
73
+ init (configurationService .getLeaderElectionConfiguration ().orElseThrow ());
98
74
leaderElectionFuture = leaderElector .start ();
99
75
}
100
76
}
@@ -104,4 +80,32 @@ public void stop() {
104
80
leaderElectionFuture .cancel (false );
105
81
}
106
82
}
83
+
84
+ private void init (LeaderElectionConfiguration config ) {
85
+ this .identity = identity (config );
86
+ final var leaseNamespace =
87
+ config .getLeaseNamespace ().orElseGet (
88
+ () -> configurationService .getClientConfiguration ().getNamespace ());
89
+ if (leaseNamespace == null ) {
90
+ final var message =
91
+ "Lease namespace is not set and cannot be inferred. Leader election cannot continue." ;
92
+ log .error (message );
93
+ throw new IllegalArgumentException (message );
94
+ }
95
+ final var lock = new LeaseLock (leaseNamespace , config .getLeaseName (), identity );
96
+ // releaseOnCancel is not used in the underlying implementation
97
+ leaderElector =
98
+ new LeaderElectorBuilder (
99
+ kubernetesClient , executorServiceManager .cachingExecutorService ())
100
+ .withConfig (
101
+ new LeaderElectionConfig (
102
+ lock ,
103
+ config .getLeaseDuration (),
104
+ config .getRenewDeadline (),
105
+ config .getRetryPeriod (),
106
+ leaderCallbacks (),
107
+ true ,
108
+ config .getLeaseName ()))
109
+ .build ();
110
+ }
107
111
}
0 commit comments