Skip to content

Commit 16fcfe1

Browse files
committed
draft of changes to simplify the recording mechanism
1 parent 70f0eae commit 16fcfe1

File tree

9 files changed

+86
-427
lines changed

9 files changed

+86
-427
lines changed

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/RecentOperationEventFilter.java

-11
This file was deleted.

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

+10-34
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
3737
implements KubernetesClientAware,
3838
DependentResourceConfigurator<KubernetesDependentResourceConfig<R>> {
3939

40+
public static String PREVIOUS_ANNOTATION_KEY = "javaoperatorsdk.io/previous";
41+
4042
private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);
4143

4244
protected KubernetesClient client;
@@ -103,29 +105,6 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
103105
setEventSource(informerEventSource);
104106
}
105107

106-
107-
protected R handleCreate(R desired, P primary, Context<P> context) {
108-
ResourceID resourceID = ResourceID.fromResource(desired);
109-
try {
110-
prepareEventFiltering(desired, resourceID);
111-
return super.handleCreate(desired, primary, context);
112-
} catch (RuntimeException e) {
113-
cleanupAfterEventFiltering(resourceID);
114-
throw e;
115-
}
116-
}
117-
118-
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
119-
ResourceID resourceID = ResourceID.fromResource(desired);
120-
try {
121-
prepareEventFiltering(desired, resourceID);
122-
return super.handleUpdate(actual, desired, primary, context);
123-
} catch (RuntimeException e) {
124-
cleanupAfterEventFiltering(resourceID);
125-
throw e;
126-
}
127-
}
128-
129108
@SuppressWarnings("unused")
130109
public R create(R target, P primary, Context<P> context) {
131110
if (useSSA(context)) {
@@ -137,6 +116,8 @@ public R create(R target, P primary, Context<P> context) {
137116
target.getMetadata().setResourceVersion("1");
138117
}
139118
}
119+
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
120+
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY, id);
140121
final var resource = prepare(target, primary, "Creating");
141122
return useSSA(context)
142123
? resource
@@ -152,20 +133,24 @@ public R update(R actual, R target, P primary, Context<P> context) {
152133
actual.getMetadata().getResourceVersion());
153134
}
154135
R updatedResource;
136+
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
137+
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY,
138+
id + "," + actual.getMetadata().getResourceVersion());
155139
if (useSSA(context)) {
156140
target.getMetadata().setResourceVersion(actual.getMetadata().getResourceVersion());
157141
updatedResource = prepare(target, primary, "Updating")
158142
.fieldManager(context.getControllerConfiguration().fieldManager())
159143
.forceConflicts().serverSideApply();
160144
} else {
161145
var updatedActual = updaterMatcher.updateResource(actual, target, context);
162-
updatedResource = prepare(updatedActual, primary, "Updating").replace();
146+
updatedResource = prepare(updatedActual, primary, "Updating").update();
163147
}
164148
log.debug("Resource version after update: {}",
165149
updatedResource.getMetadata().getResourceVersion());
166150
return updatedResource;
167151
}
168152

153+
@Override
169154
public Result<R> match(R actualResource, P primary, Context<P> context) {
170155
final var desired = desired(primary, context);
171156
final boolean matches;
@@ -198,6 +183,7 @@ private boolean useSSA(Context<P> context) {
198183
.ssaBasedCreateUpdateMatchForDependentResources();
199184
}
200185

186+
@Override
201187
protected void handleDelete(P primary, R secondary, Context<P> context) {
202188
if (secondary != null) {
203189
client.resource(secondary).delete();
@@ -295,16 +281,6 @@ protected R desired(P primary, Context<P> context) {
295281
return super.desired(primary, context);
296282
}
297283

298-
private void prepareEventFiltering(R desired, ResourceID resourceID) {
299-
((InformerEventSource<R, P>) eventSource().orElseThrow())
300-
.prepareForCreateOrUpdateEventFiltering(resourceID, desired);
301-
}
302-
303-
private void cleanupAfterEventFiltering(ResourceID resourceID) {
304-
((InformerEventSource<R, P>) eventSource().orElseThrow())
305-
.cleanupOnCreateOrUpdateEventFiltering(resourceID);
306-
}
307-
308284
@Override
309285
public Optional<KubernetesDependentResourceConfig<R>> configuration() {
310286
return Optional.ofNullable(kubernetesDependentResourceConfig);

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/SSABasedGenericKubernetesResourceMatcher.java

+15
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,28 @@ public boolean matches(R actual, R desired, Context<?> context) {
8888
keepOnlyManagedFields(prunedActual, actualMap,
8989
managedFieldsEntry.getFieldsV1().getAdditionalProperties(), objectMapper);
9090

91+
removeSDKAnnotations(prunedActual);
92+
9193
removeIrrelevantValues(desiredMap);
9294

9395
log.debug("Pruned actual: \n {} \n desired: \n {} ", prunedActual, desiredMap);
9496

9597
return prunedActual.equals(desiredMap);
9698
}
9799

100+
private void removeSDKAnnotations(HashMap<String, Object> prunedActual) {
101+
Optional.ofNullable(((Map<String, Object>) prunedActual.get(METADATA_KEY)))
102+
.ifPresent(m -> m.computeIfPresent("annotations",
103+
(k, v) -> {
104+
var annotations = (Map<String, Object>) v;
105+
annotations.remove(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
106+
if (annotations.isEmpty()) {
107+
return null;
108+
}
109+
return annotations;
110+
}));
111+
}
112+
98113
@SuppressWarnings("unchecked")
99114
private static void removeIrrelevantValues(Map<String, Object> desiredMap) {
100115
var metadata = (Map<String, Object>) desiredMap.get(METADATA_KEY);

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/EventRecorder.java

-72
This file was deleted.

0 commit comments

Comments
 (0)