Skip to content

Commit

Permalink
chore(core): revert labels as list in a Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jul 13, 2023
1 parent 007142c commit ae74c1b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 25 deletions.
9 changes: 1 addition & 8 deletions core/src/main/java/io/kestra/core/models/flows/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import io.kestra.core.exceptions.InternalException;
import io.kestra.core.models.DeletedInterface;
import io.kestra.core.models.Label;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.listeners.Listener;
import io.kestra.core.models.tasks.FlowableTask;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.models.triggers.AbstractTrigger;
import io.kestra.core.models.validations.ManualConstraintViolation;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.core.serializers.ListOrMapOfLabelDeserializer;
import io.kestra.core.serializers.ListOrMapOfLabelSerializer;
import io.kestra.core.services.FlowService;
import io.kestra.core.validations.FlowValidation;
import io.micronaut.core.annotation.Introspected;
Expand Down Expand Up @@ -68,9 +63,7 @@ public boolean hasIgnoreMarker(final AnnotatedMember m) {

String description;

@JsonSerialize(using = ListOrMapOfLabelSerializer.class)
@JsonDeserialize(using = ListOrMapOfLabelDeserializer.class)
List<Label> labels;
Map<String, String> labels;

@Valid
List<Input<?>> inputs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.kestra.core.models.triggers.types;

import io.kestra.core.models.Label;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.utils.LabelUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;
Expand Down Expand Up @@ -85,7 +87,7 @@ public Optional<Execution> evaluate(RunContext runContext, io.kestra.core.models
.namespace(flow.getNamespace())
.flowId(flow.getId())
.flowRevision(flow.getRevision())
.labels(flow.getLabels())
.labels(LabelUtils.from(flow.getLabels()))
.state(new State())
.trigger(ExecutionTrigger.of(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import com.google.common.collect.ImmutableMap;
import io.kestra.core.models.Label;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
Expand All @@ -20,6 +21,7 @@
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunnerUtils;
import io.kestra.core.services.ConditionService;
import io.kestra.core.utils.LabelUtils;
import io.kestra.core.validations.CronExpression;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
Expand Down Expand Up @@ -273,7 +275,7 @@ public Optional<Execution> evaluate(ConditionContext conditionContext, TriggerCo
.namespace(context.getNamespace())
.flowId(context.getFlowId())
.flowRevision(context.getFlowRevision())
.labels(conditionContext.getFlow().getLabels())
.labels(LabelUtils.from(conditionContext.getFlow().getLabels()))
.state(new State())
.trigger(executionTrigger)
// keep to avoid breaking compatibility
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/kestra/core/runners/RunnerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.Await;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.LabelUtils;
import io.micronaut.http.multipart.StreamingFileUpload;
import io.reactivex.Flowable;
import io.reactivex.Single;
Expand Down Expand Up @@ -397,7 +398,7 @@ public Execution newExecution(Flow flow, BiFunction<Flow, Execution, Map<String,

List<Label> executionLabels = new ArrayList<>();
if (flow.getLabels() != null) {
executionLabels.addAll(flow.getLabels());
executionLabels.addAll(LabelUtils.from(flow.getLabels()));
}
if (labels != null) {
executionLabels.addAll(labels);
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/kestra/core/runners/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.kestra.core.tasks.flows.WorkingDirectory;
import io.kestra.core.utils.Await;
import io.kestra.core.utils.ExecutorsUtils;
import io.kestra.core.utils.LabelUtils;
import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.inject.qualifiers.Qualifiers;
Expand Down Expand Up @@ -202,7 +203,7 @@ private void handleTrigger(WorkerTrigger workerTrigger) {
if (flowLabels != null) {
evaluate = evaluate.map( execution -> {
List<Label> executionLabels = execution.getLabels() != null ? execution.getLabels() : new ArrayList<>();
executionLabels.addAll(flowLabels);
executionLabels.addAll(LabelUtils.from(flowLabels));
return execution.withLabels(executionLabels);
}
);
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/io/kestra/core/utils/LabelUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.kestra.core.utils;

import io.kestra.core.models.Label;

import java.util.List;
import java.util.Map;

public final class LabelUtils {
private LabelUtils() {
//utility class pattern
}

public static List<Label> from(Map<String, String> labels) {
return labels == null ? null : labels.entrySet().stream()
.map(entry -> new Label(entry.getKey(), entry.getValue()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -30,9 +29,9 @@ void success() {
.namespace("io.kestra.unittest")
.revision(1)
.labels(
List.of(
new Label("flow-label-1", "flow-label-1"),
new Label("flow-label-2", "flow-label-2"))
Map.of(
"flow-label-1", "flow-label-1",
"flow-label-2", "flow-label-2")
)
.tasks(Collections.singletonList(Return.builder()
.id("test")
Expand Down Expand Up @@ -61,6 +60,6 @@ void success() {
assertThat(evaluate.isPresent(), is(true));
assertThat(evaluate.get().getFlowId(), is("flow-with-flow-trigger"));
assertThat(evaluate.get().getLabels().get(0), is(new Label("flow-label-1", "flow-label-1")));
assertThat(evaluate.get().getLabels().get(1), is(new Label("flow-label-2", "flow-label-2")));;
assertThat(evaluate.get().getLabels().get(1), is(new Label("flow-label-2", "flow-label-2")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ private ConditionContext conditionContext(AbstractTrigger trigger) {
.id(IdUtils.create())
.namespace("io.kestra.tests")
.labels(
List.of(
new Label("flow-label-1", "flow-label-1"),
new Label("flow-label-2", "flow-label-2"))
Map.of(
"flow-label-1", "flow-label-1",
"flow-label-2", "flow-label-2")
)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.kestra.core.schedulers;

import com.google.common.collect.ImmutableMap;
import io.kestra.core.models.Label;
import io.kestra.core.models.conditions.ConditionContext;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.executions.ExecutionTrigger;
Expand Down Expand Up @@ -63,9 +62,9 @@ protected static Flow createFlow(List<AbstractTrigger> triggers, List<TaskDefaul
))
.revision(1)
.labels(
List.of(
new Label("flow-label-1", "flow-label-1"),
new Label("flow-label-2", "flow-label-2"))
Map.of(
"flow-label-1", "flow-label-1",
"flow-label-2", "flow-label-2")
)
.triggers(triggers)
.tasks(Collections.singletonList(Return.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.kestra.core.runners.FlowableUtils;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.utils.LabelUtils;
import io.kestra.webserver.responses.BulkErrorResponse;
import io.kestra.webserver.responses.BulkResponse;
import io.kestra.webserver.utils.RequestUtils;
Expand Down Expand Up @@ -431,7 +432,7 @@ private Execution webhook(

var result = execution.get();
if (flow.getLabels() != null) {
result = result.withLabels(flow.getLabels());
result = result.withLabels(LabelUtils.from(flow.getLabels()));
}
executionQueue.emit(result);
eventPublisher.publishEvent(new CrudEvent<>(result, CrudEventType.CREATE));
Expand Down

0 comments on commit ae74c1b

Please sign in to comment.