diff --git a/api/src/main/java/run/halo/app/extension/ExtensionMatcher.java b/api/src/main/java/run/halo/app/extension/ExtensionMatcher.java index 498a113ec9..ccc2582e4a 100644 --- a/api/src/main/java/run/halo/app/extension/ExtensionMatcher.java +++ b/api/src/main/java/run/halo/app/extension/ExtensionMatcher.java @@ -4,11 +4,20 @@ import run.halo.app.extension.router.selector.LabelSelector; public interface ExtensionMatcher { - GroupVersionKind getGvk(); + @Deprecated(since = "2.17.0", forRemoval = true) + default GroupVersionKind getGvk() { + return null; + } - LabelSelector getLabelSelector(); + @Deprecated(since = "2.17.0", forRemoval = true) + default LabelSelector getLabelSelector() { + return null; + } - FieldSelector getFieldSelector(); + @Deprecated(since = "2.17.0", forRemoval = true) + default FieldSelector getFieldSelector() { + return null; + } boolean match(Extension extension); } diff --git a/api/src/main/java/run/halo/app/extension/WatcherExtensionMatchers.java b/api/src/main/java/run/halo/app/extension/WatcherExtensionMatchers.java index 0a23f3f352..d8fce31050 100644 --- a/api/src/main/java/run/halo/app/extension/WatcherExtensionMatchers.java +++ b/api/src/main/java/run/halo/app/extension/WatcherExtensionMatchers.java @@ -4,6 +4,8 @@ import lombok.Builder; import lombok.Getter; import org.springframework.util.Assert; +import run.halo.app.extension.router.selector.FieldSelector; +import run.halo.app.extension.router.selector.LabelSelector; public class WatcherExtensionMatchers { @Getter @@ -38,15 +40,15 @@ public GroupVersionKind getGroupVersionKind() { } public ExtensionMatcher onAddMatcher() { - return this.onAddMatcher; + return delegateExtensionMatcher(this.onAddMatcher); } public ExtensionMatcher onUpdateMatcher() { - return this.onUpdateMatcher; + return delegateExtensionMatcher(this.onUpdateMatcher); } public ExtensionMatcher onDeleteMatcher() { - return this.onDeleteMatcher; + return delegateExtensionMatcher(this.onDeleteMatcher); } public static WatcherExtensionMatchersBuilder builder(ExtensionClient client, @@ -58,4 +60,32 @@ static ExtensionMatcher emptyMatcher(ExtensionClient client, GroupVersionKind gvk) { return DefaultExtensionMatcher.builder(client, gvk).build(); } + + /** + * Remove this method when the deprecated methods are removed. + */ + ExtensionMatcher delegateExtensionMatcher(ExtensionMatcher matcher) { + return new ExtensionMatcher() { + + @Override + public GroupVersionKind getGvk() { + return matcher.getGvk(); + } + + @Override + public LabelSelector getLabelSelector() { + return matcher.getLabelSelector(); + } + + @Override + public FieldSelector getFieldSelector() { + return matcher.getFieldSelector(); + } + + @Override + public boolean match(Extension extension) { + return extension.groupVersionKind().equals(gvk) && matcher.match(extension); + } + }; + } } diff --git a/api/src/main/java/run/halo/app/extension/controller/ControllerBuilder.java b/api/src/main/java/run/halo/app/extension/controller/ControllerBuilder.java index 2bfcd9c4c4..6c36e25bc1 100644 --- a/api/src/main/java/run/halo/app/extension/controller/ControllerBuilder.java +++ b/api/src/main/java/run/halo/app/extension/controller/ControllerBuilder.java @@ -7,6 +7,7 @@ import run.halo.app.extension.Extension; import run.halo.app.extension.ExtensionClient; import run.halo.app.extension.ExtensionMatcher; +import run.halo.app.extension.ListOptions; import run.halo.app.extension.WatcherExtensionMatchers; import run.halo.app.extension.controller.Reconciler.Request; @@ -30,6 +31,8 @@ public class ControllerBuilder { private ExtensionMatcher onUpdateMatcher; + private ListOptions syncAllListOptions; + private final ExtensionClient client; private boolean syncAllOnStart = true; @@ -84,6 +87,11 @@ public ControllerBuilder syncAllOnStart(boolean syncAllAtStart) { return this; } + public ControllerBuilder syncAllListOptions(ListOptions syncAllListOptions) { + this.syncAllListOptions = syncAllListOptions; + return this; + } + public ControllerBuilder workerCount(int workerCount) { this.workerCount = workerCount; return this; @@ -116,8 +124,23 @@ public Controller build() { client, extension, watcher, - extensionMatchers.onAddMatcher()); + determineSyncAllListOptions()); return new DefaultController<>(name, reconciler, queue, synchronizer, minDelay, maxDelay, workerCount); } + + ListOptions determineSyncAllListOptions() { + if (syncAllListOptions != null) { + return syncAllListOptions; + } + // In order to be compatible with the previous version of the code + // The previous version of the code determined syncAllListOptions through onAddMatcher + // TODO Will be removed later + if (onAddMatcher != null) { + return new ListOptions() + .setLabelSelector(onAddMatcher.getLabelSelector()) + .setFieldSelector(onAddMatcher.getFieldSelector()); + } + return new ListOptions(); + } } diff --git a/api/src/main/java/run/halo/app/extension/controller/RequestSynchronizer.java b/api/src/main/java/run/halo/app/extension/controller/RequestSynchronizer.java index bc5e14ab29..bdb7ddf568 100644 --- a/api/src/main/java/run/halo/app/extension/controller/RequestSynchronizer.java +++ b/api/src/main/java/run/halo/app/extension/controller/RequestSynchronizer.java @@ -5,7 +5,6 @@ import org.springframework.data.domain.Sort; import run.halo.app.extension.Extension; import run.halo.app.extension.ExtensionClient; -import run.halo.app.extension.ExtensionMatcher; import run.halo.app.extension.GroupVersionKind; import run.halo.app.extension.ListOptions; import run.halo.app.extension.Watcher; @@ -27,7 +26,7 @@ public class RequestSynchronizer implements Synchronizer { private final Watcher watcher; - private final ExtensionMatcher listMatcher; + private final ListOptions listOptions; @Getter private volatile boolean started = false; @@ -36,13 +35,13 @@ public RequestSynchronizer(boolean syncAllOnStart, ExtensionClient client, Extension extension, Watcher watcher, - ExtensionMatcher listMatcher) { + ListOptions listOptions) { this.syncAllOnStart = syncAllOnStart; this.client = client; this.type = extension.groupVersionKind(); this.watcher = watcher; this.indexedQueryEngine = client.indexedQueryEngine(); - this.listMatcher = listMatcher; + this.listOptions = listOptions; } @Override @@ -54,11 +53,6 @@ public void start() { started = true; if (syncAllOnStart) { - var listOptions = new ListOptions(); - if (listMatcher != null) { - listOptions.setFieldSelector(listMatcher.getFieldSelector()); - listOptions.setLabelSelector(listMatcher.getLabelSelector()); - } indexedQueryEngine.retrieveAll(type, listOptions, Sort.by("metadata.creationTimestamp")) .forEach(name -> watcher.onAdd(new Request(name))); } diff --git a/api/src/test/java/run/halo/app/extension/controller/RequestSynchronizerTest.java b/api/src/test/java/run/halo/app/extension/controller/RequestSynchronizerTest.java index b8368c65f5..787f9fe77e 100644 --- a/api/src/test/java/run/halo/app/extension/controller/RequestSynchronizerTest.java +++ b/api/src/test/java/run/halo/app/extension/controller/RequestSynchronizerTest.java @@ -18,7 +18,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Sort; import run.halo.app.extension.ExtensionClient; -import run.halo.app.extension.ExtensionMatcher; import run.halo.app.extension.FakeExtension; import run.halo.app.extension.GroupVersionKind; import run.halo.app.extension.ListOptions; @@ -37,16 +36,13 @@ class RequestSynchronizerTest { @Mock Watcher watcher; - @Mock - ExtensionMatcher listMatcher; - RequestSynchronizer synchronizer; @BeforeEach void setUp() { when(client.indexedQueryEngine()).thenReturn(indexedQueryEngine); synchronizer = - new RequestSynchronizer(true, client, new FakeExtension(), watcher, listMatcher); + new RequestSynchronizer(true, client, new FakeExtension(), watcher, new ListOptions()); assertFalse(synchronizer.isDisposed()); assertFalse(synchronizer.isStarted()); } @@ -71,7 +67,7 @@ void shouldStartCorrectlyWhenSyncingAllOnStart() { @Test void shouldStartCorrectlyWhenNotSyncingAllOnStart() { synchronizer = - new RequestSynchronizer(false, client, new FakeExtension(), watcher, listMatcher); + new RequestSynchronizer(false, client, new FakeExtension(), watcher, new ListOptions()); assertFalse(synchronizer.isDisposed()); assertFalse(synchronizer.isStarted());