1
1
package io .javaoperatorsdk .operator ;
2
2
3
- import java .util .UUID ;
4
3
import java .util .concurrent .CompletableFuture ;
5
4
6
5
import org .slf4j .Logger ;
16
15
import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
17
16
import io .javaoperatorsdk .operator .api .config .LeaderElectionConfiguration ;
18
17
19
- public class LeaderElectionManager {
18
+ class LeaderElectionManager {
20
19
21
20
private static final Logger log = LoggerFactory .getLogger (LeaderElectionManager .class );
22
21
@@ -30,25 +29,24 @@ public LeaderElectionManager(ControllerManager controllerManager) {
30
29
}
31
30
32
31
public void init (LeaderElectionConfiguration config , KubernetesClient client ) {
33
- this .identity = identity ( config );
32
+ this .identity = config . getIdentity ( );
34
33
Lock lock = new LeaseLock (config .getLeaseNamespace (), config .getLeaseName (), identity );
35
34
// releaseOnCancel is not used in the underlying implementation
36
35
leaderElector = new LeaderElectorBuilder (client ,
37
36
ConfigurationServiceProvider .instance ().getExecutorService ())
38
- .withConfig (
39
- new LeaderElectionConfig (lock , config .getLeaseDuration (), config .getRenewDeadline (),
40
- config .getRetryPeriod (), leaderCallbacks (), true , config .getLeaseName ()))
41
- .build ();
37
+ .withConfig (
38
+ new LeaderElectionConfig (lock , config .getLeaseDuration (), config .getRenewDeadline (),
39
+ config .getRetryPeriod (), leaderCallbacks (), true , config .getLeaseName ()))
40
+ .build ();
42
41
}
43
42
44
43
public boolean isLeaderElectionOn () {
45
44
return leaderElector != null ;
46
45
}
47
46
48
47
private LeaderCallbacks leaderCallbacks () {
49
- return new LeaderCallbacks (this ::startLeading , this ::stopLeading , leader -> {
50
- log .info ("New leader with identity: {}" , leader );
51
- });
48
+ return new LeaderCallbacks (this ::startLeading , this ::stopLeading ,
49
+ leader -> log .info ("New leader with identity: {}" , leader ));
52
50
}
53
51
54
52
private void startLeading () {
@@ -63,14 +61,6 @@ private void stopLeading() {
63
61
System .exit (1 );
64
62
}
65
63
66
- private String identity (LeaderElectionConfiguration config ) {
67
- String id = config .getIdentity ().orElse (System .getenv ("HOSTNAME" ));
68
- if (id == null || id .isBlank ()) {
69
- id = UUID .randomUUID ().toString ();
70
- }
71
- return id ;
72
- }
73
-
74
64
public void start () {
75
65
if (isLeaderElectionOn ()) {
76
66
leaderElectionFuture = leaderElector .start ();
0 commit comments