Skip to content

fix: dependent resource cache filler and filter issue #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
private final boolean creatable = this instanceof Creator;
private final boolean updatable = this instanceof Updater;
private final boolean deletable = this instanceof Deleter;
private final boolean filteringEventSource;
private final boolean cachingEventSource;
protected Creator<R, P> creator;
protected Updater<R, P> updater;
protected Deleter<P> deleter;

@SuppressWarnings("unchecked")
public AbstractDependentResource() {
if (this instanceof EventSourceProvider) {
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
filteringEventSource = eventSource instanceof RecentOperationEventFilter;
cachingEventSource = eventSource instanceof RecentOperationCacheFiller;
} else {
filteringEventSource = false;
cachingEventSource = false;
}
creator = creatable ? (Creator<R, P>) this : null;
updater = updatable ? (Updater<R, P>) this : null;
deleter = deletable ? (Deleter<P>) this : null;
Expand Down Expand Up @@ -81,27 +71,27 @@ protected void handleCreate(R desired, P primary, Context context) {
}

private void cleanupAfterEventFiltering(R desired, ResourceID resourceID, R created) {
if (filteringEventSource) {
if (isFilteringEventSource()) {
eventSourceAsRecentOperationEventFilter()
.cleanupOnCreateOrUpdateEventFiltering(resourceID, created);
}
}

private void cacheAfterCreate(ResourceID resourceID, R created) {
if (cachingEventSource) {
if (isRecentOperationCacheFiller()) {
eventSourceAsRecentOperationCacheFiller().handleRecentResourceCreate(resourceID, created);
}
}

private void cacheAfterUpdate(R actual, ResourceID resourceID, R updated) {
if (cachingEventSource) {
if (isRecentOperationCacheFiller()) {
eventSourceAsRecentOperationCacheFiller().handleRecentResourceUpdate(resourceID, updated,
actual);
}
}

private void prepareEventFiltering(R desired, ResourceID resourceID) {
if (filteringEventSource) {
if (isFilteringEventSource()) {
eventSourceAsRecentOperationEventFilter().prepareForCreateOrUpdateEventFiltering(resourceID,
desired);
}
Expand Down Expand Up @@ -130,6 +120,26 @@ private RecentOperationCacheFiller<R> eventSourceAsRecentOperationCacheFiller()
return (RecentOperationCacheFiller<R>) ((EventSourceProvider<P>) this).getEventSource();
}

// this cannot be done in constructor since event source might be initialized later
protected boolean isFilteringEventSource() {
if (this instanceof EventSourceProvider) {
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
return eventSource instanceof RecentOperationEventFilter;
} else {
return false;
}
}

// this cannot be done in constructor since event source might be initialized later
protected boolean isRecentOperationCacheFiller() {
if (this instanceof EventSourceProvider) {
final var eventSource = ((EventSourceProvider<P>) this).getEventSource();
return eventSource instanceof RecentOperationCacheFiller;
} else {
return false;
}
}

@Override
public void cleanup(P primary, Context context) {
if (isDeletable(primary, context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import io.javaoperatorsdk.operator.junit.OperatorExtension;
import io.javaoperatorsdk.operator.sample.standalonedependent.StandaloneDependentTestCustomResource;
Expand Down Expand Up @@ -53,8 +54,9 @@ void executeUpdateForTestingCacheUpdateForGetResource() {

awaitForDeploymentReadyReplicas(1);

createdCR.getSpec().setReplicaCount(2);
operator.replace(StandaloneDependentTestCustomResource.class, createdCR);
var clonedCr = ConfigurationService.DEFAULT_CLONER.clone(createdCR);
clonedCr.getSpec().setReplicaCount(2);
operator.replace(StandaloneDependentTestCustomResource.class, clonedCr);

awaitForDeploymentReadyReplicas(2);
assertThat(
Expand Down