-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): refactor labels handling
Fixes kestra-io/kestra-ee#2001
- Loading branch information
1 parent
2b72a82
commit 791ced8
Showing
13 changed files
with
200 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/src/main/java/io/kestra/core/services/LabelService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.kestra.core.services; | ||
|
||
import io.kestra.core.exceptions.IllegalVariableEvaluationException; | ||
import io.kestra.core.models.Label; | ||
import io.kestra.core.models.flows.Flow; | ||
import io.kestra.core.models.triggers.AbstractTrigger; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.core.utils.ListUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class LabelService { | ||
private LabelService() {} | ||
|
||
/** | ||
* Return flow labels excluding system labels. | ||
*/ | ||
public static List<Label> labelsExcludingSystem(Flow flow) { | ||
return ListUtils.emptyOnNull(flow.getLabels()).stream().filter(label -> !label.key().startsWith(Label.SYSTEM_PREFIX)).toList(); | ||
} | ||
|
||
/** | ||
* Return flow labels excluding system labels concatenated with trigger labels. | ||
* | ||
* Trigger labels will be rendered via the run context but not flow labels. | ||
* In case rendering is not possible, the label will be omitted. | ||
*/ | ||
public static List<Label> fromTrigger(RunContext runContext, Flow flow, AbstractTrigger trigger) { | ||
final List<Label> labels = new ArrayList<>(); | ||
|
||
if (flow.getLabels() != null) { | ||
labels.addAll(LabelService.labelsExcludingSystem(flow)); // no need for rendering | ||
} | ||
|
||
if (trigger.getLabels() != null) { | ||
for (Label label : trigger.getLabels()) { | ||
final var value = renderLabelValue(runContext, label); | ||
if (value != null) { | ||
labels.add(new Label(label.key(), value)); | ||
} | ||
} | ||
} | ||
|
||
return labels; | ||
} | ||
|
||
private static String renderLabelValue(RunContext runContext, Label label) { | ||
try { | ||
return runContext.render(label.value()); | ||
} catch (IllegalVariableEvaluationException e) { | ||
runContext.logger().warn("Failed to render label '{}', it will be omitted", label.key(), e); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.