-
Notifications
You must be signed in to change notification settings - Fork 220
Update both status and custom resource #258
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 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0260dfb
update resource and status impl, unit test
csviri 3b5eec2
integration test (wip)
csviri fe94ed8
rename from CR
csviri 0045a54
remove obsolete retry config property
csviri b2c6915
Merge branch 'master' into update_both_status_and_cr
csviri 502f2d1
fixes on impl
csviri 3ddcbc4
package fix
csviri fe478ba
shortname fix crd
csviri 4508894
Update operator-framework/src/test/java/io/javaoperatorsdk/operator/d…
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
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
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
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
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
90 changes: 90 additions & 0 deletions
90
operator-framework/src/test/java/io/javaoperatorsdk/operator/UpdatingResAndSubResIT.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,90 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.DefaultKubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.javaoperatorsdk.operator.doubleupdate.subresource.DoubleUpdateTestCustomResource; | ||
import io.javaoperatorsdk.operator.doubleupdate.subresource.DoubleUpdateTestCustomResourceController; | ||
import io.javaoperatorsdk.operator.doubleupdate.subresource.DoubleUpdateTestCustomResourceSpec; | ||
import io.javaoperatorsdk.operator.doubleupdate.subresource.DoubleUpdateTestCustomResourceStatus; | ||
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceStatus; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static io.javaoperatorsdk.operator.IntegrationTestSupport.TEST_NAMESPACE; | ||
import static io.javaoperatorsdk.operator.TestUtils.waitXms; | ||
import static io.javaoperatorsdk.operator.doubleupdate.subresource.DoubleUpdateTestCustomResourceController.TEST_ANNOTATION; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class UpdatingResAndSubResIT { | ||
|
||
private IntegrationTestSupport integrationTestSupport = new IntegrationTestSupport(); | ||
|
||
@BeforeEach | ||
public void initAndCleanup() { | ||
KubernetesClient k8sClient = new DefaultKubernetesClient(); | ||
integrationTestSupport.initialize( | ||
k8sClient, new DoubleUpdateTestCustomResourceController(), "doubleupdate-test-crd.yaml"); | ||
integrationTestSupport.cleanup(); | ||
} | ||
|
||
@Test | ||
public void updatesSubResourceStatus() { | ||
integrationTestSupport.teardownIfSuccess( | ||
() -> { | ||
DoubleUpdateTestCustomResource resource = createTestCustomResource("1"); | ||
integrationTestSupport.getCrOperations().inNamespace(TEST_NAMESPACE).create(resource); | ||
|
||
awaitStatusUpdated(resource.getMetadata().getName()); | ||
// wait for sure, there are no more events | ||
waitXms(200); | ||
|
||
DoubleUpdateTestCustomResource customResource = | ||
(DoubleUpdateTestCustomResource) | ||
integrationTestSupport.getCustomResource(resource.getMetadata().getName()); | ||
assertThat(integrationTestSupport.numberOfControllerExecutions()).isEqualTo(1); | ||
assertThat(customResource.getStatus()) | ||
.isEqualTo(DoubleUpdateTestCustomResourceStatus.State.SUCCESS); | ||
assertThat(customResource.getMetadata().getAnnotations().get(TEST_ANNOTATION)) | ||
.isNotNull(); | ||
}); | ||
} | ||
|
||
void awaitStatusUpdated(String name) { | ||
await("cr status updated") | ||
.atMost(5, TimeUnit.SECONDS) | ||
.untilAsserted( | ||
() -> { | ||
DoubleUpdateTestCustomResource cr = | ||
(DoubleUpdateTestCustomResource) | ||
integrationTestSupport | ||
.getCrOperations() | ||
.inNamespace(TEST_NAMESPACE) | ||
.withName(name) | ||
.get(); | ||
assertThat(cr.getMetadata().getFinalizers()).hasSize(1); | ||
assertThat(cr).isNotNull(); | ||
assertThat(cr.getStatus()).isNotNull(); | ||
assertThat(cr.getStatus().getState()) | ||
.isEqualTo(SubResourceTestCustomResourceStatus.State.SUCCESS); | ||
}); | ||
} | ||
|
||
public DoubleUpdateTestCustomResource createTestCustomResource(String id) { | ||
DoubleUpdateTestCustomResource resource = new DoubleUpdateTestCustomResource(); | ||
resource.setMetadata( | ||
new ObjectMetaBuilder() | ||
.withName("doubleupdateresource-" + id) | ||
.withNamespace(TEST_NAMESPACE) | ||
.build()); | ||
resource.setKind("DoubleUpdateSample"); | ||
resource.setSpec(new DoubleUpdateTestCustomResourceSpec()); | ||
resource.getSpec().setValue(id); | ||
return resource; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../io/javaoperatorsdk/operator/doubleupdate/subresource/DoubleUpdateTestCustomResource.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,38 @@ | ||
package io.javaoperatorsdk.operator.doubleupdate.subresource; | ||
|
||
import io.fabric8.kubernetes.client.CustomResource; | ||
|
||
public class DoubleUpdateTestCustomResource extends CustomResource { | ||
|
||
private DoubleUpdateTestCustomResourceSpec spec; | ||
|
||
private DoubleUpdateTestCustomResourceStatus status; | ||
|
||
public DoubleUpdateTestCustomResourceSpec getSpec() { | ||
return spec; | ||
} | ||
|
||
public void setSpec(DoubleUpdateTestCustomResourceSpec spec) { | ||
this.spec = spec; | ||
} | ||
|
||
public DoubleUpdateTestCustomResourceStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(DoubleUpdateTestCustomResourceStatus status) { | ||
this.status = status; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TestCustomResource{" | ||
metacosm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ "spec=" | ||
+ spec | ||
+ ", status=" | ||
+ status | ||
+ ", extendedFrom=" | ||
+ super.toString() | ||
+ '}'; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...eratorsdk/operator/doubleupdate/subresource/DoubleUpdateTestCustomResourceController.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,54 @@ | ||
package io.javaoperatorsdk.operator.doubleupdate.subresource; | ||
|
||
import io.javaoperatorsdk.operator.TestExecutionInfoProvider; | ||
import io.javaoperatorsdk.operator.api.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Controller(crdName = DoubleUpdateTestCustomResourceController.CRD_NAME) | ||
public class DoubleUpdateTestCustomResourceController | ||
implements ResourceController<DoubleUpdateTestCustomResource>, TestExecutionInfoProvider { | ||
|
||
public static final String CRD_NAME = "doubleupdatesamples.sample.javaoperatorsdk"; | ||
private static final Logger log = | ||
LoggerFactory.getLogger(DoubleUpdateTestCustomResourceController.class); | ||
public static final String TEST_ANNOTATION = "TestAnnotation"; | ||
public static final String TEST_ANNOTATION_VALUE = "TestAnnotationValue"; | ||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
@Override | ||
public DeleteControl deleteResource( | ||
DoubleUpdateTestCustomResource resource, Context<DoubleUpdateTestCustomResource> context) { | ||
return DeleteControl.DEFAULT_DELETE; | ||
} | ||
|
||
@Override | ||
public UpdateControl<DoubleUpdateTestCustomResource> createOrUpdateResource( | ||
DoubleUpdateTestCustomResource resource, Context<DoubleUpdateTestCustomResource> context) { | ||
numberOfExecutions.addAndGet(1); | ||
|
||
log.info("Value: " + resource.getSpec().getValue()); | ||
|
||
resource.getMetadata().setAnnotations(new HashMap<>()); | ||
resource.getMetadata().getAnnotations().put(TEST_ANNOTATION, TEST_ANNOTATION_VALUE); | ||
ensureStatusExists(resource); | ||
resource.getStatus().setState(DoubleUpdateTestCustomResourceStatus.State.SUCCESS); | ||
|
||
return UpdateControl.updateCustomResourceAndStatusSubResource(resource); | ||
} | ||
|
||
private void ensureStatusExists(DoubleUpdateTestCustomResource resource) { | ||
DoubleUpdateTestCustomResourceStatus status = resource.getStatus(); | ||
if (status == null) { | ||
status = new DoubleUpdateTestCustomResourceStatus(); | ||
resource.setStatus(status); | ||
} | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...javaoperatorsdk/operator/doubleupdate/subresource/DoubleUpdateTestCustomResourceSpec.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,15 @@ | ||
package io.javaoperatorsdk.operator.doubleupdate.subresource; | ||
|
||
public class DoubleUpdateTestCustomResourceSpec { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public DoubleUpdateTestCustomResourceSpec setValue(String value) { | ||
this.value = value; | ||
return this; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...vaoperatorsdk/operator/doubleupdate/subresource/DoubleUpdateTestCustomResourceStatus.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,20 @@ | ||
package io.javaoperatorsdk.operator.doubleupdate.subresource; | ||
|
||
public class DoubleUpdateTestCustomResourceStatus { | ||
|
||
private State state; | ||
|
||
public State getState() { | ||
return state; | ||
} | ||
|
||
public DoubleUpdateTestCustomResourceStatus setState(State state) { | ||
this.state = state; | ||
return this; | ||
} | ||
|
||
public enum State { | ||
SUCCESS, | ||
ERROR | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
operator-framework/src/test/resources/io/javaoperatorsdk/operator/doubleupdate-test-crd.yaml
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,16 @@ | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: doubleupdatesamples.sample.javaoperatorsdk | ||
spec: | ||
group: sample.javaoperatorsdk | ||
version: v1 | ||
subresources: | ||
status: {} | ||
scope: Namespaced | ||
names: | ||
plural: doubleupdatesamples | ||
singular: doubleupdatesample | ||
kind: DoubleUpdateSample | ||
shortNames: | ||
- ss |
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.