Skip to content

Commit 20a444b

Browse files
committed
wip
1 parent 1c28955 commit 20a444b

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ public R getResource(String namespace, String name) {
382382

383383
public R serverSideApplyLockResource(R resource, R originalResource) {
384384
var patchContext = PatchContext.of(PatchType.SERVER_SIDE_APPLY);
385-
// todo verify
386385
patchContext.setForce(true);
387386
return resource(originalResource).patch(patchContext,
388387
resource);

operator-framework/src/test/java/io/javaoperatorsdk/operator/CachePruneIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,19 @@ void pruningRelatedBehavior() {
4949
var configMap = operator.get(ConfigMap.class, TEST_RESOURCE_NAME);
5050
assertThat(actual.getStatus().getCreated()).isTrue();
5151
assertThat(actual.getMetadata().getLabels()).isNotEmpty();
52+
assertThat(actual.getMetadata().getFinalizers()).isNotEmpty();
5253
assertThat(configMap.getData()).containsEntry(DATA_KEY, UPDATED_DATA);
5354
assertThat(configMap.getMetadata().getLabels()).isNotEmpty();
5455
});
56+
57+
operator.delete(updated);
58+
59+
await().untilAsserted(() -> {
60+
var actual = operator.get(CachePruneCustomResource.class, TEST_RESOURCE_NAME);
61+
var configMap = operator.get(ConfigMap.class, TEST_RESOURCE_NAME);
62+
assertThat(configMap).isNull();
63+
assertThat(actual).isNull();
64+
});
5565
}
5666

5767
CachePruneCustomResource testResource() {

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/cacheprune/CachePruneReconciler.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@
44
import java.util.Map;
55
import java.util.concurrent.atomic.AtomicInteger;
66

7+
import org.assertj.core.util.Lists;
8+
79
import io.fabric8.kubernetes.api.model.ConfigMap;
10+
import io.fabric8.kubernetes.api.model.FieldsV1Builder;
11+
import io.fabric8.kubernetes.api.model.ManagedFieldsEntryBuilder;
812
import io.fabric8.kubernetes.api.model.ObjectMeta;
913
import io.fabric8.kubernetes.client.KubernetesClient;
1014
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
11-
import io.fabric8.kubernetes.client.dsl.base.PatchType;
1215
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
1316
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
1417
import io.javaoperatorsdk.operator.api.reconciler.*;
1518
import io.javaoperatorsdk.operator.junit.KubernetesClientAware;
1619
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
1720
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
1821

19-
@ControllerConfiguration
20-
// @ControllerConfiguration(itemStore = LabelRemovingItemStore.class)
22+
@ControllerConfiguration(itemStore = LabelRemovingItemStore.class)
2123
public class CachePruneReconciler
2224
implements Reconciler<CachePruneCustomResource>,
2325
EventSourceInitializer<CachePruneCustomResource>,
2426
Cleaner<CachePruneCustomResource>, KubernetesClientAware {
2527

2628
public static final String DATA_KEY = "data";
29+
public static final String FIELD_MANAGER = "controller";
30+
public static final String SECONDARY_CREATE_FIELD_MANAGER = "creator";
2731
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
2832
private KubernetesClient client;
2933

@@ -39,11 +43,15 @@ public UpdateControl<CachePruneCustomResource> reconcile(
3943
.equals(resource.getSpec().getData())) {
4044
var cloned = ConfigurationServiceProvider.instance().getResourceCloner().clone(cm);
4145
cloned.getData().put(DATA_KEY, resource.getSpec().getData());
42-
var res = client.configMaps().resource(cm)
43-
.patch(PatchContext.of(PatchType.SERVER_SIDE_APPLY), cloned);
44-
System.out.println(res);
46+
var patchContext = patchContextWithFieldManager(FIELD_MANAGER);
47+
// setting new field manager since we don't control label anymore
48+
patchContext.setForce(true);
49+
patchContext.setFieldManager(FIELD_MANAGER);
50+
client.configMaps().resource(cm)
51+
.patch(patchContext, cloned);
4552
}
46-
}, () -> client.configMaps().resource(configMap(resource)).create());
53+
}, () -> client.configMaps().resource(configMap(resource))
54+
.patch(patchContextWithFieldManager(SECONDARY_CREATE_FIELD_MANAGER)));
4755

4856
resource.setStatus(new CachePruneStatus());
4957
resource.getStatus().setCreated(true);
@@ -55,12 +63,18 @@ public int getNumberOfExecutions() {
5563
return numberOfExecutions.get();
5664
}
5765

66+
private PatchContext patchContextWithFieldManager(String fieldManager) {
67+
PatchContext patchContext = new PatchContext();
68+
patchContext.setFieldManager(fieldManager);
69+
return patchContext;
70+
}
71+
5872
@Override
5973
public Map<String, EventSource> prepareEventSources(
6074
EventSourceContext<CachePruneCustomResource> context) {
6175
InformerEventSource<ConfigMap, CachePruneCustomResource> configMapEventSource =
6276
new InformerEventSource<>(InformerConfiguration.from(ConfigMap.class, context)
63-
// .withItemStore(new LabelRemovingItemStore<>())
77+
.withItemStore(new LabelRemovingItemStore<>())
6478
.build(),
6579
context);
6680
return EventSourceInitializer.nameEventSources(configMapEventSource);
@@ -71,6 +85,16 @@ ConfigMap configMap(CachePruneCustomResource resource) {
7185
configMap.setMetadata(new ObjectMeta());
7286
configMap.getMetadata().setName(resource.getMetadata().getName());
7387
configMap.getMetadata().setNamespace(resource.getMetadata().getNamespace());
88+
configMap.getMetadata().setManagedFields(Lists.list(new ManagedFieldsEntryBuilder()
89+
.withApiVersion("v1")
90+
.withFieldsType("FieldsV1")
91+
.withManager("fabric8")
92+
.withOperation("Apply")
93+
.withTime("2022-11-30T09:27:00.000Z")
94+
.withFieldsV1(new FieldsV1Builder()
95+
.withAdditionalProperties(Map.of("f:data", Map.of("f:data", Map.of())))
96+
.build())
97+
.build()));
7498
configMap.setData(Map.of(DATA_KEY, resource.getSpec().getData()));
7599
HashMap<String, String> labels = new HashMap<>();
76100
labels.put("mylabel", "val");

0 commit comments

Comments
 (0)