-
Notifications
You must be signed in to change notification settings - Fork 237
Try to get lease namespace if unspecified #1450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b621d9d
1317c4f
cdd6fea
0c12947
014e05a
56a69be
87315bb
cdbe8d8
1204727
a2abd99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,15 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace) { | |
| RETRY_PERIOD_DEFAULT_VALUE, null); | ||
| } | ||
|
|
||
| public LeaderElectionConfiguration(String leaseName) { | ||
| this( | ||
| leaseName, | ||
| null, | ||
| LEASE_DURATION_DEFAULT_VALUE, | ||
| RENEW_DEADLINE_DEFAULT_VALUE, | ||
| RETRY_PERIOD_DEFAULT_VALUE, null); | ||
| } | ||
|
|
||
| public LeaderElectionConfiguration( | ||
| String leaseName, | ||
| String leaseNamespace, | ||
|
|
@@ -59,8 +68,8 @@ public LeaderElectionConfiguration( | |
| this.identity = identity; | ||
| } | ||
|
|
||
| public String getLeaseNamespace() { | ||
| return leaseNamespace; | ||
| public Optional<String> getLeaseNamespace() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this is a breaking change. I can mark it as deprecated and provide another method
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh normally I would prefer backwards compatible with deprecated.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably would be better, yes. Then again, I don't know if anyone is using Leader Election already…
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for users, direct usage of this API will be rare even they do use leader election feature. Probably only in unit test. |
||
| return Optional.ofNullable(leaseNamespace); | ||
| } | ||
|
|
||
| public String getLeaseName() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package io.javaoperatorsdk.operator; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import io.fabric8.kubernetes.client.KubernetesClient; | ||
| import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration; | ||
|
|
||
| import static io.fabric8.kubernetes.client.Config.KUBERNETES_NAMESPACE_FILE; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| class LeaderElectionManagerTest { | ||
|
|
||
| private ControllerManager controllerManager; | ||
| private KubernetesClient kubernetesClient; | ||
| private LeaderElectionManager leaderElectionManager; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| controllerManager = mock(ControllerManager.class); | ||
| kubernetesClient = mock(KubernetesClient.class); | ||
| leaderElectionManager = new LeaderElectionManager(controllerManager); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| System.getProperties().remove(KUBERNETES_NAMESPACE_FILE); | ||
| } | ||
|
|
||
| @Test | ||
| void testInit() { | ||
| leaderElectionManager.init(new LeaderElectionConfiguration("test", "testns"), kubernetesClient); | ||
| assertTrue(leaderElectionManager.isLeaderElectionEnabled()); | ||
| } | ||
|
|
||
| @Test | ||
| void testInitInferLeaseNamespace(@TempDir Path tempDir) throws IOException { | ||
| var namespace = "foo"; | ||
| var namespacePath = tempDir.resolve("namespace"); | ||
| Files.writeString(namespacePath, namespace); | ||
|
|
||
| System.setProperty(KUBERNETES_NAMESPACE_FILE, namespacePath.toString()); | ||
|
|
||
| leaderElectionManager.init(new LeaderElectionConfiguration("test"), kubernetesClient); | ||
| assertTrue(leaderElectionManager.isLeaderElectionEnabled()); | ||
| } | ||
|
|
||
| @Test | ||
| void testFailedToInitInferLeaseNamespace() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> leaderElectionManager.init(new LeaderElectionConfiguration("test"), | ||
| kubernetesClient)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.