-
Notifications
You must be signed in to change notification settings - Fork 220
fix: change namespace starts processor on namespace change even if not leader #2344
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-framework/src/test/java/io/javaoperatorsdk/operator/LeaderElectionChangeNamespaceIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.time.Duration; | ||
import java.time.ZonedDateTime; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.coordination.v1.Lease; | ||
import io.fabric8.kubernetes.api.model.coordination.v1.LeaseSpecBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.leaderelectionchangenamespace.LeaderElectionChangeNamespaceCustomResource; | ||
import io.javaoperatorsdk.operator.sample.leaderelectionchangenamespace.LeaderElectionChangeNamespaceReconciler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class LeaderElectionChangeNamespaceIT { | ||
|
||
public static final String LEASE_NAME = "nschangelease"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withConfigurationService(o -> o.withLeaderElectionConfiguration( | ||
new LeaderElectionConfiguration(LEASE_NAME))) | ||
.withReconciler(new LeaderElectionChangeNamespaceReconciler()) | ||
.build(); | ||
|
||
private static KubernetesClient client = new KubernetesClientBuilder().build(); | ||
|
||
@BeforeAll | ||
static void createLeaseManually() { | ||
client.resource(lease()).create(); | ||
} | ||
|
||
@AfterAll | ||
static void deleteLeaseManually() { | ||
client.resource(lease()).delete(); | ||
} | ||
|
||
@Test | ||
@DisplayName("If operator is not a leader, namespace change should not start processor") | ||
void noReconcileOnChangeNamespace() { | ||
extension.create(testResource()); | ||
|
||
var reconciler = extension.getReconcilerOfType(LeaderElectionChangeNamespaceReconciler.class); | ||
await().pollDelay(Duration.ofSeconds(1)) | ||
.timeout(Duration.ofSeconds(3)) | ||
.untilAsserted(() -> { | ||
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(0); | ||
}); | ||
|
||
extension.getRegisteredControllerForReconcile(LeaderElectionChangeNamespaceReconciler.class) | ||
.changeNamespaces("default", extension.getNamespace()); | ||
|
||
await().pollDelay(Duration.ofSeconds(1)) | ||
.timeout(Duration.ofSeconds(3)) | ||
.untilAsserted(() -> { | ||
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(0); | ||
}); | ||
} | ||
|
||
|
||
LeaderElectionChangeNamespaceCustomResource testResource() { | ||
var resource = new LeaderElectionChangeNamespaceCustomResource(); | ||
resource.setMetadata(new ObjectMetaBuilder() | ||
.withName("test1") | ||
.build()); | ||
return resource; | ||
} | ||
|
||
static Lease lease() { | ||
var lease = new Lease(); | ||
lease.setMetadata(new ObjectMetaBuilder() | ||
.withName(LEASE_NAME) | ||
.withNamespace("default") | ||
.build()); | ||
var time = ZonedDateTime.now(); | ||
lease.setSpec(new LeaseSpecBuilder() | ||
.withAcquireTime(ZonedDateTime.now()) | ||
.withRenewTime(time) | ||
.withAcquireTime(time) | ||
.withHolderIdentity("non-operator-identity") | ||
.withLeaseTransitions(0) | ||
.withLeaseDurationSeconds(30) | ||
.build()); | ||
|
||
return lease; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...tor/sample/leaderelectionchangenamespace/LeaderElectionChangeNamespaceCustomResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.javaoperatorsdk.operator.sample.leaderelectionchangenamespace; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.ShortNames; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.javaoperatorsdk") | ||
@Version("v1") | ||
@ShortNames("lcn") | ||
public class LeaderElectionChangeNamespaceCustomResource | ||
extends CustomResource<Void, Void> | ||
implements Namespaced { | ||
} |
29 changes: 29 additions & 0 deletions
29
...perator/sample/leaderelectionchangenamespace/LeaderElectionChangeNamespaceReconciler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.javaoperatorsdk.operator.sample.leaderelectionchangenamespace; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; | ||
|
||
@ControllerConfiguration() | ||
public class LeaderElectionChangeNamespaceReconciler | ||
implements Reconciler<LeaderElectionChangeNamespaceCustomResource>, TestExecutionInfoProvider { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
@Override | ||
public UpdateControl<LeaderElectionChangeNamespaceCustomResource> reconcile( | ||
LeaderElectionChangeNamespaceCustomResource resource, | ||
Context<LeaderElectionChangeNamespaceCustomResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could it happen that at this point we are are already the leader (and accidentally not start processing) or the
synchronised
will take care of thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly,
synchronized
takes care of that