-
Notifications
You must be signed in to change notification settings - Fork 218
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
feat: allow manually specifying CRDs in test extension #2569
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: tests.crd.example | ||
spec: | ||
group: crd.example | ||
names: | ||
kind: Test | ||
singular: test | ||
plural: tests | ||
scope: Namespaced | ||
versions: | ||
- name: v1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
type: "object" | ||
served: true | ||
storage: true |
57 changes: 57 additions & 0 deletions
57
...ator-framework/src/test/java/io/javaoperatorsdk/operator/CRDMappingInTestExtensionIT.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,57 @@ | ||
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.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Kind; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
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.junit.LocallyRunOperatorExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class CRDMappingInTestExtensionIT { | ||
private final KubernetesClient client = new KubernetesClientBuilder().build(); | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension operator = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(new TestReconciler()) | ||
.withCRDMapping(TestCR.class, "src/test/crd/test.crd") | ||
.build(); | ||
|
||
@Test | ||
void correctlyAppliesManuallySpecifiedCRD() { | ||
operator.applyCrd(TestCR.class); | ||
|
||
final var crdClient = client.apiextensions().v1().customResourceDefinitions(); | ||
await().pollDelay(Duration.ofMillis(150)) | ||
.untilAsserted(() -> assertThat(crdClient.withName("tests.crd.example").get()).isNotNull()); | ||
} | ||
|
||
@Group("crd.example") | ||
@Version("v1") | ||
@Kind("Test") | ||
private static class TestCR extends CustomResource<Void, Void> implements Namespaced { | ||
} | ||
|
||
@ControllerConfiguration | ||
private static class TestReconciler implements Reconciler<TestCR> { | ||
@Override | ||
public UpdateControl<TestCR> reconcile(TestCR resource, Context<TestCR> context) | ||
throws Exception { | ||
return null; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding this API, not sure if we need to specify the class here, that is rather limiting, if someone for example uses dynamic API does not have the class. It would be enough to have just the path to cover even that possibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feature is meant for people who use the contract-first approach so they would have the class handy and most likely wouldn't use generic resources but I guess we could use the CRD name instead of the CR class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean we can make it even cover even that case (So dynamic resources handling with
GenericKubernetesResource
)Why we need a name or any id ? cannot we have an API just with a path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because that would imply parsing the file to check the name, which I would rather avoid. It also allows to potentially override a generated CRD with a manual one if needed (though, this probably is more of a corner case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, but should we rather parse the CRD and index it based on API version. And check if that is the same as resources API version mentioned in reconciler. (But always apply it even if there is no reconciler corresponding to it). This also delegates responsibility to the user to name it correctly, what we could definitely simplify in the background seamlessly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So basically I think we should parse the crd to have nicer API here. If you want I can do it in a separate PR. (But we should not release before)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please open a PR with that change.