Skip to content

Commit 04f1bad

Browse files
committed
Integration test
Signed-off-by: Attila Mészáros <csviri@gmail.com>
1 parent e970d46 commit 04f1bad

File tree

6 files changed

+150
-11
lines changed

6 files changed

+150
-11
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcher.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,11 @@ public R patchStatus(R resource, R originalResource) {
481481
}
482482

483483
public R patchResourceWithSSA(R resource) {
484-
var managedFields = resource.getMetadata().getManagedFields();
485-
resource.getMetadata().setManagedFields(null);
486-
try {
487-
return resource(resource).patch(new PatchContext.Builder()
488-
.withFieldManager(fieldManager)
489-
.withForce(true)
490-
.withPatchType(PatchType.SERVER_SIDE_APPLY)
491-
.build());
492-
} finally {
493-
resource.getMetadata().setManagedFields(managedFields);
494-
}
484+
return resource(resource).patch(new PatchContext.Builder()
485+
.withFieldManager(fieldManager)
486+
.withForce(true)
487+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
488+
.build());
495489
}
496490

497491
private Resource<R> resource(R resource) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.javaoperatorsdk.operator;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.RegisterExtension;
5+
6+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
7+
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
8+
import io.javaoperatorsdk.operator.sample.patchresourcewithssa.PatchResourceWithSSACustomResource;
9+
import io.javaoperatorsdk.operator.sample.patchresourcewithssa.PatchResourceWithSSAReconciler;
10+
import io.javaoperatorsdk.operator.sample.patchresourcewithssa.PatchResourceWithSSASpec;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.awaitility.Awaitility.await;
14+
15+
public class PatchResourceWithSSAIT {
16+
17+
public static final String RESOURCE_NAME = "test1";
18+
public static final String INIT_VALUE = "init value";
19+
20+
@RegisterExtension
21+
LocallyRunOperatorExtension extension =
22+
LocallyRunOperatorExtension.builder()
23+
.withReconciler(new PatchResourceWithSSAReconciler())
24+
.build();
25+
26+
@Test
27+
void reconcilerPatchesResourceWithSSA() {
28+
extension.create(testResource());
29+
30+
await().untilAsserted(() -> {
31+
var actualResource = extension.get(PatchResourceWithSSACustomResource.class, RESOURCE_NAME);
32+
33+
assertThat(actualResource.getSpec().getInitValue()).isEqualTo(INIT_VALUE);
34+
assertThat(actualResource.getSpec().getControllerManagedValue())
35+
.isEqualTo(PatchResourceWithSSAReconciler.ADDED_VALUE);
36+
// finalizer is added to the SSA patch in the background by the framework
37+
assertThat(actualResource.getMetadata().getFinalizers()).isNotEmpty();
38+
assertThat(actualResource.getStatus().isSuccessfullyReconciled()).isTrue();
39+
});
40+
}
41+
42+
PatchResourceWithSSACustomResource testResource() {
43+
var res = new PatchResourceWithSSACustomResource();
44+
res.setMetadata(new ObjectMetaBuilder()
45+
.withName(RESOURCE_NAME)
46+
.build());
47+
res.setSpec(new PatchResourceWithSSASpec());
48+
res.getSpec().setInitValue(INIT_VALUE);
49+
return res;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.javaoperatorsdk.operator.sample.patchresourcewithssa;
2+
3+
import io.fabric8.kubernetes.api.model.Namespaced;
4+
import io.fabric8.kubernetes.client.CustomResource;
5+
import io.fabric8.kubernetes.model.annotation.Group;
6+
import io.fabric8.kubernetes.model.annotation.ShortNames;
7+
import io.fabric8.kubernetes.model.annotation.Version;
8+
9+
@Group("sample.javaoperatorsdk")
10+
@Version("v1")
11+
@ShortNames("prs")
12+
public class PatchResourceWithSSACustomResource
13+
extends CustomResource<PatchResourceWithSSASpec, PatchResourceWithSSAStatus>
14+
implements Namespaced {
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.javaoperatorsdk.operator.sample.patchresourcewithssa;
2+
3+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
4+
import io.javaoperatorsdk.operator.api.reconciler.*;
5+
6+
@ControllerConfiguration
7+
public class PatchResourceWithSSAReconciler
8+
implements Reconciler<PatchResourceWithSSACustomResource>,
9+
Cleaner<PatchResourceWithSSACustomResource> {
10+
11+
public static final String ADDED_VALUE = "Added Value";
12+
13+
@Override
14+
public UpdateControl<PatchResourceWithSSACustomResource> reconcile(
15+
PatchResourceWithSSACustomResource resource,
16+
Context<PatchResourceWithSSACustomResource> context) {
17+
18+
var res = new PatchResourceWithSSACustomResource();
19+
res.setMetadata(new ObjectMetaBuilder()
20+
.withName(resource.getMetadata().getName())
21+
.withNamespace(resource.getMetadata().getNamespace())
22+
.build());
23+
24+
// first update the spec with missing value, then status in next reconciliation
25+
if (resource.getSpec().getControllerManagedValue() == null) {
26+
res.setSpec(new PatchResourceWithSSASpec());
27+
res.getSpec().setControllerManagedValue(ADDED_VALUE);
28+
return UpdateControl.patchResource(res);
29+
} else {
30+
res.setStatus(new PatchResourceWithSSAStatus());
31+
res.getStatus().setSuccessfullyReconciled(true);
32+
return UpdateControl.patchStatus(res);
33+
}
34+
}
35+
36+
@Override
37+
public DeleteControl cleanup(PatchResourceWithSSACustomResource resource,
38+
Context<PatchResourceWithSSACustomResource> context) {
39+
return DeleteControl.defaultDelete();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.javaoperatorsdk.operator.sample.patchresourcewithssa;
2+
3+
public class PatchResourceWithSSASpec {
4+
5+
private String initValue;
6+
private String controllerManagedValue;
7+
8+
public String getInitValue() {
9+
return initValue;
10+
}
11+
12+
public void setInitValue(String initValue) {
13+
this.initValue = initValue;
14+
}
15+
16+
public String getControllerManagedValue() {
17+
return controllerManagedValue;
18+
}
19+
20+
public void setControllerManagedValue(String controllerManagedValue) {
21+
this.controllerManagedValue = controllerManagedValue;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.javaoperatorsdk.operator.sample.patchresourcewithssa;
2+
3+
public class PatchResourceWithSSAStatus {
4+
5+
private boolean successfullyReconciled;
6+
7+
public boolean isSuccessfullyReconciled() {
8+
return successfullyReconciled;
9+
}
10+
11+
public void setSuccessfullyReconciled(boolean successfullyReconciled) {
12+
this.successfullyReconciled = successfullyReconciled;
13+
}
14+
}

0 commit comments

Comments
 (0)