From 98ee431d1fb3303abadfe090943a819d7cb31628 Mon Sep 17 00:00:00 2001 From: "brian.mulier" Date: Tue, 10 Sep 2024 15:46:54 +0200 Subject: [PATCH] feat(webserver): Purge system flow created from blueprint at startup closes #4878 --- .../io/kestra/core/utils/NamespaceUtils.java | 3 +++ .../services/FlowAutoLoaderService.java | 20 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/kestra/core/utils/NamespaceUtils.java b/core/src/main/java/io/kestra/core/utils/NamespaceUtils.java index 062afd6d15..26cd2dc1de 100644 --- a/core/src/main/java/io/kestra/core/utils/NamespaceUtils.java +++ b/core/src/main/java/io/kestra/core/utils/NamespaceUtils.java @@ -5,9 +5,12 @@ import lombok.Getter; import java.util.*; +import java.util.regex.Pattern; @Singleton public class NamespaceUtils { + public static final Pattern NAMESPACE_FROM_FLOW_SOURCE_PATTERN = Pattern.compile("^namespace: \\S*", Pattern.MULTILINE); + public static final String SYSTEM_FLOWS_DEFAULT_NAMESPACE = "system"; @Getter diff --git a/webserver/src/main/java/io/kestra/webserver/services/FlowAutoLoaderService.java b/webserver/src/main/java/io/kestra/webserver/services/FlowAutoLoaderService.java index 5d0f40b074..b940687f43 100644 --- a/webserver/src/main/java/io/kestra/webserver/services/FlowAutoLoaderService.java +++ b/webserver/src/main/java/io/kestra/webserver/services/FlowAutoLoaderService.java @@ -4,6 +4,7 @@ import io.kestra.core.repositories.FlowRepositoryInterface; import io.kestra.core.serializers.YamlFlowParser; import io.kestra.core.services.PluginDefaultService; +import io.kestra.core.utils.NamespaceUtils; import io.kestra.webserver.annotation.WebServerEnabled; import io.kestra.webserver.controllers.api.BlueprintController.BlueprintItem; import io.kestra.webserver.controllers.api.BlueprintController.BlueprintTagItem; @@ -22,6 +23,7 @@ import reactor.core.publisher.Mono; import java.util.Collection; +import java.util.Objects; import java.util.Optional; import java.util.function.Function; @@ -32,9 +34,10 @@ @WebServerEnabled @Requires(property = "kestra.tutorial-flows.enabled", value = "true", defaultValue = "true") public class FlowAutoLoaderService { - private static final Logger log = LoggerFactory.getLogger(FlowAutoLoaderService.class); + public static final String PURGE_SYSTEM_FLOW_BLUEPRINT_ID = "234"; + @Inject protected FlowRepositoryInterface repository; @@ -48,6 +51,9 @@ public class FlowAutoLoaderService { @Inject private YamlFlowParser yamlFlowParser; + @Inject + private NamespaceUtils namespaceUtils; + @SuppressWarnings("unchecked") public void load() { // Gets the tag ID for 'Getting Started'. @@ -78,13 +84,19 @@ public void load() { )) .map(response -> ((PagedResults)response.body()).getResults()) .flatMapIterable(Function.identity()) - .flatMap(it -> httpClient + .mergeWith(Mono.just(BlueprintItem.builder().id(PURGE_SYSTEM_FLOW_BLUEPRINT_ID).build())) + .flatMap(it -> Mono.from(httpClient .exchange( HttpRequest.create(HttpMethod.GET, "/v1/blueprints/" + it.getId() + "/flow"), Argument.STRING - ) + )).mapNotNull(response -> { + String body = response.body(); + if (it.getId().equals(PURGE_SYSTEM_FLOW_BLUEPRINT_ID)) { + return NamespaceUtils.NAMESPACE_FROM_FLOW_SOURCE_PATTERN.matcher(Objects.requireNonNull(body)).replaceFirst("namespace: " + namespaceUtils.getSystemFlowNamespace()); + } + return body; + }) ) - .map(HttpResponse::body) .map(source -> { Flow flow = yamlFlowParser.parse(source, Flow.class); repository.create(flow, source, pluginDefaultService.injectDefaults(flow));