-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: different reconciler same type (#2229)
Signed-off-by: Attila Mészáros <[email protected]> Signed-off-by: Chris Laprun <[email protected]> Co-authored-by: Chris Laprun <[email protected]>
- Loading branch information
Showing
8 changed files
with
177 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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
16 changes: 0 additions & 16 deletions
16
...k-core/src/test/java/io/javaoperatorsdk/operator/sample/simple/DuplicateCRController.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
...tor-framework/src/test/java/io/javaoperatorsdk/operator/MultipleReconcilerSameTypeIT.java
This file contains 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,61 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.multiplereconcilersametype.MultipleReconcilerSameTypeCustomResource; | ||
import io.javaoperatorsdk.operator.sample.multiplereconcilersametype.MultipleReconcilerSameTypeReconciler1; | ||
import io.javaoperatorsdk.operator.sample.multiplereconcilersametype.MultipleReconcilerSameTypeReconciler2; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class MultipleReconcilerSameTypeIT { | ||
|
||
public static final String TEST_RESOURCE_1 = "test1"; | ||
public static final String TEST_RESOURCE_2 = "test2"; | ||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(MultipleReconcilerSameTypeReconciler1.class) | ||
.withReconciler(MultipleReconcilerSameTypeReconciler2.class) | ||
.build(); | ||
|
||
|
||
@Test | ||
void multipleReconcilersBasedOnLeaderElection() { | ||
extension.create(testResource(TEST_RESOURCE_1, true)); | ||
extension.create(testResource(TEST_RESOURCE_2, false)); | ||
|
||
|
||
await().untilAsserted(() -> { | ||
assertThat(extension.getReconcilerOfType(MultipleReconcilerSameTypeReconciler1.class) | ||
.getNumberOfExecutions()).isEqualTo(1); | ||
assertThat(extension.getReconcilerOfType(MultipleReconcilerSameTypeReconciler2.class) | ||
.getNumberOfExecutions()).isEqualTo(1); | ||
|
||
var res1 = extension.get(MultipleReconcilerSameTypeCustomResource.class, TEST_RESOURCE_1); | ||
var res2 = extension.get(MultipleReconcilerSameTypeCustomResource.class, TEST_RESOURCE_2); | ||
assertThat(res1).isNotNull(); | ||
assertThat(res2).isNotNull(); | ||
assertThat(res1.getStatus().getReconciledBy()) | ||
.isEqualTo(MultipleReconcilerSameTypeReconciler1.class.getSimpleName()); | ||
assertThat(res2.getStatus().getReconciledBy()) | ||
.isEqualTo(MultipleReconcilerSameTypeReconciler2.class.getSimpleName()); | ||
}); | ||
} | ||
|
||
MultipleReconcilerSameTypeCustomResource testResource(String name, boolean type1) { | ||
var res = new MultipleReconcilerSameTypeCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(name) | ||
.build()); | ||
if (type1) { | ||
res.getMetadata().getLabels().put("reconciler", "1"); | ||
} | ||
return res; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
.../operator/sample/multiplereconcilersametype/MultipleReconcilerSameTypeCustomResource.java
This file contains 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.multiplereconcilersametype; | ||
|
||
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("mrst") | ||
public class MultipleReconcilerSameTypeCustomResource | ||
extends CustomResource<Void, MultipleReconcilerSameTypeStatus> | ||
implements Namespaced { | ||
} |
29 changes: 29 additions & 0 deletions
29
...sdk/operator/sample/multiplereconcilersametype/MultipleReconcilerSameTypeReconciler1.java
This file contains 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.multiplereconcilersametype; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; | ||
|
||
@ControllerConfiguration(labelSelector = "reconciler = 1") | ||
public class MultipleReconcilerSameTypeReconciler1 | ||
implements Reconciler<MultipleReconcilerSameTypeCustomResource>, TestExecutionInfoProvider { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
@Override | ||
public UpdateControl<MultipleReconcilerSameTypeCustomResource> reconcile( | ||
MultipleReconcilerSameTypeCustomResource resource, | ||
Context<MultipleReconcilerSameTypeCustomResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
|
||
resource.setStatus(new MultipleReconcilerSameTypeStatus()); | ||
resource.getStatus().setReconciledBy(getClass().getSimpleName()); | ||
return UpdateControl.patchStatus(resource); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...sdk/operator/sample/multiplereconcilersametype/MultipleReconcilerSameTypeReconciler2.java
This file contains 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,32 @@ | ||
package io.javaoperatorsdk.operator.sample.multiplereconcilersametype; | ||
|
||
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(labelSelector = "reconciler != 1") | ||
public class MultipleReconcilerSameTypeReconciler2 | ||
implements Reconciler<MultipleReconcilerSameTypeCustomResource>, TestExecutionInfoProvider { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
@Override | ||
public UpdateControl<MultipleReconcilerSameTypeCustomResource> reconcile( | ||
MultipleReconcilerSameTypeCustomResource resource, | ||
Context<MultipleReconcilerSameTypeCustomResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
|
||
resource.setStatus(new MultipleReconcilerSameTypeStatus()); | ||
resource.getStatus().setReconciledBy(getClass().getSimpleName()); | ||
return UpdateControl.patchStatus(resource); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...ratorsdk/operator/sample/multiplereconcilersametype/MultipleReconcilerSameTypeStatus.java
This file contains 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,14 @@ | ||
package io.javaoperatorsdk.operator.sample.multiplereconcilersametype; | ||
|
||
public class MultipleReconcilerSameTypeStatus { | ||
|
||
private String reconciledBy; | ||
|
||
public String getReconciledBy() { | ||
return reconciledBy; | ||
} | ||
|
||
public void setReconciledBy(String reconciledBy) { | ||
this.reconciledBy = reconciledBy; | ||
} | ||
} |