Skip to content

Commit

Permalink
feat(core): Re-introduce executionEngine flag, add ExecutionEngineRun…
Browse files Browse the repository at this point in the history
…ner that selects an underlying ExecutionRunner based on the specified ExecutionEngine
  • Loading branch information
jonsie committed Oct 30, 2020
1 parent 29bb862 commit 7c67278
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

package com.netflix.spinnaker.orca.pipeline;
package com.netflix.spinnaker.orca.api.pipeline;

import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** The touch point into running a {@link PipelineExecution}. */
public interface ExecutionRunner {
void start(@Nonnull PipelineExecution execution);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.netflix.spinnaker.orca.api.pipeline.models;

import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** Signals that the annotated element supports a specific {@link ExecutionEngine} version. */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
public @interface ExecutionEngineVersion {
ExecutionEngine value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public interface PipelineExecution {

void setStatus(ExecutionStatus status);

ExecutionEngine getExecutionEngine();

void setExecutionEngine(ExecutionEngine executionEngine);

AuthenticationDetails getAuthentication();

void setAuthentication(AuthenticationDetails authentication);
Expand Down Expand Up @@ -252,4 +256,21 @@ public void setVersion(@Nonnull String version) {
this.version = version;
}
}

ExecutionEngine DEFAULT_EXECUTION_ENGINE = ExecutionEngine.v3;

enum ExecutionEngine {
/**
* v2 is obsolete, but it is here as a failsafe in case it is present in a pipeline
* configuration and needs to be deserialized. If v2 is specified, the default execution engine
* (v3) will be used instead.
*/
v2,

/** v3 execution engine is the keiko execution engine. */
v3,

/** v4 execution engine does not yet exist, early prototyping is underway. */
v4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.netflix.spinnaker.orca.DynamicStageResolver;
import com.netflix.spinnaker.orca.StageResolver;
import com.netflix.spinnaker.orca.TaskResolver;
import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner;
import com.netflix.spinnaker.orca.api.pipeline.Task;
import com.netflix.spinnaker.orca.api.pipeline.graph.StageDefinitionBuilder;
import com.netflix.spinnaker.orca.commands.ForceExecutionCancellationCommand;
Expand All @@ -40,7 +41,7 @@
import com.netflix.spinnaker.orca.listeners.*;
import com.netflix.spinnaker.orca.pipeline.CompoundExecutionOperator;
import com.netflix.spinnaker.orca.pipeline.DefaultStageDefinitionBuilderFactory;
import com.netflix.spinnaker.orca.pipeline.ExecutionRunner;
import com.netflix.spinnaker.orca.pipeline.ExecutionEngineRunner;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilderFactory;
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository;
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor;
Expand Down Expand Up @@ -236,9 +237,14 @@ public ForceExecutionCancellationCommand forceExecutionCancellationCommand(
return new ForceExecutionCancellationCommand(executionRepository, clock);
}

@Bean
public ExecutionEngineRunner executionEngineRunner(List<ExecutionRunner> executionRunners) {
return new ExecutionEngineRunner(executionRunners);
}

@Bean
public CompoundExecutionOperator compoundExecutionOperator(
ExecutionRepository repository, ExecutionRunner runner, RetrySupport retrySupport) {
ExecutionRepository repository, ExecutionEngineRunner runner, RetrySupport retrySupport) {
return new CompoundExecutionOperator(repository, runner, retrySupport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.orca.pipeline;

import com.netflix.spinnaker.kork.core.RetrySupport;
import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.netflix.spinnaker.orca.pipeline;

import static com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.DEFAULT_EXECUTION_ENGINE;

import com.netflix.spinnaker.kork.annotations.VisibleForTesting;
import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionEngineVersion;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine;
import java.util.List;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Nullable;

/**
* Executes the {@link PipelineExecution} based on the specified {@link ExecutionEngine}. The {@link
* ExecutionEngine} is expected to be specified via the {@link ExecutionEngineVersion} annotation.
*/
public class ExecutionEngineRunner implements ExecutionRunner {

private final List<ExecutionRunner> executionRunners;

public ExecutionEngineRunner(List<ExecutionRunner> executionRunners) {
this.executionRunners = executionRunners;
}

@Override
public void start(@Nonnull PipelineExecution execution) {
executionRunner(execution.getExecutionEngine()).start(execution);
}

@Override
public void restart(@Nonnull PipelineExecution execution, @Nonnull String stageId) {
executionRunner(execution.getExecutionEngine()).restart(execution, stageId);
}

@Override
public void reschedule(@Nonnull PipelineExecution execution) {
executionRunner(execution.getExecutionEngine()).reschedule(execution);
}

@Override
public void unpause(@Nonnull PipelineExecution execution) {
executionRunner(execution.getExecutionEngine()).unpause(execution);
}

@Override
public void cancel(
@Nonnull PipelineExecution execution, @Nonnull String user, @Nullable String reason) {
executionRunner(execution.getExecutionEngine()).cancel(execution, user, reason);
}

@VisibleForTesting
protected ExecutionRunner executionRunner(ExecutionEngine executionEngine) {
return executionRunners.stream()
.filter(it -> it.getClass().isAnnotationPresent(ExecutionEngineVersion.class))
.filter(
it ->
it.getClass().getAnnotation(ExecutionEngineVersion.class).value()
== executionEngine)
.findFirst()
.orElseGet(
() ->
executionRunners.stream()
.filter(it -> it.getClass().isAnnotationPresent(ExecutionEngineVersion.class))
.filter(
it ->
it.getClass().getAnnotation(ExecutionEngineVersion.class).value()
== DEFAULT_EXECUTION_ENGINE)
.findFirst()
.orElseThrow(
() ->
new UnsupportedOperationException(
"No execution engine runner found!")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.kork.exceptions.UserException;
import com.netflix.spinnaker.kork.web.exceptions.ValidationException;
import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine;
import com.netflix.spinnaker.orca.api.pipeline.models.Trigger;
import com.netflix.spinnaker.orca.events.BeforeInitialExecutionPersist;
import com.netflix.spinnaker.orca.pipeline.model.PipelineBuilder;
Expand Down Expand Up @@ -67,7 +69,7 @@ public class ExecutionLauncher {
public ExecutionLauncher(
ObjectMapper objectMapper,
ExecutionRepository executionRepository,
ExecutionRunner executionRunner,
ExecutionEngineRunner executionRunner,
Clock clock,
ApplicationEventPublisher applicationEventPublisher,
Optional<PipelineValidator> pipelineValidator,
Expand Down Expand Up @@ -215,6 +217,7 @@ private PipelineExecution parsePipeline(String configJson) throws IOException {
.withStages((List<Map<String, Object>>) config.get("stages"))
.withLimitConcurrent(getBoolean(config, "limitConcurrent"))
.withKeepWaitingPipelines(getBoolean(config, "keepWaitingPipelines"))
.withExecutionEngine(getEnum(config, "executionEngine", ExecutionEngine.class))
.withNotifications((List<Map<String, Object>>) config.get("notifications"))
.withInitialConfig((Map<String, Object>) config.get("initialConfig"))
.withOrigin(getString(config, "origin"))
Expand All @@ -240,6 +243,9 @@ private PipelineExecution parseOrchestration(String configJson) throws IOExcepti
if (config.containsKey("description")) {
orchestration.setDescription(getString(config, "description"));
}
if (config.containsKey("executionEngine")) {
orchestration.setExecutionEngine(getEnum(config, "executionEngine", ExecutionEngine.class));
}

for (Map<String, Object> context : getList(config, "stages")) {
String type = context.remove("type").toString();
Expand Down Expand Up @@ -299,6 +305,14 @@ private final List<Map<String, Object>> getList(Map<String, ?> map, String key)

private final <E extends Enum<E>> E getEnum(Map<String, ?> map, String key, Class<E> type) {
String value = (String) map.get(key);
return value != null ? Enum.valueOf(type, value) : null;
if (value != null) {
try {
return Enum.valueOf(type, value);
} catch (IllegalArgumentException e) {
log.error(e.getMessage());
return null;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.netflix.spinnaker.orca.pipeline.model;

import static com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.DEFAULT_EXECUTION_ENGINE;

import com.google.common.base.Strings;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution;
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine;
import com.netflix.spinnaker.orca.api.pipeline.models.Trigger;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -117,6 +120,12 @@ public PipelineBuilder withKeepWaitingPipelines(boolean waiting) {
return this;
}

public PipelineBuilder withExecutionEngine(ExecutionEngine executionEngine) {
pipeline.setExecutionEngine(
executionEngine != null ? executionEngine : DEFAULT_EXECUTION_ENGINE);
return this;
}

public PipelineBuilder withOrigin(String origin) {
pipeline.setOrigin(origin);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ public void setStatus(@Nonnull ExecutionStatus status) {
this.status = status;
}

private ExecutionEngine executionEngine = DEFAULT_EXECUTION_ENGINE;

@Override
public ExecutionEngine getExecutionEngine() {
return executionEngine;
}

@Override
public void setExecutionEngine(ExecutionEngine executionEngine) {
this.executionEngine = executionEngine;
}

private AuthenticationDetails authentication;

public @Nullable AuthenticationDetails getAuthentication() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.orca.pipeline

import com.netflix.spinnaker.kork.core.RetrySupport
import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution
import com.netflix.spinnaker.orca.pipeline.model.StageExecutionImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.netflix.spinnaker.orca.pipeline

import com.netflix.spinnaker.orca.api.pipeline.ExecutionRunner
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionEngineVersion
import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution
import org.jetbrains.annotations.Nullable
import spock.lang.Specification
import spock.lang.Unroll

import javax.annotation.Nonnull

import static com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine.v2
import static com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine.v3
import static com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution.ExecutionEngine.v4

class ExecutionEngineRunnerSpec extends Specification {

@Unroll
def "Finds the correct execution runner based on the execution engine"() {
given:
ExecutionEngineRunner executionEngineRunner = new ExecutionEngineRunner([new V3EngineRunner(), new V4EngineRunner()])

when:
def runner = executionEngineRunner.executionRunner(supplied)

then:
runner.class == expected

where:
supplied | expected
v2 | V3EngineRunner.class //v2 is obsolete, use v3 engine
v3 | V3EngineRunner.class
v4 | V4EngineRunner.class
}

@Unroll
def "Throws UnsupportedOperationException when execution engine runner can not be found"() {
given:
ExecutionEngineRunner executionEngineRunner = new ExecutionEngineRunner([new UnsupportedRunner()])

when:
executionEngineRunner.executionRunner(v3)

then:
thrown(UnsupportedOperationException)
}
}

@ExecutionEngineVersion(v3)
class V3EngineRunner implements ExecutionRunner {
@Override
void start(@Nonnull PipelineExecution execution) {}
@Override
void restart(@Nonnull PipelineExecution execution, @Nonnull String stageId) {}
@Override
void reschedule(@Nonnull PipelineExecution execution) {}
@Override
void unpause(@Nonnull PipelineExecution execution) {}
@Override
void cancel(@Nonnull PipelineExecution execution, @Nonnull String user, @Nullable String reason) {}
}

@ExecutionEngineVersion(v4)
class V4EngineRunner implements ExecutionRunner {
@Override
void start(@Nonnull PipelineExecution execution) {}
@Override
void restart(@Nonnull PipelineExecution execution, @Nonnull String stageId) {}
@Override
void reschedule(@Nonnull PipelineExecution execution) {}
@Override
void unpause(@Nonnull PipelineExecution execution) {}
@Override
void cancel(@Nonnull PipelineExecution execution, @Nonnull String user, @Nullable String reason) {}
}

class UnsupportedRunner implements ExecutionRunner {
@Override
void start(@Nonnull PipelineExecution execution) {}
@Override
void restart(@Nonnull PipelineExecution execution, @Nonnull String stageId) {}
@Override
void reschedule(@Nonnull PipelineExecution execution) {}
@Override
void unpause(@Nonnull PipelineExecution execution) {}
@Override
void cancel(@Nonnull PipelineExecution execution, @Nonnull String user, @Nullable String reason) {}
}
Loading

0 comments on commit 7c67278

Please sign in to comment.