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 12 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")
}
26 changes: 26 additions & 0 deletions orca-api/src/main/java/com/netflix/spinnaker/orca/api/Stage.java
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 Stage<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of duplicated class name. It hurts readability and increases complexity by having to determine which type of Stage object you're working with.

<T> StageOutput execute(StageInput<T> stageInput);

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 StageInput<T> {
private T value;

public StageInput(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 StageOutput {
private StageStatus status;

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

public StageStatus 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 StageStatus {
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.Stage;
import com.netflix.spinnaker.orca.pipeline.ApiStageDefinitionBuilder;
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,8 @@
public class StageResolver {
private final Map<String, StageDefinitionBuilder> stageDefinitionBuilderByAlias = new HashMap<>();

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

if (!Objects.equals(apiStages, null)) {
for (Stage stage : apiStages) {
ApiStageDefinitionBuilder builder = new ApiStageDefinitionBuilder(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.Stage;
import com.netflix.spinnaker.orca.commands.ForceExecutionCancellationCommand;
import com.netflix.spinnaker.orca.events.ExecutionEvent;
import com.netflix.spinnaker.orca.events.ExecutionListenerAdapter;
Expand Down Expand Up @@ -187,8 +188,9 @@ public TaskResolver taskResolver(Collection<Task> tasks) {
}

@Bean
public StageResolver stageResolver(Collection<StageDefinitionBuilder> stageDefinitionBuilders) {
return new StageResolver(stageDefinitionBuilders);
public StageResolver stageResolver(
Collection<StageDefinitionBuilder> stageDefinitionBuilders, Collection<Stage> apiStages) {
return new StageResolver(stageDefinitionBuilders, apiStages);
}

@Bean(name = EVENT_LISTENER_FACTORY_BEAN_NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.pipeline.model.Stage;
import javax.annotation.Nonnull;

public class ApiStageDefinitionBuilder implements StageDefinitionBuilder {
private com.netflix.spinnaker.orca.api.Stage apiStage;

public ApiStageDefinitionBuilder(com.netflix.spinnaker.orca.api.Stage apiStage) {
this.apiStage = apiStage;
}

public void taskGraph(@Nonnull Stage stage, @Nonnull TaskNode.Builder builder) {
ApiTask task = new ApiTask(apiStage);
builder.withTask(apiStage.getName(), task.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.StageInput;
import com.netflix.spinnaker.orca.api.StageOutput;
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.Map;
import javax.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ApiTask implements Task {
private com.netflix.spinnaker.orca.api.Stage apiStage;

ApiTask(com.netflix.spinnaker.orca.api.Stage apiStage) {
this.apiStage = apiStage;
}

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

ExecutionStatus status;
try {
Class[] cArg = new Class[1];
cArg[0] = StageInput.class;

Choose a reason for hiding this comment

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

You can do Arrays.asList to clean this up.

Method method = apiStage.getClass().getMethod("execute", cArg);
Type type = ResolvableType.forMethodParameter(method, 0).getGeneric().getType();

Choose a reason for hiding this comment

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

Small nit: imight be a bit more descriptive to use inputType instead of type for this variable name.

Copy link
Author

Choose a reason for hiding this comment

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

👍

Map<TypeVariable, Type> typeVariableMap =
GenericTypeResolver.getTypeVariableMap(apiStage.getClass());

StageInput stageInput =
new StageInput(
objectMapper.convertValue(
stage.getContext(), GenericTypeResolver.resolveType(type, typeVariableMap)));

Choose a reason for hiding this comment

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

So, the idea here is that I can have a StageInput class which looks like

public class MyPluginStageInput extends StageInput {
  public String foo;
}

and use it as my stage input like so

public execute(MyPluginStageInput input) {}

Is this correct?

Copy link
Author

Choose a reason for hiding this comment

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

One would create a new class like:

public class MyPluginStageInput {
}

and then in their new stage do:

public class MyStage implements Stage<MyPluginStageInput> {
  @Override
  public <MyPluginStageInput> StageOutput execute(StageInput<MyPluginStageInput> stageInput) {

StageOutput outputs = apiStage.execute(stageInput);
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 " + apiStage.getName());
log.error(e.getMessage());
status = ExecutionStatus.TERMINAL;
}

return TaskResult.ofStatus(status);

Choose a reason for hiding this comment

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

Don't we want to throw the outputs of the stage back into the context here? This would allow us to use any outputs from the stage in downstream stages.

TaskResult.builder(status).context(outputs.getOutputs()).outputs(outputs.getOutputs()).build();

}
}
1 change: 1 addition & 0 deletions orca-web/orca-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation("net.logstash.logback:logstash-logback-encoder")

implementation(project(":orca-core"))
implementation(project(":orca-api"))
implementation(project(":orca-redis"))
implementation(project(":orca-bakery"))
implementation(project(":orca-clouddriver"))
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

include "orca-extensionpoint",
"orca-core",
"orca-api",
"orca-core-tck",
"orca-redis",
"orca-test-redis",
Expand Down