-
Notifications
You must be signed in to change notification settings - Fork 218
feat: context getSecondary resource is activation condition aware #2532
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
3 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
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
16 changes: 16 additions & 0 deletions
16
...ain/java/io/javaoperatorsdk/operator/processing/event/NoEventSourceForClassException.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,16 @@ | ||
package io.javaoperatorsdk.operator.processing.event; | ||
|
||
import io.javaoperatorsdk.operator.OperatorException; | ||
|
||
public class NoEventSourceForClassException extends OperatorException { | ||
|
||
private Class<?> clazz; | ||
|
||
public NoEventSourceForClassException(Class<?> clazz) { | ||
this.clazz = clazz; | ||
} | ||
|
||
public Class<?> getClazz() { | ||
return clazz; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ork-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/DefaultContextTest.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,37 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.javaoperatorsdk.operator.processing.Controller; | ||
import io.javaoperatorsdk.operator.processing.event.EventSourceManager; | ||
import io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class DefaultContextTest { | ||
|
||
Secret primary = new Secret(); | ||
Controller<Secret> mockController = mock(Controller.class); | ||
|
||
DefaultContext<?> context = new DefaultContext<>(null, mockController, primary); | ||
|
||
@Test | ||
void getSecondaryResourceReturnsEmptyOptionalOnNonActivatedDRType() { | ||
var mockManager = mock(EventSourceManager.class); | ||
when(mockController.getEventSourceManager()).thenReturn(mockManager); | ||
when(mockController.workflowContainsDependentForType(ConfigMap.class)).thenReturn(true); | ||
when(mockManager.getEventSourceFor(any(), any())) | ||
.thenThrow(new NoEventSourceForClassException(ConfigMap.class)); | ||
|
||
var res = context.getSecondaryResource(ConfigMap.class); | ||
|
||
assertThat(res).isEmpty(); | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
...o/javaoperatorsdk/operator/workflow/getnonactivesecondary/ConfigMapDependentResource.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,28 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
|
||
public class ConfigMapDependentResource | ||
extends CRUDKubernetesDependentResource<ConfigMap, GetNonActiveSecondaryCustomResource> { | ||
|
||
public static final String DATA_KEY = "data"; | ||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(GetNonActiveSecondaryCustomResource primary, | ||
Context<GetNonActiveSecondaryCustomResource> context) { | ||
ConfigMap configMap = new ConfigMap(); | ||
configMap.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
return configMap; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../io/javaoperatorsdk/operator/workflow/getnonactivesecondary/FalseActivationCondition.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,17 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
import io.fabric8.openshift.api.model.Route; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition; | ||
|
||
public class FalseActivationCondition | ||
implements Condition<Route, GetNonActiveSecondaryCustomResource> { | ||
@Override | ||
public boolean isMet( | ||
DependentResource<Route, GetNonActiveSecondaryCustomResource> dependentResource, | ||
GetNonActiveSecondaryCustomResource primary, | ||
Context<GetNonActiveSecondaryCustomResource> context) { | ||
return false; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ratorsdk/operator/workflow/getnonactivesecondary/GetNonActiveSecondaryCustomResource.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,17 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
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("gnas") | ||
public class GetNonActiveSecondaryCustomResource | ||
extends CustomResource<Void, Void> | ||
implements Namespaced { | ||
|
||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...va/io/javaoperatorsdk/operator/workflow/getnonactivesecondary/RouteDependentResource.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,27 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.openshift.api.model.Route; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
|
||
public class RouteDependentResource | ||
extends CRUDKubernetesDependentResource<Route, GetNonActiveSecondaryCustomResource> { | ||
|
||
public RouteDependentResource() { | ||
super(Route.class); | ||
} | ||
|
||
@Override | ||
protected Route desired(GetNonActiveSecondaryCustomResource primary, | ||
Context<GetNonActiveSecondaryCustomResource> context) { | ||
// basically does not matter since this should not be called | ||
Route route = new Route(); | ||
route.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
|
||
return route; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...avaoperatorsdk/operator/workflow/getnonactivesecondary/WorkflowActivationConditionIT.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,40 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
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 static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class WorkflowActivationConditionIT { | ||
|
||
public static final String TEST_RESOURCE_NAME = "test1"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(WorkflowActivationConditionReconciler.class) | ||
.build(); | ||
|
||
@Test | ||
void reconciledOnVanillaKubernetesDespiteRouteInWorkflow() { | ||
extension.create(testResource()); | ||
|
||
await().untilAsserted(() -> { | ||
assertThat(extension.getReconcilerOfType(WorkflowActivationConditionReconciler.class) | ||
.getNumberOfReconciliationExecution()).isEqualTo(1); | ||
}); | ||
} | ||
|
||
private GetNonActiveSecondaryCustomResource testResource() { | ||
var res = new GetNonActiveSecondaryCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(TEST_RESOURCE_NAME) | ||
.build()); | ||
return res; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...torsdk/operator/workflow/getnonactivesecondary/WorkflowActivationConditionReconciler.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,40 @@ | ||
package io.javaoperatorsdk.operator.workflow.getnonactivesecondary; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.fabric8.openshift.api.model.Route; | ||
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.api.reconciler.Workflow; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
|
||
@Workflow(dependents = { | ||
@Dependent(type = ConfigMapDependentResource.class), | ||
@Dependent(type = RouteDependentResource.class, | ||
activationCondition = FalseActivationCondition.class) | ||
}) | ||
@ControllerConfiguration | ||
public class WorkflowActivationConditionReconciler | ||
implements Reconciler<GetNonActiveSecondaryCustomResource> { | ||
|
||
private final AtomicInteger numberOfReconciliationExecution = new AtomicInteger(0); | ||
|
||
@Override | ||
public UpdateControl<GetNonActiveSecondaryCustomResource> reconcile( | ||
GetNonActiveSecondaryCustomResource resource, | ||
Context<GetNonActiveSecondaryCustomResource> context) { | ||
|
||
// should not throw an exception even if the condition is false | ||
var route = context.getSecondaryResource(Route.class); | ||
|
||
numberOfReconciliationExecution.incrementAndGet(); | ||
|
||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
public int getNumberOfReconciliationExecution() { | ||
return numberOfReconciliationExecution.get(); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.