Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,38 @@
*/
package io.serverlessworkflow.impl.container.executors;

import static io.serverlessworkflow.impl.WorkflowUtils.isValid;

import com.github.dockerjava.api.command.CreateContainerCmd;
import io.serverlessworkflow.api.types.Container;
import java.util.function.Function;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import java.util.Optional;

class CommandPropertySetter implements ContainerPropertySetter {

class CommandPropertySetter extends ContainerPropertySetter {
private Optional<WorkflowValueResolver<String>> command;

CommandPropertySetter(CreateContainerCmd createContainerCmd, Container configuration) {
super(createContainerCmd, configuration);
CommandPropertySetter(WorkflowDefinition definition, Container configuration) {
String commandName = configuration.getCommand();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During parsing time (when the constructor is invoked) we create the filter to resolve the command

command =
isValid(commandName)
? Optional.of(WorkflowUtils.buildStringFilter(definition.application(), commandName))
: Optional.empty();
}

@Override
public void accept(Function<String, String> resolver) {
if (configuration.getCommand() != null && !configuration.getCommand().isEmpty()) {
createContainerCmd.withCmd("sh", "-c", configuration.getCommand());
}
public void accept(
CreateContainerCmd containerCmd,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel model) {
command
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During runtime, we evaluate the filter and then set container command with that info

.map(c -> c.apply(workflowContext, taskContext, model))
.ifPresent(c -> containerCmd.withCmd("sh", "-c", c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,43 @@

import com.github.dockerjava.api.command.CreateContainerCmd;
import io.serverlessworkflow.api.types.Container;
import java.util.ArrayList;
import java.util.List;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import java.util.Map;
import java.util.function.Function;
import java.util.Optional;

class ContainerEnvironmentPropertySetter extends ContainerPropertySetter {
class ContainerEnvironmentPropertySetter implements ContainerPropertySetter {

ContainerEnvironmentPropertySetter(
CreateContainerCmd createContainerCmd, Container configuration) {
super(createContainerCmd, configuration);
private final Optional<WorkflowValueResolver<Map<String, Object>>> envResolver;

ContainerEnvironmentPropertySetter(WorkflowDefinition definition, Container configuration) {

this.envResolver =
configuration.getEnvironment() != null
&& configuration.getEnvironment().getAdditionalProperties() != null
? Optional.of(
WorkflowUtils.buildMapResolver(
definition.application(),
null,
configuration.getEnvironment().getAdditionalProperties()))
: Optional.empty();
}

@Override
public void accept(Function<String, String> resolver) {
if (!(configuration.getEnvironment() == null
|| configuration.getEnvironment().getAdditionalProperties() == null)) {
List<String> envs = new ArrayList<>();
for (Map.Entry<String, Object> entry :
configuration.getEnvironment().getAdditionalProperties().entrySet()) {
String key = entry.getKey();
if (entry.getValue() instanceof String value) {
String resolvedValue = resolver.apply(value);
envs.add(key + "=" + resolvedValue);
} else {
throw new IllegalArgumentException("Environment variable values must be strings");
}
}
createContainerCmd.withEnv(envs.toArray(new String[0]));
}
public void accept(
CreateContainerCmd command,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel model) {
envResolver
.map(env -> env.apply(workflowContext, taskContext, model))
.ifPresent(
envs ->
command.withEnv(
envs.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
package io.serverlessworkflow.impl.container.executors;

import com.github.dockerjava.api.command.CreateContainerCmd;
import io.serverlessworkflow.api.types.Container;
import java.util.function.Consumer;
import java.util.function.Function;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;

abstract class ContainerPropertySetter implements Consumer<Function<String, String>> {

protected final CreateContainerCmd createContainerCmd;
protected final Container configuration;

ContainerPropertySetter(CreateContainerCmd createContainerCmd, Container configuration) {
this.createContainerCmd = createContainerCmd;
this.configuration = configuration;
}
interface ContainerPropertySetter {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContainerPropertySetter is now a interface which implementations add things to the container command
Since implemenation are very likely to contain expressions, they will be evaluated with the help of the context

abstract void accept(
CreateContainerCmd command,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel model);
}
Loading