refactor: plugin path in annotations when plugin installation (#4179)

#### What type of PR is this?
/kind improvement
/area core
/area plugin
/milestone 2.7.x

#### What this PR does / why we need it:
修复生产模式下插件安装时的位置信息为绝对路径会影响迁移的问题

how to test it?
1. 生产模式下安装插件看 annotation 中 `plugin.halo.run/plugin-path` 的值是否为相对于 pluginsRoot 的相对路径
2. 在生产模式下在 main 分支启动后安装的插件切换到此 PR 后 `plugin.halo.run/plugin-path` 是否变为相对路径

#### Which issue(s) this PR fixes:
Fixes #4178

#### Does this PR introduce a user-facing change?
```release-note
修复生产模式下插件安装时的位置信息为绝对路径会影响迁移的问题
```
pull/4208/head^2
guqing 2023-07-19 10:34:11 +08:00 committed by GitHub
parent e6f31759a0
commit 1dc2f6f4ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 13 deletions

View File

@ -118,15 +118,13 @@ public class PluginReconciler implements Reconciler<Request> {
client.fetch(Plugin.class, name).ifPresent(plugin -> {
Map<String, String> annotations = nullSafeAnnotations(plugin);
String oldPluginPath = annotations.get(PLUGIN_PATH);
String pluginPath = oldPluginPath;
if (StringUtils.isBlank(pluginPath)) {
URI loadLocation = plugin.statusNonNull().getLoadLocation();
pluginPath = Optional.ofNullable(loadLocation)
.map(URI::getPath)
.orElseGet(() -> PluginUtils.generateFileName(plugin));
}
annotations.put(PLUGIN_PATH, pluginPath);
if (!StringUtils.equals(pluginPath, oldPluginPath)) {
String pluginPath = StringUtils.isBlank(oldPluginPath)
? Optional.ofNullable(plugin.statusNonNull().getLoadLocation())
.map(URI::getPath)
.orElseGet(() -> PluginUtils.generateFileName(plugin)) : oldPluginPath;
String pluginPathAnno = resolvePluginPathForAnno(pluginPath);
annotations.put(PLUGIN_PATH, pluginPathAnno);
if (!StringUtils.equals(pluginPathAnno, oldPluginPath)) {
client.update(plugin);
}
});
@ -555,7 +553,7 @@ public class PluginReconciler implements Reconciler<Request> {
Map<String, String> newAnnotations = nullSafeAnnotations(persisted);
newAnnotations.putAll(nullSafeAnnotations(pluginInPath));
newAnnotations.put(PLUGIN_PATH, resolvePluginPathAnnotation(newPluginPath));
newAnnotations.put(PLUGIN_PATH, resolvePluginPathForAnno(newPluginPath));
newAnnotations.remove(RELOAD_ANNO);
nullSafeLabels(persisted).putAll(nullSafeLabels(pluginInPath));
persisted.statusNonNull().setLoadLocation(toUri(newPluginPath));
@ -567,7 +565,7 @@ public class PluginReconciler implements Reconciler<Request> {
});
}
String resolvePluginPathAnnotation(String pluginPathString) {
String resolvePluginPathForAnno(String pluginPathString) {
Path pluginsRoot = toPath(haloPluginManager.getPluginsRoot().toString());
Path pluginPath = toPath(pluginPathString);
if (pluginPath.startsWith(pluginsRoot)) {

View File

@ -79,6 +79,7 @@ class PluginReconcilerTest {
@BeforeEach
void setUp() {
pluginReconciler = new PluginReconciler(extensionClient, haloPluginManager, eventPublisher);
lenient().when(haloPluginManager.getPluginsRoot()).thenReturn(Paths.get("plugins"));
lenient().when(haloPluginManager.validatePluginVersion(any())).thenReturn(true);
lenient().when(haloPluginManager.getSystemVersion()).thenReturn("0.0.0");
lenient().when(haloPluginManager.getPlugin(any())).thenReturn(pluginWrapper);
@ -378,10 +379,10 @@ class PluginReconcilerTest {
@Test
void resolvePluginPathAnnotation() {
when(haloPluginManager.getPluginsRoot()).thenReturn(Paths.get("/tmp/plugins"));
String path = pluginReconciler.resolvePluginPathAnnotation("/tmp/plugins/sitemap-1.0.jar");
String path = pluginReconciler.resolvePluginPathForAnno("/tmp/plugins/sitemap-1.0.jar");
assertThat(path).isEqualTo("sitemap-1.0.jar");
path = pluginReconciler.resolvePluginPathAnnotation("/abc/plugins/sitemap-1.0.jar");
path = pluginReconciler.resolvePluginPathForAnno("/abc/plugins/sitemap-1.0.jar");
assertThat(path).isEqualTo("/abc/plugins/sitemap-1.0.jar");
}