Skip to content

Commit a2b79d8

Browse files
committed
Revert "refactor: move identity generation to configuration"
This reverts commit 93a260a.
1 parent 951d28d commit a2b79d8

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator;
22

3+
import java.util.UUID;
34
import java.util.concurrent.CompletableFuture;
45

56
import org.slf4j.Logger;
@@ -15,7 +16,7 @@
1516
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
1617
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration;
1718

18-
class LeaderElectionManager {
19+
public class LeaderElectionManager {
1920

2021
private static final Logger log = LoggerFactory.getLogger(LeaderElectionManager.class);
2122

@@ -29,7 +30,7 @@ public LeaderElectionManager(ControllerManager controllerManager) {
2930
}
3031

3132
public void init(LeaderElectionConfiguration config, KubernetesClient client) {
32-
this.identity = config.getIdentity();
33+
this.identity = identity(config);
3334
Lock lock = new LeaseLock(config.getLeaseNamespace(), config.getLeaseName(), identity);
3435
// releaseOnCancel is not used in the underlying implementation
3536
leaderElector = new LeaderElectorBuilder(client,
@@ -45,8 +46,9 @@ public boolean isLeaderElectionEnabled() {
4546
}
4647

4748
private LeaderCallbacks leaderCallbacks() {
48-
return new LeaderCallbacks(this::startLeading, this::stopLeading,
49-
leader -> log.info("New leader with identity: {}", leader));
49+
return new LeaderCallbacks(this::startLeading, this::stopLeading, leader -> {
50+
log.info("New leader with identity: {}", leader);
51+
});
5052
}
5153

5254
private void startLeading() {
@@ -61,6 +63,14 @@ private void stopLeading() {
6163
System.exit(1);
6264
}
6365

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+
6474
public void start() {
6575
if (isLeaderElectionEnabled()) {
6676
leaderElectionFuture = leaderElector.start();

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package io.javaoperatorsdk.operator.api.config;
22

33
import java.time.Duration;
4-
import java.util.UUID;
5-
6-
import io.fabric8.zjsonpatch.internal.guava.Strings;
4+
import java.util.Optional;
75

86
public class LeaderElectionConfiguration {
97

@@ -13,7 +11,7 @@ public class LeaderElectionConfiguration {
1311

1412
private final String leaseName;
1513
private final String leaseNamespace;
16-
private String identity;
14+
private final String identity;
1715

1816
private final Duration leaseDuration;
1917
private final Duration renewDeadline;
@@ -72,18 +70,7 @@ public Duration getRetryPeriod() {
7270
return retryPeriod;
7371
}
7472

75-
public String getIdentity() {
76-
if (identity == null) {
77-
identity = System.getenv("HOSTNAME");
78-
if (Strings.isNullOrEmpty(identity)) {
79-
identity = UUID.randomUUID().toString();
80-
} else {
81-
identity = identity.trim();
82-
if (identity.isBlank()) {
83-
identity = UUID.randomUUID().toString();
84-
}
85-
}
86-
}
87-
return identity;
73+
public Optional<String> getIdentity() {
74+
return Optional.ofNullable(identity);
8875
}
8976
}

0 commit comments

Comments
 (0)