Skip to content

Commit f93fddc

Browse files
committed
draft of changes to simplify the recording mechanism
1 parent 3b72bb4 commit f93fddc

File tree

8 files changed

+77
-425
lines changed

8 files changed

+77
-425
lines changed

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

-11
This file was deleted.

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,35 +105,14 @@ 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)) {
132111
// setting resource version for SSA so only created if it doesn't exist already
133112
target.getMetadata().setResourceVersion("1");
134113
}
114+
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
115+
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY, id);
135116
final var resource = prepare(target, primary, "Creating");
136117
return useSSA(context)
137118
? resource
@@ -147,20 +128,24 @@ public R update(R actual, R target, P primary, Context<P> context) {
147128
actual.getMetadata().getResourceVersion());
148129
}
149130
R updatedResource;
131+
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
132+
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY,
133+
id + "," + actual.getMetadata().getResourceVersion());
150134
if (useSSA(context)) {
151135
target.getMetadata().setResourceVersion(actual.getMetadata().getResourceVersion());
152136
updatedResource = prepare(target, primary, "Updating")
153137
.fieldManager(context.getControllerConfiguration().fieldManager())
154138
.forceConflicts().serverSideApply();
155139
} else {
156140
var updatedActual = updaterMatcher.updateResource(actual, target, context);
157-
updatedResource = prepare(updatedActual, primary, "Updating").replace();
141+
updatedResource = prepare(updatedActual, primary, "Updating").update();
158142
}
159143
log.debug("Resource version after update: {}",
160144
updatedResource.getMetadata().getResourceVersion());
161145
return updatedResource;
162146
}
163147

148+
@Override
164149
public Result<R> match(R actualResource, P primary, Context<P> context) {
165150
final var desired = desired(primary, context);
166151
final boolean matches;
@@ -193,6 +178,7 @@ private boolean useSSA(Context<P> context) {
193178
.ssaBasedCreateUpdateMatchForDependentResources();
194179
}
195180

181+
@Override
196182
protected void handleDelete(P primary, R secondary, Context<P> context) {
197183
if (secondary != null) {
198184
client.resource(secondary).delete();
@@ -290,16 +276,6 @@ protected R desired(P primary, Context<P> context) {
290276
return super.desired(primary, context);
291277
}
292278

293-
private void prepareEventFiltering(R desired, ResourceID resourceID) {
294-
((InformerEventSource<R, P>) eventSource().orElseThrow())
295-
.prepareForCreateOrUpdateEventFiltering(resourceID, desired);
296-
}
297-
298-
private void cleanupAfterEventFiltering(ResourceID resourceID) {
299-
((InformerEventSource<R, P>) eventSource().orElseThrow())
300-
.cleanupOnCreateOrUpdateEventFiltering(resourceID);
301-
}
302-
303279
@Override
304280
public Optional<KubernetesDependentResourceConfig<R>> configuration() {
305281
return Optional.ofNullable(kubernetesDependentResourceConfig);

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

+9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ public boolean matches(R actual, R desired, Context<?> context) {
9090
keepOnlyManagedFields(prunedActual, actualMap,
9191
managedFieldsEntry.getFieldsV1().getAdditionalProperties(), objectMapper);
9292

93+
((Map<String, Object>) prunedActual.get(METADATA_KEY)).computeIfPresent("annotations", (k, v) -> {
94+
var annotations = (Map<String, Object>)v;
95+
annotations.remove(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
96+
if (annotations.isEmpty()) {
97+
return null;
98+
}
99+
return annotations;
100+
});
101+
93102
removeIrrelevantValues(desiredMap);
94103

95104
if (LoggingUtils.isNotSensitiveResource(desired)) {

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)