Skip to content

Commit 0c12947

Browse files
committed
Get namespace from cached cilent config
1 parent cdd6fea commit 0c12947

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public LeaderElectionManager(ControllerManager controllerManager) {
3939
public void init(LeaderElectionConfiguration config, KubernetesClient client) {
4040
this.identity = identity(config);
4141
final var leaseNamespace =
42-
config.getLeaseNamespace().or(LeaderElectionManager::tryNamespaceFromPath);
43-
if (leaseNamespace.isEmpty()) {
42+
config.getLeaseNamespace().orElseGet(
43+
() -> ConfigurationServiceProvider.instance().getClientConfiguration().getNamespace());
44+
if (leaseNamespace == null) {
4445
final var message =
4546
"Lease namespace is not set and cannot be inferred. Leader election cannot continue.";
4647
log.error(message);
4748
throw new IllegalArgumentException(message);
4849
}
49-
final var lock = new LeaseLock(leaseNamespace.orElseThrow(), config.getLeaseName(), identity);
50+
final var lock = new LeaseLock(leaseNamespace, config.getLeaseName(), identity);
5051
// releaseOnCancel is not used in the underlying implementation
5152
leaderElector =
5253
new LeaderElectorBuilder(

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.fabric8.kubernetes.client.KubernetesClient;
1313
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration;
1414

15+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY;
1516
import static io.fabric8.kubernetes.client.Config.KUBERNETES_NAMESPACE_FILE;
1617
import static org.junit.jupiter.api.Assertions.assertThrows;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -33,6 +34,7 @@ void setUp() {
3334
@AfterEach
3435
void tearDown() {
3536
System.getProperties().remove(KUBERNETES_NAMESPACE_FILE);
37+
System.getProperties().remove(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY);
3638
}
3739

3840
@Test
@@ -47,6 +49,7 @@ void testInitInferLeaseNamespace(@TempDir Path tempDir) throws IOException {
4749
var namespacePath = tempDir.resolve("namespace");
4850
Files.writeString(namespacePath, namespace);
4951

52+
System.setProperty(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
5053
System.setProperty(KUBERNETES_NAMESPACE_FILE, namespacePath.toString());
5154

5255
leaderElectionManager.init(new LeaderElectionConfiguration("test"), kubernetesClient);
@@ -55,6 +58,15 @@ void testInitInferLeaseNamespace(@TempDir Path tempDir) throws IOException {
5558

5659
@Test
5760
void testFailedToInitInferLeaseNamespace() {
61+
System.setProperty(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
62+
assertThrows(
63+
IllegalArgumentException.class,
64+
() -> leaderElectionManager.init(new LeaderElectionConfiguration("test"),
65+
kubernetesClient));
66+
}
67+
68+
@Test
69+
void testFailedToInitInferLeaseNamespaceProbablyUsingKubeConfig() {
5870
assertThrows(
5971
IllegalArgumentException.class,
6072
() -> leaderElectionManager.init(new LeaderElectionConfiguration("test"),

0 commit comments

Comments
 (0)