Skip to content

Commit c3d1c80

Browse files
committed
removed old filters
1 parent 1759ec3 commit c3d1c80

File tree

4 files changed

+62
-154
lines changed

4 files changed

+62
-154
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerResourceEventSource.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getName;
2121
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getUID;
2222
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getVersion;
23+
import static io.javaoperatorsdk.operator.processing.event.source.controller.InternalEventFilters.*;
2324

2425
public class ControllerResourceEventSource<T extends HasMetadata>
2526
extends ManagedInformerEventSource<T, T, ControllerConfiguration<T>>
@@ -34,19 +35,19 @@ public class ControllerResourceEventSource<T extends HasMetadata>
3435
public ControllerResourceEventSource(Controller<T> controller) {
3536
super(controller.getCRClient(), controller.getConfiguration());
3637
this.controller = controller;
37-
var filters = new ResourceEventFilter[] {
38-
ResourceEventFilters.finalizerNeededAndApplied(),
39-
ResourceEventFilters.markedForDeletion(),
40-
ResourceEventFilters.generationAware(),
41-
};
42-
if (controller.getConfiguration().getEventFilter() != null) {
43-
legacyFilters =
44-
controller.getConfiguration().getEventFilter().and(ResourceEventFilters.or(filters));
45-
} else {
46-
legacyFilters = ResourceEventFilters.or(filters);
47-
}
38+
39+
BiPredicate<T, T> internalOnUpdateFilter = onUpdateFinalizerNeededAndApplied(controller)
40+
.or(onUpdateGenerationAware(controller.getConfiguration().isGenerationAware()))
41+
.or(onUpdateMarkedForDeletion());
42+
43+
legacyFilters = controller.getConfiguration().getEventFilter();
44+
45+
// by default the on add should be processed in all cases regarding internal filters
4846
controller.getConfiguration().onAddFilter().ifPresent(this::setOnAddFilter);
49-
controller.getConfiguration().onUpdateFilter().ifPresent(this::setOnUpdateFilter);
47+
48+
controller.getConfiguration().onUpdateFilter()
49+
.ifPresentOrElse(filter -> setOnUpdateFilter(filter.and(internalOnUpdateFilter)),
50+
() -> setOnUpdateFilter(internalOnUpdateFilter));
5051
}
5152

