-
Notifications
You must be signed in to change notification settings - Fork 218
feat: integration test for cross reference dependent resource #1203
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
efb53b8
feat: integration test for cross reference dependent resource
csviri 8a7e2d6
Update operator-framework/src/test/java/io/javaoperatorsdk/operator/s…
csviri 540f354
reduce number of resources, format
csviri bf2c250
Merge branch 'concurrency-issue-dependents' of github.com:java-operat…
csviri 635582d
fix
csviri 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
53 changes: 53 additions & 0 deletions
53
...ator-framework/src/test/java/io/javaoperatorsdk/operator/DependentResourceCrossRefIT.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,53 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.javaoperatorsdk.operator.junit.LocalOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.dependentresourcecrossref.DependentResourceCrossRefReconciler; | ||
import io.javaoperatorsdk.operator.sample.dependentresourcecrossref.DependentResourceCrossRefResource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class DependentResourceCrossRefIT { | ||
|
||
public static final String TEST_RESOURCE_NAME = "test"; | ||
public static final int EXECUTION_NUMBER = 500; | ||
|
||
@RegisterExtension | ||
LocalOperatorExtension operator = | ||
LocalOperatorExtension.builder() | ||
.withReconciler(new DependentResourceCrossRefReconciler()) | ||
.build(); | ||
|
||
@Test | ||
void dependentResourceCanReferenceEachOther() { | ||
for (int i = 0; i < EXECUTION_NUMBER; i++) { | ||
operator.create(DependentResourceCrossRefResource.class, testResource(i)); | ||
} | ||
await() | ||
.pollDelay(Duration.ofMillis(150)) | ||
.untilAsserted( | ||
() -> { | ||
assertThat(operator | ||
.getReconcilerOfType(DependentResourceCrossRefReconciler.class) | ||
.isErrorHappened()).isFalse(); | ||
for (int i = 0; i < EXECUTION_NUMBER; i++) { | ||
assertThat(operator.get(ConfigMap.class, TEST_RESOURCE_NAME + i)).isNotNull(); | ||
assertThat(operator.get(Secret.class, TEST_RESOURCE_NAME + i)).isNotNull(); | ||
} | ||
}); | ||
} | ||
|
||
DependentResourceCrossRefResource testResource(int n) { | ||
var res = new DependentResourceCrossRefResource(); | ||
res.setMetadata(new ObjectMetaBuilder().withName(TEST_RESOURCE_NAME + n).build()); | ||
return res; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...torsdk/operator/sample/dependentresourcecrossref/DependentResourceCrossRefReconciler.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,97 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentresourcecrossref; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUKubernetesDependentResource; | ||
|
||
@ControllerConfiguration(dependents = { | ||
@Dependent(type = DependentResourceCrossRefReconciler.SecretDependentResource.class), | ||
@Dependent(type = DependentResourceCrossRefReconciler.ConfigMapDependentResource.class)}) | ||
public class DependentResourceCrossRefReconciler | ||
implements Reconciler<DependentResourceCrossRefResource>, | ||
ErrorStatusHandler<DependentResourceCrossRefResource> { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
private volatile boolean errorHappened = false; | ||
|
||
@Override | ||
public UpdateControl<DependentResourceCrossRefResource> reconcile( | ||
DependentResourceCrossRefResource resource, | ||
Context<DependentResourceCrossRefResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
|
||
@Override | ||
public ErrorStatusUpdateControl<DependentResourceCrossRefResource> updateErrorStatus( | ||
DependentResourceCrossRefResource resource, | ||
Context<DependentResourceCrossRefResource> context, Exception e) { | ||
errorHappened = true; | ||
return ErrorStatusUpdateControl.noStatusUpdate(); | ||
} | ||
|
||
public boolean isErrorHappened() { | ||
return errorHappened; | ||
} | ||
|
||
public static class SecretDependentResource extends | ||
CRUKubernetesDependentResource<Secret, DependentResourceCrossRefResource> { | ||
|
||
public SecretDependentResource() { | ||
super(Secret.class); | ||
} | ||
|
||
@Override | ||
protected Secret desired(DependentResourceCrossRefResource primary, | ||
Context<DependentResourceCrossRefResource> context) { | ||
Secret secret = new Secret(); | ||
secret.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
secret.setData(Map.of("key", Base64.getEncoder().encodeToString("secretData".getBytes()))); | ||
return secret; | ||
} | ||
} | ||
|
||
public static class ConfigMapDependentResource extends | ||
CRUKubernetesDependentResource<ConfigMap, DependentResourceCrossRefResource> { | ||
|
||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(DependentResourceCrossRefResource primary, | ||
Context<DependentResourceCrossRefResource> context) { | ||
var secret = context.getSecondaryResource(Secret.class); | ||
if (secret.isEmpty()) { | ||
throw new IllegalStateException("Secret is empty"); | ||
} | ||
ConfigMap configMap = new ConfigMap(); | ||
configMap.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
configMap | ||
.setData(Map.of("secretKey", new ArrayList<>(secret.get().getData().keySet()).get(0))); | ||
return configMap; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ratorsdk/operator/sample/dependentresourcecrossref/DependentResourceCrossRefResource.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.sample.dependentresourcecrossref; | ||
|
||
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.Kind; | ||
import io.fabric8.kubernetes.model.annotation.ShortNames; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.javaoperatorsdk") | ||
@Version("v1") | ||
@Kind("MaxIntervalTestCustomResource") | ||
@ShortNames("mit") | ||
public class DependentResourceCrossRefResource | ||
extends CustomResource<Void, DependentResourceCrossRefResourceStatus> | ||
implements Namespaced { | ||
} |
5 changes: 5 additions & 0 deletions
5
...dk/operator/sample/dependentresourcecrossref/DependentResourceCrossRefResourceStatus.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,5 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentresourcecrossref; | ||
|
||
public class DependentResourceCrossRefResourceStatus { | ||
|
||
} |
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.