Skip to content

Commit 59f030f

Browse files
committed
leader election fix
1 parent b168191 commit 59f030f

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,19 @@ public class LeaderElectionManager {
2626
private CompletableFuture<?> leaderElectionFuture;
2727
private ConfigurationService configurationService;
2828
private ExecutorServiceManager executorServiceManager;
29+
private KubernetesClient kubernetesClient;
2930

30-
public LeaderElectionManager(ControllerManager controllerManager,
31+
public LeaderElectionManager(KubernetesClient kubernetesClient,
32+
ControllerManager controllerManager,
3133
ConfigurationService configurationService, ExecutorServiceManager executorServiceManager) {
34+
this.kubernetesClient = kubernetesClient;
3235
this.controllerManager = controllerManager;
3336
this.configurationService = configurationService;
3437
this.executorServiceManager = executorServiceManager;
3538
}
3639

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-
6540
public boolean isLeaderElectionEnabled() {
66-
return leaderElector != null;
41+
return configurationService.getLeaderElectionConfiguration().isPresent();
6742
}
6843

6944
private LeaderCallbacks leaderCallbacks() {
@@ -95,6 +70,7 @@ private String identity(LeaderElectionConfiguration config) {
9570

9671
public void start() {
9772
if (isLeaderElectionEnabled()) {
73+
init(configurationService.getLeaderElectionConfiguration().orElseThrow());
9874
leaderElectionFuture = leaderElector.start();
9975
}
10076
}
@@ -104,4 +80,32 @@ public void stop() {
10480
leaderElectionFuture.cancel(false);
10581
}
10682
}
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+
}
107111
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ public Operator(KubernetesClient kubernetesClient, ConfigurationService configur
7676

7777

7878
leaderElectionManager =
79-
new LeaderElectionManager(controllerManager, configurationService, executorServiceManager);
80-
configurationService.getLeaderElectionConfiguration()
81-
.ifPresent(c -> leaderElectionManager.init(c, this.kubernetesClient));
79+
new LeaderElectionManager(kubernetesClient, controllerManager, configurationService,
80+
executorServiceManager);
8281
}
8382

8483
/**

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/LeaderElectionManagerTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import io.fabric8.kubernetes.client.KubernetesClient;
1313
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
14+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1415
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
1516
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration;
1617

@@ -29,10 +30,13 @@ class LeaderElectionManagerTest {
2930
void setUp() {
3031
ControllerManager controllerManager = mock(ControllerManager.class);
3132
kubernetesClient = mock(KubernetesClient.class);
32-
var configurationService = new BaseConfigurationService();
33+
var configurationService = ConfigurationService.overrideCurrent(new BaseConfigurationService(),
34+
o -> o.withLeaderElectionConfiguration(new LeaderElectionConfiguration("test")));
35+
var execServiceManager = new ExecutorServiceManager(configurationService);
36+
execServiceManager.init();
3337
leaderElectionManager =
34-
new LeaderElectionManager(controllerManager, configurationService,
35-
new ExecutorServiceManager(configurationService));
38+
new LeaderElectionManager(kubernetesClient, controllerManager, configurationService,
39+
execServiceManager);
3640
}
3741

3842
@AfterEach
@@ -41,12 +45,6 @@ void tearDown() {
4145
System.getProperties().remove(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY);
4246
}
4347

44-
@Test
45-
void testInit() {
46-
leaderElectionManager.init(new LeaderElectionConfiguration("test", "testns"), kubernetesClient);
47-
assertTrue(leaderElectionManager.isLeaderElectionEnabled());
48-
}
49-
5048
@Test
5149
void testInitInferLeaseNamespace(@TempDir Path tempDir) throws IOException {
5250
var namespace = "foo";
@@ -56,7 +54,7 @@ void testInitInferLeaseNamespace(@TempDir Path tempDir) throws IOException {
5654
System.setProperty(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
5755
System.setProperty(KUBERNETES_NAMESPACE_FILE, namespacePath.toString());
5856

59-
leaderElectionManager.init(new LeaderElectionConfiguration("test"), kubernetesClient);
57+
leaderElectionManager.start();
6058
assertTrue(leaderElectionManager.isLeaderElectionEnabled());
6159
}
6260

@@ -65,7 +63,6 @@ void testFailedToInitInferLeaseNamespace() {
6563
System.setProperty(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
6664
assertThrows(
6765
IllegalArgumentException.class,
68-
() -> leaderElectionManager.init(new LeaderElectionConfiguration("test"),
69-
kubernetesClient));
66+
() -> leaderElectionManager.start());
7067
}
7168
}

0 commit comments

Comments
 (0)