-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleanup of resources with activation condition (#2223)
Signed-off-by: Attila Mészáros <csviri@gmail.com>
- Loading branch information
Showing
11 changed files
with
227 additions
and
19 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
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
79 changes: 79 additions & 0 deletions
79
...ator-framework/src/test/java/io/javaoperatorsdk/operator/WorkflowActivationCleanupIT.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,79 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.*; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespace; | ||
import io.fabric8.kubernetes.api.model.NamespaceBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupCustomResource; | ||
import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupReconciler; | ||
import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupSpec; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class WorkflowActivationCleanupIT { | ||
|
||
private final KubernetesClient client = new KubernetesClientBuilder().build(); | ||
private Operator operator; | ||
|
||
private String testNamespace; | ||
|
||
@BeforeEach | ||
void beforeEach(TestInfo testInfo) { | ||
LocallyRunOperatorExtension.applyCrd(WorkflowActivationCleanupCustomResource.class, | ||
client); | ||
|
||
testInfo.getTestMethod() | ||
.ifPresent(method -> testNamespace = KubernetesResourceUtil.sanitizeName(method.getName())); | ||
client.namespaces().resource(testNamespace(testNamespace)).create(); | ||
operator = new Operator(o -> o.withCloseClientOnStop(false)); | ||
operator.register(new WorkflowActivationCleanupReconciler(), | ||
o -> o.settingNamespaces(testNamespace)); | ||
} | ||
|
||
@AfterEach | ||
void stopOperator() { | ||
client.namespaces().withName(testNamespace).delete(); | ||
await().untilAsserted(() -> { | ||
var ns = client.namespaces().withName(testNamespace).get(); | ||
assertThat(ns).isNull(); | ||
}); | ||
operator.stop(); | ||
} | ||
|
||
@Test | ||
void testCleanupOnMarkedResourceOnOperatorStartup() { | ||
var resource = client.resource(testResourceWithFinalizer()).create(); | ||
client.resource(resource).delete(); | ||
operator.start(); | ||
|
||
await().untilAsserted(() -> { | ||
var res = client.resource(resource).get(); | ||
assertThat(res).isNull(); | ||
}); | ||
} | ||
|
||
private WorkflowActivationCleanupCustomResource testResourceWithFinalizer() { | ||
var resource = new WorkflowActivationCleanupCustomResource(); | ||
resource.setMetadata(new ObjectMetaBuilder() | ||
.withName("test1") | ||
.withFinalizers("workflowactivationcleanupcustomresources.sample.javaoperatorsdk/finalizer") | ||
.withNamespace(testNamespace) | ||
.build()); | ||
resource.setSpec(new WorkflowActivationCleanupSpec()); | ||
resource.getSpec().setValue("val1"); | ||
return resource; | ||
} | ||
|
||
private Namespace testNamespace(String name) { | ||
return new NamespaceBuilder().withMetadata(new ObjectMetaBuilder() | ||
.withName(name) | ||
.build()).build(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...javaoperatorsdk/operator/sample/workflowactivationcleanup/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,31 @@ | ||
package io.javaoperatorsdk.operator.sample.workflowactivationcleanup; | ||
|
||
import java.util.Map; | ||
|
||
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.CRUDNoGCKubernetesDependentResource; | ||
|
||
public class ConfigMapDependentResource | ||
extends | ||
CRUDNoGCKubernetesDependentResource<ConfigMap, WorkflowActivationCleanupCustomResource> { | ||
|
||
public static final String DATA_KEY = "data"; | ||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(WorkflowActivationCleanupCustomResource primary, | ||
Context<WorkflowActivationCleanupCustomResource> context) { | ||
ConfigMap configMap = new ConfigMap(); | ||
configMap.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
configMap.setData(Map.of(DATA_KEY, primary.getSpec().getValue())); | ||
return configMap; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...o/javaoperatorsdk/operator/sample/workflowactivationcleanup/TestActivcationCondition.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,18 @@ | ||
package io.javaoperatorsdk.operator.sample.workflowactivationcleanup; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
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 TestActivcationCondition | ||
implements Condition<ConfigMap, WorkflowActivationCleanupCustomResource> { | ||
|
||
@Override | ||
public boolean isMet( | ||
DependentResource<ConfigMap, WorkflowActivationCleanupCustomResource> dependentResource, | ||
WorkflowActivationCleanupCustomResource primary, | ||
Context<WorkflowActivationCleanupCustomResource> context) { | ||
return true; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...dk/operator/sample/workflowactivationcleanup/WorkflowActivationCleanupCustomResource.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.sample.workflowactivationcleanup; | ||
|
||
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("wacc") | ||
public class WorkflowActivationCleanupCustomResource | ||
extends CustomResource<WorkflowActivationCleanupSpec, Void> | ||
implements Namespaced { | ||
|
||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...torsdk/operator/sample/workflowactivationcleanup/WorkflowActivationCleanupReconciler.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.sample.workflowactivationcleanup; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
|
||
@ControllerConfiguration(dependents = { | ||
@Dependent(type = ConfigMapDependentResource.class, | ||
activationCondition = TestActivcationCondition.class), | ||
}) | ||
public class WorkflowActivationCleanupReconciler | ||
implements Reconciler<WorkflowActivationCleanupCustomResource>, | ||
Cleaner<WorkflowActivationCleanupCustomResource> { | ||
|
||
@Override | ||
public UpdateControl<WorkflowActivationCleanupCustomResource> reconcile( | ||
WorkflowActivationCleanupCustomResource resource, | ||
Context<WorkflowActivationCleanupCustomResource> context) { | ||
|
||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
@Override | ||
public DeleteControl cleanup(WorkflowActivationCleanupCustomResource resource, | ||
Context<WorkflowActivationCleanupCustomResource> context) { | ||
return DeleteControl.defaultDelete(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...aoperatorsdk/operator/sample/workflowactivationcleanup/WorkflowActivationCleanupSpec.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.workflowactivationcleanup; | ||
|
||
public class WorkflowActivationCleanupSpec { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} |