11package io .javaoperatorsdk .operator ;
22
3- import java .util .UUID ;
43import java .util .concurrent .CompletableFuture ;
54
65import org .slf4j .Logger ;
1615import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
1716import io .javaoperatorsdk .operator .api .config .LeaderElectionConfiguration ;
1817
19- public class LeaderElectionManager {
18+ class LeaderElectionManager {
2019
2120 private static final Logger log = LoggerFactory .getLogger (LeaderElectionManager .class );
2221
@@ -30,25 +29,24 @@ public LeaderElectionManager(ControllerManager controllerManager) {
3029 }
3130
3231 public void init (LeaderElectionConfiguration config , KubernetesClient client ) {
33- this .identity = identity ( config );
32+ this .identity = config . getIdentity ( );
3433 Lock lock = new LeaseLock (config .getLeaseNamespace (), config .getLeaseName (), identity );
3534 // releaseOnCancel is not used in the underlying implementation
3635 leaderElector = new LeaderElectorBuilder (client ,
3736 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 ();
4241 }
4342
4443 public boolean isLeaderElectionOn () {
4544 return leaderElector != null ;
4645 }
4746
4847 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 ));
5250 }
5351
5452 private void startLeading () {
@@ -63,14 +61,6 @@ private void stopLeading() {
6361 System .exit (1 );
6462 }
6563
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-
7464 public void start () {
7565 if (isLeaderElectionOn ()) {
7666 leaderElectionFuture = leaderElector .start ();
0 commit comments