Skip to content

Commit

Permalink
feat(webserver): Purge system flow created from blueprint at startup
Browse files Browse the repository at this point in the history
closes #4878
  • Loading branch information
brian-mulier-p committed Sep 12, 2024
1 parent f6c9ed8 commit 98ee431
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/io/kestra/core/utils/NamespaceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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'.
Expand Down Expand Up @@ -78,13 +84,19 @@ public void load() {
))
.map(response -> ((PagedResults<BlueprintItem>)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));
Expand Down

0 comments on commit 98ee431

Please sign in to comment.