Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): filled in scaffolding #11

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions orca-api/orca-api.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply from: "$rootDir/gradle/kotlin.gradle"
apply from: "$rootDir/gradle/spock.gradle"

test {
useJUnitPlatform {
includeEngines "junit-vintage", "junit-jupiter"
}
}

dependencies {
implementation("com.google.guava:guava")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.api;

import com.google.common.annotations.Beta;

@Beta
public interface SimpleStage<T> {
<T> SimpleStageOutput execute(SimpleStageInput<T> simpleStageInput);

String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.api;

import com.google.common.annotations.Beta;

@Beta
public class SimpleStageInput<T> {
private T value;

public SimpleStageInput(T value) {
this.value = value;
}

public void setValue(T value) {
this.value = value;
}

public T getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.api;

import com.google.common.annotations.Beta;
import java.util.Map;

@Beta
public class SimpleStageOutput {
private SimpleStageStatus status;

public void setStatus(SimpleStageStatus status) {
this.status = status;
}

public SimpleStageStatus getStatus() {
return this.status;
}

private Map outputs;

public void setOutputs(Map outputs) {
this.outputs = outputs;
}

public Map getOutputs() {
return this.outputs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.api;

import com.google.common.annotations.Beta;

@Beta
public enum SimpleStageStatus {
TERMINAL,
RUNNING,
COMPLETED,
NOT_STARTED
}
1 change: 1 addition & 0 deletions orca-core/orca-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
api("io.reactivex:rxjava")

implementation(project(":orca-extensionpoint"))
implementation(project(":orca-api"))
implementation("com.github.ben-manes.caffeine:guava")
implementation("org.slf4j:slf4j-api")
implementation("com.fasterxml.jackson.core:jackson-annotations")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import static java.lang.String.format;

import com.netflix.spinnaker.orca.api.SimpleStage;
import com.netflix.spinnaker.orca.pipeline.SimpleStageDefinitionBuilder;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nonnull;

/**
Expand All @@ -32,7 +35,9 @@
public class StageResolver {
private final Map<String, StageDefinitionBuilder> stageDefinitionBuilderByAlias = new HashMap<>();

public StageResolver(Collection<StageDefinitionBuilder> stageDefinitionBuilders) {
public StageResolver(
Collection<StageDefinitionBuilder> stageDefinitionBuilders,
Collection<SimpleStage> apiStages) {
for (StageDefinitionBuilder stageDefinitionBuilder : stageDefinitionBuilders) {
stageDefinitionBuilderByAlias.put(stageDefinitionBuilder.getType(), stageDefinitionBuilder);
for (String alias : stageDefinitionBuilder.aliases()) {
Expand All @@ -48,6 +53,13 @@ public StageResolver(Collection<StageDefinitionBuilder> stageDefinitionBuilders)
stageDefinitionBuilderByAlias.put(alias, stageDefinitionBuilder);
}
}

if (!Objects.equals(apiStages, null)) {
for (SimpleStage stage : apiStages) {
SimpleStageDefinitionBuilder builder = new SimpleStageDefinitionBuilder(stage);
stageDefinitionBuilderByAlias.put(stage.getName(), builder);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.netflix.spinnaker.orca.StageResolver;
import com.netflix.spinnaker.orca.Task;
import com.netflix.spinnaker.orca.TaskResolver;
import com.netflix.spinnaker.orca.api.SimpleStage;
import com.netflix.spinnaker.orca.commands.ForceExecutionCancellationCommand;
import com.netflix.spinnaker.orca.events.ExecutionEvent;
import com.netflix.spinnaker.orca.events.ExecutionListenerAdapter;
Expand All @@ -40,8 +41,7 @@
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor;
import java.time.Clock;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -187,8 +187,12 @@ public TaskResolver taskResolver(Collection<Task> tasks) {
}

@Bean
public StageResolver stageResolver(Collection<StageDefinitionBuilder> stageDefinitionBuilders) {
return new StageResolver(stageDefinitionBuilders);
public StageResolver stageResolver(
Collection<StageDefinitionBuilder> stageDefinitionBuilders,
Optional<Collection<SimpleStage>> simpleStages) {
Collection<SimpleStage> stages =
simpleStages.isPresent() ? simpleStages.get() : new ArrayList<SimpleStage>();
return new StageResolver(stageDefinitionBuilders, stages);
}

@Bean(name = EVENT_LISTENER_FACTORY_BEAN_NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.pipeline;

import com.netflix.spinnaker.orca.api.SimpleStage;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import javax.annotation.Nonnull;

public class SimpleStageDefinitionBuilder implements StageDefinitionBuilder {
private SimpleStage simpleStage;

public SimpleStageDefinitionBuilder(SimpleStage simpleStage) {
this.simpleStage = simpleStage;
}

public void taskGraph(@Nonnull Stage stage, @Nonnull TaskNode.Builder builder) {
SimpleTask task = new SimpleTask(simpleStage);
builder.withTask(simpleStage.getName(), task.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.pipeline;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.Task;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.api.SimpleStage;
import com.netflix.spinnaker.orca.api.SimpleStageInput;
import com.netflix.spinnaker.orca.api.SimpleStageOutput;
import com.netflix.spinnaker.orca.jackson.OrcaObjectMapper;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SimpleTask implements Task {
private SimpleStage simpleStage;

SimpleTask(@Nullable SimpleStage simpleStage) {
this.simpleStage = simpleStage;
}

@Nonnull
public TaskResult execute(@Nonnull Stage stage) {
ObjectMapper objectMapper = OrcaObjectMapper.newInstance();

ExecutionStatus status;
SimpleStageOutput outputs = new SimpleStageOutput();

try {
List<Class<?>> cArg = Arrays.asList(SimpleStageInput.class);
Method method = simpleStage.getClass().getMethod("execute", cArg.toArray(new Class[0]));
Type inputType = ResolvableType.forMethodParameter(method, 0).getGeneric().getType();
Map<TypeVariable, Type> typeVariableMap =
GenericTypeResolver.getTypeVariableMap(simpleStage.getClass());

SimpleStageInput simpleStageInput =
new SimpleStageInput(
objectMapper.convertValue(
stage.getContext(), GenericTypeResolver.resolveType(inputType, typeVariableMap)));
outputs = simpleStage.execute(simpleStageInput);
switch (outputs.getStatus()) {
case TERMINAL:
status = ExecutionStatus.TERMINAL;
break;
case RUNNING:
status = ExecutionStatus.RUNNING;
break;
case COMPLETED:
status = ExecutionStatus.SUCCEEDED;
break;
case NOT_STARTED:
status = ExecutionStatus.NOT_STARTED;
break;
default:
status = ExecutionStatus.FAILED_CONTINUE;
break;
}
} catch (Exception e) {
log.error("Cannot execute stage " + simpleStage.getName());
log.error(e.getMessage());
status = ExecutionStatus.TERMINAL;
}

return TaskResult.builder(status)
.context(outputs.getOutputs())
.outputs(outputs.getOutputs())
.build();
}
}
Loading