Skip to content

Commit dc0b621

Browse files
authored
fix: dependent resource cache iller and filter issue (#1018)
1 parent cfccc71 commit dc0b621

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,12 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
1414
private final boolean creatable = this instanceof Creator;
1515
private final boolean updatable = this instanceof Updater;
1616
private final boolean deletable = this instanceof Deleter;
17-
private final boolean filteringEventSource;
18-
private final boolean cachingEventSource;
1917
protected Creator<R, P> creator;
2018
protected Updater<R, P> updater;
2119
protected Deleter<P> deleter;
2220

2321
@SuppressWarnings("unchecked")
2422
public AbstractDependentResource() {
25-
if (this instanceof EventSourceProvider) {
26-
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
27-
filteringEventSource = eventSource instanceof RecentOperationEventFilter;
28-
cachingEventSource = eventSource instanceof RecentOperationCacheFiller;
29-
} else {
30-
filteringEventSource = false;
31-
cachingEventSource = false;
32-
}
3323
creator = creatable ? (Creator<R, P>) this : null;
3424
updater = updatable ? (Updater<R, P>) this : null;
3525
deleter = deletable ? (Deleter<P>) this : null;
@@ -81,27 +71,27 @@ protected void handleCreate(R desired, P primary, Context<P> context) {
8171
}
8272

8373
private void cleanupAfterEventFiltering(R desired, ResourceID resourceID, R created) {
84-
if (filteringEventSource) {
74+
if (isFilteringEventSource()) {
8575
eventSourceAsRecentOperationEventFilter()
8676
.cleanupOnCreateOrUpdateEventFiltering(resourceID, created);
8777
}
8878
}
8979

9080
private void cacheAfterCreate(ResourceID resourceID, R created) {
91-
if (cachingEventSource) {
81+
if (isRecentOperationCacheFiller()) {
9282
eventSourceAsRecentOperationCacheFiller().handleRecentResourceCreate(resourceID, created);
9383
}
9484
}
9585

9686
private void cacheAfterUpdate(R actual, ResourceID resourceID, R updated) {
97-
if (cachingEventSource) {
87+
if (isRecentOperationCacheFiller()) {
9888
eventSourceAsRecentOperationCacheFiller().handleRecentResourceUpdate(resourceID, updated,
9989
actual);
10090
}
10191
}
10292

10393
private void prepareEventFiltering(R desired, ResourceID resourceID) {
104-
if (filteringEventSource) {
94+
if (isFilteringEventSource()) {
10595
eventSourceAsRecentOperationEventFilter().prepareForCreateOrUpdateEventFiltering(resourceID,
10696
desired);
10797
}
@@ -130,6 +120,26 @@ private RecentOperationCacheFiller<R> eventSourceAsRecentOperationCacheFiller()
130120
return (RecentOperationCacheFiller<R>) ((EventSourceProvider<P>) this).getEventSource();
131121
}
132122

123+
// this cannot be done in constructor since event source might be initialized later
124+
protected boolean isFilteringEventSource() {
125+
if (this instanceof EventSourceProvider) {
126+
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
127+
return eventSource instanceof RecentOperationEventFilter;
128+
} else {
129+
return false;
130+
}
131+
}
132+
133+
// this cannot be done in constructor since event source might be initialized later
134+
protected boolean isRecentOperationCacheFiller() {
135+
if (this instanceof EventSourceProvider) {
136+
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
137+
return eventSource instanceof RecentOperationCacheFiller;
138+
} else {
139+
return false;
140+
}
141+
}
142+
133143
@Override
134144
public void cleanup(P primary, Context<P> context) {
135145
if (isDeletable(primary, context)) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import io.fabric8.kubernetes.api.model.ObjectMeta;
99
import io.fabric8.kubernetes.api.model.apps.Deployment;
10+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1011
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
1112
import io.javaoperatorsdk.operator.junit.OperatorExtension;
1213
import io.javaoperatorsdk.operator.sample.standalonedependent.StandaloneDependentTestCustomResource;
@@ -53,8 +54,9 @@ void executeUpdateForTestingCacheUpdateForGetResource() {
5354

5455
awaitForDeploymentReadyReplicas(1);
5556

56-
createdCR.getSpec().setReplicaCount(2);
57-
operator.replace(StandaloneDependentTestCustomResource.class, createdCR);
57+
var clonedCr = ConfigurationService.DEFAULT_CLONER.clone(createdCR);
58+
clonedCr.getSpec().setReplicaCount(2);
59+
operator.replace(StandaloneDependentTestCustomResource.class, clonedCr);
5860

5961
awaitForDeploymentReadyReplicas(2);
6062
assertThat(

0 commit comments

Comments
 (0)