Skip to content
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

fix: route for the tag is not registered when halo starts #3322

Merged
merged 2 commits into from
Feb 15, 2023
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
@@ -1,6 +1,5 @@
package run.halo.app.core.extension.reconciler;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -34,20 +33,17 @@ public TagReconciler(ExtensionClient client, TagPermalinkPolicy tagPermalinkPoli

@Override
public Result reconcile(Request request) {
return client.fetch(Tag.class, request.name())
.map(tag -> {
client.fetch(Tag.class, request.name())
.ifPresent(tag -> {
if (isDeleted(tag)) {
cleanUpResourcesAndRemoveFinalizer(request.name());
return new Result(false, null);
return;
}
addFinalizerIfNecessary(tag);

this.reconcileStatusPermalink(request.name());

reconcileStatusPosts(request.name());
return new Result(true, Duration.ofMinutes(1));
})
.orElseGet(() -> new Result(false, null));
});
return new Result(false, null);
}

@Override
Expand Down Expand Up @@ -90,22 +86,6 @@ private void cleanUpResourcesAndRemoveFinalizer(String tagName) {
});
}

private void reconcileStatusPermalink(String tagName) {
client.fetch(Tag.class, tagName)
.ifPresent(tag -> {
Tag oldTag = JsonUtils.deepCopy(tag);
tagPermalinkPolicy.onPermalinkDelete(oldTag);

tag.getStatusOrDefault()
.setPermalink(tagPermalinkPolicy.permalink(tag));
tagPermalinkPolicy.onPermalinkAdd(tag);

if (!oldTag.equals(tag)) {
client.update(tag);
}
});
}

private void reconcileStatusPosts(String tagName) {
client.fetch(Tag.class, tagName).ifPresent(tag -> {
Tag oldTag = JsonUtils.deepCopy(tag);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package run.halo.app.core.extension.reconciler;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import run.halo.app.content.permalinks.TagPermalinkPolicy;
import run.halo.app.core.extension.content.Tag;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
import run.halo.app.extension.controller.Reconciler;

@Component
@RequiredArgsConstructor
public class TagRouteReconciler implements Reconciler<Reconciler.Request> {
private final ExtensionClient client;
private final TagPermalinkPolicy tagPermalinkPolicy;

@Override
public Result reconcile(Request request) {
client.fetch(Tag.class, request.name())
.ifPresent(tag -> {
if (tag.getMetadata().getDeletionTimestamp() != null) {
// TagReconciler already did it, so there is no need to remove permalink
Copy link
Member Author

@guqing guqing Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注: 这里不再删除路由,这个逻辑在 TagReconciler 已经做了就不在修改,因为此 Reconciler 没有添加 Finalizer 如果在这里做可能不可靠

return;
}

reconcilePermalinkRoute(request.name());
});
return new Result(false, null);
}

private void reconcilePermalinkRoute(String tagName) {
client.fetch(Tag.class, tagName)
.ifPresent(tag -> {
final String oldPermalink = tag.getStatusOrDefault().getPermalink();

tagPermalinkPolicy.onPermalinkDelete(tag);

String permalink = tagPermalinkPolicy.permalink(tag);
tag.getStatusOrDefault().setPermalink(permalink);
tagPermalinkPolicy.onPermalinkAdd(tag);

if (!StringUtils.equals(permalink, oldPermalink)) {
client.update(tag);
}
});
}

@Override
public Controller setupWith(ControllerBuilder builder) {
return builder
.extension(new Tag())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,6 @@ class TagReconcilerTest {
@InjectMocks
private TagReconciler tagReconciler;

@Test
void reconcile() {
Tag tag = tag();
when(client.fetch(eq(Tag.class), eq("fake-tag")))
.thenReturn(Optional.of(tag));
when(tagPermalinkPolicy.permalink(any()))
.thenAnswer(arg -> "/tags/" + tag.getSpec().getSlug());
ArgumentCaptor<Tag> captor = ArgumentCaptor.forClass(Tag.class);

tagReconciler.reconcile(new TagReconciler.Request("fake-tag"));

verify(client, times(3)).update(captor.capture());
verify(tagPermalinkPolicy, times(1)).onPermalinkAdd(any());
verify(tagPermalinkPolicy, times(1)).onPermalinkDelete(any());
Tag capture = captor.getValue();
assertThat(capture.getStatus().getPermalink()).isEqualTo("/tags/fake-slug");

// change slug
tag.getSpec().setSlug("new-slug");
tagReconciler.reconcile(new TagReconciler.Request("fake-tag"));
verify(client, times(4)).update(captor.capture());
verify(tagPermalinkPolicy, times(2)).onPermalinkAdd(any());
verify(tagPermalinkPolicy, times(2)).onPermalinkDelete(any());
assertThat(capture.getStatus().getPermalink()).isEqualTo("/tags/new-slug");
}

@Test
void reconcileDelete() {
Tag tag = tag();
Expand Down