5253
@Override
@@ -64,7 +65,8 @@ public void eventReceived(ResourceAction action, T resource, T oldResource) {
6465
log.debug("Event received for resource: {}", getName(resource));
6566
MDCUtils.addResourceInfo(resource);
6667
controller.getEventSourceManager().broadcastOnResourceEvent(action, resource, oldResource);
67-
if (legacyFilters.acceptChange(controller, oldResource, resource)
68+
if ((legacyFilters == null ||
69+
legacyFilters.acceptChange(controller, oldResource, resource))
6870
&& acceptFilters(action, resource, oldResource)) {
6971
getEventHandler().handleEvent(
7072
new ResourceEvent(action, ResourceID.fromResource(resource), resource));
@@ -83,7 +85,7 @@ private boolean acceptFilters(ResourceAction action, T resource, T oldResource)
8385
case ADDED:
8486
return onAddFilter == null || onAddFilter.test(resource);
8587
case UPDATED:
86-
return onUpdateFilter == null || onUpdateFilter.test(resource, oldResource);
88+
return onUpdateFilter.test(resource, oldResource);
8789
}
8890
return true;
8991
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
package io.javaoperatorsdk.operator.processing.event.source.controller;
22

3+
import java.util.function.BiPredicate;
4+
5+
import io.fabric8.kubernetes.api.model.HasMetadata;
6+
import io.javaoperatorsdk.operator.processing.Controller;
7+
38
public class InternalEventFilters {
9+
10+
static <T extends HasMetadata> BiPredicate<T, T> onUpdateMarkedForDeletion() {
11+
return (newResource, oldResource) -> newResource.isMarkedForDeletion();
12+
}
13+
14+
static <T extends HasMetadata> BiPredicate<T, T> onUpdateGenerationAware(
15+
boolean generationAware) {
16+
17+
return (newResource, oldResource) -> {
18+
if (!generationAware) {
19+
return true;
20+
}
21+
return oldResource.getMetadata().getGeneration() < newResource
22+
.getMetadata().getGeneration();
23+
};
24+
}
25+
26+
static <T extends HasMetadata> BiPredicate<T, T> onUpdateFinalizerNeededAndApplied(
27+
Controller<T> controller) {
28+
return (newResource, oldResource) -> {
29+
if (controller.useFinalizer()) {
30+
final var finalizer = controller.getConfiguration().getFinalizerName();
31+
boolean oldFinalizer = oldResource.hasFinalizer(finalizer);
32+
boolean newFinalizer = newResource.hasFinalizer(finalizer);
33+
// accepts event if old did not have finalizer, since it was just added, so the event needs
34+
// to
35+
// be published.
36+
return !newFinalizer || !oldFinalizer;
37+
} else {
38+
return false;
39+
}
40+
};
41+
}
42+
443
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/controller/ResourceEventFilters.java

Lines changed: 1 addition & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,12 @@
55
/**
66
* Convenience implementations of, and utility methods for, {@link ResourceEventFilter}.
77
*/
8+
@Deprecated
89
public final class ResourceEventFilters {
910

10-
private static final ResourceEventFilter<HasMetadata> USE_FINALIZER =
11-
(controller, oldResource, newResource) -> {
12-
if (controller.useFinalizer()) {
13-
final var finalizer = controller.getConfiguration().getFinalizerName();
14-
boolean oldFinalizer = oldResource == null || oldResource.hasFinalizer(finalizer);
15-
boolean newFinalizer = newResource.hasFinalizer(finalizer);
16-
17-
return !newFinalizer || !oldFinalizer;
18-
} else {
19-
return false;
20-
}
21-
};
22-
23-
private static final ResourceEventFilter<HasMetadata> GENERATION_AWARE =
24-
(controller, oldResource, newResource) -> {
25-
final var generationAware = controller.getConfiguration().isGenerationAware();
26-
return oldResource == null || !generationAware ||
27-
oldResource.getMetadata().getGeneration() < newResource.getMetadata().getGeneration();
28-
};
29-
3011
private static final ResourceEventFilter<HasMetadata> PASSTHROUGH =
3112
(configuration, oldResource, newResource) -> true;
3213

33-
private static final ResourceEventFilter<HasMetadata> NONE =
34-
(configuration, oldResource, newResource) -> false;
35-
36-
private static final ResourceEventFilter<HasMetadata> MARKED_FOR_DELETION =
37-
(configuration, oldResource, newResource) -> newResource.isMarkedForDeletion();
38-
3914
private ResourceEventFilters() {}
4015

4116
/**
@@ -49,117 +24,4 @@ public static <T extends HasMetadata> ResourceEventFilter<T> passthrough() {
4924
return (ResourceEventFilter<T>) PASSTHROUGH;
5025
}
5126

52-
/**
53-
* Retrieves a filter that reject all events.
54-
*
55-
* @param <T> the type of custom resource the filter should handle
56-
* @return a filter that reject all events
57-
*/
58-
@SuppressWarnings("unchecked")
59-
public static <T extends HasMetadata> ResourceEventFilter<T> none() {
60-
return (ResourceEventFilter<T>) NONE;
61-
}
62-
63-
/**
64-
* Retrieves a filter that accepts all events if generation-aware processing is not activated but
65-
* only changes that represent a generation increase otherwise.
66-
*
67-
* @param <T> the type of custom resource the filter should handle
68-
* @return a filter accepting changes based on generation information
69-
*/
70-
@SuppressWarnings("unchecked")
71-
public static <T extends HasMetadata> ResourceEventFilter<T> generationAware() {
72-
return (ResourceEventFilter<T>) GENERATION_AWARE;
73-
}
74-
75-
/**
76-
* Retrieves a filter that accepts changes if the target controller uses a finalizer and that
77-
* finalizer hasn't already been applied, rejecting them otherwise.
78-
*
79-
* @param <T> the type of custom resource the filter should handle
80-
* @return a filter accepting changes based on whether the finalizer is needed and has been
81-
* applied
82-
*/
83-
@SuppressWarnings("unchecked")
84-
public static <T extends HasMetadata> ResourceEventFilter<T> finalizerNeededAndApplied() {
85-
return (ResourceEventFilter<T>) USE_FINALIZER;
86-
}
87-
88-
/**
89-
* Retrieves a filter that accepts changes if the custom resource is marked for deletion.
90-
*
91-
* @param <T> the type of custom resource the filter should handle
92-
* @return a filter accepting changes based on whether the Custom Resource is marked for deletion.
93-
*/
94-
@SuppressWarnings("unchecked")
95-
public static <T extends HasMetadata> ResourceEventFilter<T> markedForDeletion() {
96-
return (ResourceEventFilter<T>) MARKED_FOR_DELETION;
97-
}
98-
99-
/**
100-
* Combines the provided, potentially {@code null} filters with an AND logic, i.e. the resulting
101-
* filter will only accept the change if all filters accept it, reject it otherwise.
102-
* <p>
103-
* Note that the evaluation of filters is lazy: the result is returned as soon as possible without
104-
* evaluating all filters if possible.
105-
*
106-
* @param items the filters to combine
107-
* @param <T> the type of custom resources the filters are supposed to handle
108-
* @return a combined filter implementing the AND logic combination of the provided filters
109-
*/
110-
@SafeVarargs
111-
public static <T extends HasMetadata> ResourceEventFilter<T> and(
112-
ResourceEventFilter<T>... items) {
113-
if (items == null) {
114-
return none();
115-
}
116-
117-
return (configuration, oldResource, newResource) -> {
118-
for (ResourceEventFilter<T> item : items) {
119-
if (item == null) {
120-
continue;
121-
}
122-
123-
if (!item.acceptChange(configuration, oldResource, newResource)) {
124-
return false;
125-
}
126-
}
127-
128-
return true;
129-
};
130-
}
131-
132-
/**
133-
* Combines the provided, potentially {@code null} filters with an OR logic, i.e. the resulting
134-
* filter will accept the change if any of the filters accepts it, rejecting it only if all reject
135-
* it.
136-
* <p>
137-
* Note that the evaluation of filters is lazy: the result is returned as soon as possible without
138-
* evaluating all filters if possible.
139-
*
140-
* @param items the filters to combine
141-
* @param <T> the type of custom resources the filters are supposed to handle
142-
* @return a combined filter implementing the OR logic combination of both provided filters
143-
*/
144-
@SafeVarargs
145-
public static <T extends HasMetadata> ResourceEventFilter<T> or(
146-
ResourceEventFilter<T>... items) {
147-
if (items == null) {
148-
return none();
149-
}
150-
151-
return (configuration, oldResource, newResource) -> {
152-
for (ResourceEventFilter<T> item : items) {
153-
if (item == null) {
154-
continue;
155-
}
156-
157-
if (item.acceptChange(configuration, oldResource, newResource)) {
158-
return true;
159-
}
160-
}
161-
162-
return false;
163-
};
164-
}
16527
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/ResourceEventFilterTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ public void eventFilteredByCustomPredicate() {
5656
cr.getMetadata().setGeneration(1L);
5757
cr.getStatus().setConfigMapStatus("1");
5858

59-
eventSource.eventReceived(ResourceAction.UPDATED, cr, null);
59+
TestCustomResource cr2 = TestUtils.testCustomResource();
60+
cr.getMetadata().setFinalizers(List.of(FINALIZER));
61+
cr.getMetadata().setGeneration(1L);
62+
cr.getStatus().setConfigMapStatus("2");
63+
64+
eventSource.eventReceived(ResourceAction.UPDATED, cr, cr2);
6065
verify(eventHandler, times(1)).handleEvent(any());
6166

6267
cr.getMetadata().setGeneration(1L);

0 commit comments

Comments
 (0)