Skip to content

Commit ba009e8

Browse files
shawkinscsvirimetacosm
committed
changes to simplify the recording mechanism (#2012)
Co-authored-by: Attila Mészáros <[email protected]> Co-authored-by: Chris Laprun <[email protected]> Signed-off-by: csviri <[email protected]> Signed-off-by: Attila Mészáros <[email protected]>
1 parent e49604d commit ba009e8

File tree

13 files changed

+183
-497
lines changed

13 files changed

+183
-497
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public interface DependentResource<R, P extends HasMetadata> {
4444
* @param eventSourceContext context of event source initialization
4545
* @return an optional event source
4646
*/
47-
default Optional<ResourceEventSource<R, P>> eventSource(
47+
default Optional<? extends ResourceEventSource<R, P>> eventSource(
4848
EventSourceContext<P> eventSourceContext) {
4949
return Optional.empty();
5050
}

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/AbstractEventSourceHolderDependentResource.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected AbstractEventSourceHolderDependentResource(Class<R> resourceType) {
3434
this.resourceType = resourceType;
3535
}
3636

37-
public Optional<ResourceEventSource<R, P>> eventSource(EventSourceContext<P> context) {
37+
public Optional<T> eventSource(EventSourceContext<P> context) {
3838
// some sub-classes (e.g. KubernetesDependentResource) can have their event source created
3939
// before this method is called in the managed case, so only create the event source if it
4040
// hasn't already been set.
@@ -67,9 +67,8 @@ public void resolveEventSource(EventSourceRetriever<P> eventSourceRetriever) {
6767
* @param context for event sources
6868
* @return event source instance
6969
*/
70-
@SuppressWarnings("unchecked")
7170
public T initEventSource(EventSourceContext<P> context) {
72-
return (T) eventSource(context).orElseThrow();
71+
return eventSource(context).orElseThrow();
7372
}
7473

7574
@Override
@@ -96,7 +95,7 @@ protected void applyFilters() {
9695
this.eventSource.setGenericFilter(genericFilter);
9796
}
9897

99-
public Optional<ResourceEventSource<R, P>> eventSource() {
98+
public Optional<T> eventSource() {
10099
return Optional.ofNullable(eventSource);
101100
}
102101

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

+38-60
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
22

3-
import java.util.HashMap;
3+
import java.util.Map;
44
import java.util.Optional;
55
import java.util.Set;
66

77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

1010
import io.fabric8.kubernetes.api.model.HasMetadata;
11-
import io.fabric8.kubernetes.api.model.Namespaced;
1211
import io.fabric8.kubernetes.client.KubernetesClient;
1312
import io.fabric8.kubernetes.client.dsl.Resource;
1413
import io.javaoperatorsdk.operator.OperatorException;
@@ -103,29 +102,6 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
103102
setEventSource(informerEventSource);
104103
}
105104

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-
129105
@SuppressWarnings("unused")
130106
public R create(R target, P primary, Context<P> context) {
131107
if (useSSA(context)) {
@@ -137,6 +113,7 @@ public R create(R target, P primary, Context<P> context) {
137113
target.getMetadata().setResourceVersion("1");
138114
}
139115
}
116+
addMetadata(false, null, target, primary);
140117
final var resource = prepare(target, primary, "Creating");
141118
return useSSA(context)
142119
? resource
@@ -152,6 +129,7 @@ public R update(R actual, R target, P primary, Context<P> context) {
152129
actual.getMetadata().getResourceVersion());
153130
}
154131
R updatedResource;
132+
addMetadata(false, actual, target, primary);
155133
if (useSSA(context)) {
156134
updatedResource = prepare(target, primary, "Updating")
157135
.fieldManager(context.getControllerConfiguration().fieldManager())
@@ -165,38 +143,58 @@ public R update(R actual, R target, P primary, Context<P> context) {
165143
return updatedResource;
166144
}
167145

146+
@Override
168147
public Result<R> match(R actualResource, P primary, Context<P> context) {
169148
final var desired = desired(primary, context);
149+
return match(actualResource, desired, primary, updaterMatcher, context);
150+
}
151+
152+
@SuppressWarnings({"unused", "unchecked"})
153+
public Result<R> match(R actualResource, R desired, P primary, Context<P> context) {
154+
return match(actualResource, desired, primary,
155+
(ResourceUpdaterMatcher<R>) GenericResourceUpdaterMatcher
156+
.updaterMatcherFor(actualResource.getClass()),
157+
context);
158+
}
159+
160+
public Result<R> match(R actualResource, R desired, P primary, ResourceUpdaterMatcher<R> matcher,
161+
Context<P> context) {
170162
final boolean matches;
163+
addMetadata(true, actualResource, desired, primary);
171164
if (useSSA(context)) {
172-
addReferenceHandlingMetadata(desired, primary);
173165
matches = SSABasedGenericKubernetesResourceMatcher.getInstance()
174166
.matches(actualResource, desired, context);
175167
} else {
176-
matches = updaterMatcher.matches(actualResource, desired, context);
168+
matches = matcher.matches(actualResource, desired, context);
177169
}
178170
return Result.computed(matches, desired);
179171
}
180172

181-
@SuppressWarnings("unused")
182-
public Result<R> match(R actualResource, R desired, P primary, Context<P> context) {
183-
if (useSSA(context)) {
184-
addReferenceHandlingMetadata(desired, primary);
185-
var matches = SSABasedGenericKubernetesResourceMatcher.getInstance()
186-
.matches(actualResource, desired, context);
187-
return Result.computed(matches, desired);
188-
} else {
189-
return GenericKubernetesResourceMatcher
190-
.match(desired, actualResource, true,
191-
false, false, context);
173+
protected void addMetadata(boolean forMatch, R actualResource, final R target, P primary) {
174+
if (forMatch) { // keep the current
175+
String actual = actualResource.getMetadata().getAnnotations()
176+
.get(InformerEventSource.PREVIOUS_ANNOTATION_KEY);
177+
Map<String, String> annotations = target.getMetadata().getAnnotations();
178+
if (actual != null) {
179+
annotations.put(InformerEventSource.PREVIOUS_ANNOTATION_KEY, actual);
180+
} else {
181+
annotations.remove(InformerEventSource.PREVIOUS_ANNOTATION_KEY);
182+
}
183+
} else { // set a new one
184+
eventSource().orElseThrow().addPreviousAnnotation(
185+
Optional.ofNullable(actualResource).map(r -> r.getMetadata().getResourceVersion())
186+
.orElse(null),
187+
target);
192188
}
189+
addReferenceHandlingMetadata(target, primary);
193190
}
194191

195192
private boolean useSSA(Context<P> context) {
196193
return context.getControllerConfiguration().getConfigurationService()
197194
.ssaBasedCreateUpdateMatchForDependentResources();
198195
}
199196

197+
@Override
200198
protected void handleDelete(P primary, R secondary, Context<P> context) {
201199
if (secondary != null) {
202200
client.resource(secondary).delete();
@@ -214,13 +212,7 @@ protected Resource<R> prepare(R desired, P primary, String actionName) {
214212
desired.getClass(),
215213
ResourceID.fromResource(desired));
216214

217-
addReferenceHandlingMetadata(desired, primary);
218-
219-
if (desired instanceof Namespaced) {
220-
return client.resource(desired).inNamespace(desired.getMetadata().getNamespace());
221-
} else {
222-
return client.resource(desired);
223-
}
215+
return client.resource(desired);
224216
}
225217

226218
protected void addReferenceHandlingMetadata(R desired, P primary) {
@@ -254,7 +246,7 @@ protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> cont
254246
"Using default configuration for {} KubernetesDependentResource, call configureWith to provide configuration",
255247
resourceType().getSimpleName());
256248
}
257-
return (InformerEventSource<R, P>) eventSource().orElseThrow();
249+
return eventSource().orElseThrow();
258250
}
259251

260252
private boolean useDefaultAnnotationsToIdentifyPrimary() {
@@ -263,10 +255,6 @@ private boolean useDefaultAnnotationsToIdentifyPrimary() {
263255

264256
private void addDefaultSecondaryToPrimaryMapperAnnotations(R desired, P primary) {
265257
var annotations = desired.getMetadata().getAnnotations();
266-
if (annotations == null) {
267-
annotations = new HashMap<>();
268-
desired.getMetadata().setAnnotations(annotations);
269-
}
270258
annotations.put(Mappers.DEFAULT_ANNOTATION_FOR_NAME, primary.getMetadata().getName());
271259
var primaryNamespaces = primary.getMetadata().getNamespace();
272260
if (primaryNamespaces != null) {
@@ -294,16 +282,6 @@ protected R desired(P primary, Context<P> context) {
294282
return super.desired(primary, context);
295283
}
296284

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

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)