-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: context getSecondary resource is activation condition aware (#2532
) Signed-off-by: Attila Mészáros <[email protected]>
- Loading branch information
Showing
12 changed files
with
252 additions
and
8 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
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: 16 additions & 0 deletions
16
...ain/java/io/javaoperatorsdk/operator/processing/event/NoEventSourceForClassException.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,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 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 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 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 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 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 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 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 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(); | ||
} | ||
} |