-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: operator can be restarted (#1675)
- Loading branch information
Showing
10 changed files
with
185 additions
and
36 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
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
63 changes: 63 additions & 0 deletions
63
operator-framework/src/test/java/io/javaoperatorsdk/operator/OperatorRestartIT.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,63 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.restart.RestartTestCustomResource; | ||
import io.javaoperatorsdk.operator.sample.restart.RestartTestReconciler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class OperatorRestartIT { | ||
private final static KubernetesClient client = new KubernetesClientBuilder().build(); | ||
private final static Operator operator = new Operator(o -> o.withCloseClientOnStop(false)); | ||
private final static RestartTestReconciler reconciler = new RestartTestReconciler(); | ||
private static int reconcileNumberBeforeStop = 0; | ||
|
||
@BeforeAll | ||
static void registerReconciler() { | ||
LocallyRunOperatorExtension.applyCrd(RestartTestCustomResource.class, client); | ||
operator.register(reconciler); | ||
} | ||
|
||
@BeforeEach | ||
void startOperator() { | ||
operator.start(); | ||
} | ||
|
||
@AfterEach | ||
void stopOperator() { | ||
operator.stop(); | ||
} | ||
|
||
@Test | ||
@Order(1) | ||
void createResource() { | ||
client.resource(testCustomResource()).createOrReplace(); | ||
await().untilAsserted(() -> assertThat(reconciler.getNumberOfExecutions()).isGreaterThan(0)); | ||
reconcileNumberBeforeStop = reconciler.getNumberOfExecutions(); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
void reconcile() { | ||
await().untilAsserted(() -> assertThat(reconciler.getNumberOfExecutions()) | ||
.isGreaterThan(reconcileNumberBeforeStop)); | ||
} | ||
|
||
RestartTestCustomResource testCustomResource() { | ||
RestartTestCustomResource cr = new RestartTestCustomResource(); | ||
cr.setMetadata(new ObjectMetaBuilder() | ||
.withName("test1") | ||
.build()); | ||
return cr; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../src/test/java/io/javaoperatorsdk/operator/sample/restart/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,35 @@ | ||
package io.javaoperatorsdk.operator.sample.restart; | ||
|
||
import java.util.Map; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
|
||
@KubernetesDependent(labelSelector = "app=restart-test") | ||
public class ConfigMapDependentResource | ||
extends CRUDKubernetesDependentResource<ConfigMap, RestartTestCustomResource> { | ||
|
||
public static final String DATA_KEY = "key"; | ||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(RestartTestCustomResource primary, | ||
Context<RestartTestCustomResource> context) { | ||
return new ConfigMapBuilder() | ||
.withMetadata(new ObjectMetaBuilder() | ||
.withLabels(Map.of("app", "restart-test")) | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()) | ||
.withData(Map.of(DATA_KEY, primary.getMetadata().getName())) | ||
.build(); | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...k/src/test/java/io/javaoperatorsdk/operator/sample/restart/RestartTestCustomResource.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.restart; | ||
|
||
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("rt") | ||
public class RestartTestCustomResource | ||
extends CustomResource<Void, Void> | ||
implements Namespaced { | ||
} |
Oops, something went wrong.