Skip to content

Commit

Permalink
refactor(kubernetes): clean up source manifest resolution (#3402)
Browse files Browse the repository at this point in the history
* refactor(kubernetes): add tests for ManifestEvaluator

* refactor(kubernetes): clean up ManifestEvaluator

* refactor(kubernetes): move task-specific ManifestEvaluator logic to tasks

* refactor(kubernetes): extract source manifest resolution to own task

* refactor(plumpstuff): address code review comments

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
maggieneterval and mergify[bot] authored Jan 31, 2020
1 parent cd38c8d commit ead2877
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.netflix.spinnaker.orca.clouddriver.tasks.artifacts.CleanupArtifactsTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.*;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.DeployManifestContext.TrafficManagement;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.ResolveDeploySourceManifestTask;
import com.netflix.spinnaker.orca.clouddriver.utils.OortHelper;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder;
import com.netflix.spinnaker.orca.pipeline.TaskNode;
Expand All @@ -46,6 +47,7 @@ public class DeployManifestStage implements StageDefinitionBuilder {
@Override
public void taskGraph(@Nonnull Stage stage, @Nonnull TaskNode.Builder builder) {
builder
.withTask(ResolveDeploySourceManifestTask.TASK_NAME, ResolveDeploySourceManifestTask.class)
.withTask(DeployManifestTask.TASK_NAME, DeployManifestTask.class)
.withTask("monitorDeploy", MonitorKatoTask.class)
.withTask(PromoteManifestKatoOutputsTask.TASK_NAME, PromoteManifestKatoOutputsTask.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.ManifestForceCacheRefreshTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.PatchManifestTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.PromoteManifestKatoOutputsTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.ResolvePatchSourceManifestTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.ResolveTargetManifestTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.manifest.WaitForManifestStableTask;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder;
Expand All @@ -36,6 +37,7 @@ public class PatchManifestStage implements StageDefinitionBuilder {
public void taskGraph(Stage stage, TaskNode.Builder builder) {
builder
.withTask(ResolveTargetManifestTask.TASK_NAME, ResolveTargetManifestTask.class)
.withTask(ResolvePatchSourceManifestTask.TASK_NAME, ResolvePatchSourceManifestTask.class)
.withTask(PatchManifestTask.TASK_NAME, PatchManifestTask.class)
.withTask("monitorPatch", MonitorKatoTask.class)
.withTask(PromoteManifestKatoOutputsTask.TASK_NAME, PromoteManifestKatoOutputsTask.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,49 @@

package com.netflix.spinnaker.orca.clouddriver.tasks.manifest;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.Task;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.clouddriver.KatoService;
import com.netflix.spinnaker.orca.clouddriver.model.TaskId;
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class DeployManifestTask extends AbstractCloudProviderAwareTask implements Task {
@NonnullByDefault
public final class DeployManifestTask extends AbstractCloudProviderAwareTask implements Task {
public static final String TASK_NAME = "deployManifest";

private final ManifestEvaluator manifestEvaluator;
private final KatoService katoService;

@Autowired
public DeployManifestTask(KatoService katoService) {
this.katoService = katoService;
}

@Nonnull
@Override
public TaskResult execute(@Nonnull Stage stage) {
public TaskResult execute(Stage stage) {
ImmutableMap<String, Map> operation = getOperation(stage);
TaskId taskId = executeOperation(stage, operation);
ImmutableMap<String, Object> outputs = getOutputs(stage, taskId);
return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(outputs).build();
}

private ImmutableMap<String, Map> getOperation(Stage stage) {
DeployManifestContext context = stage.mapTo(DeployManifestContext.class);
ManifestEvaluator.Result result = manifestEvaluator.evaluate(stage, context);

Map<String, Object> task = new HashMap<>(stage.getContext());

task.put("source", "text");
task.put("manifests", result.getManifests());
task.put("requiredArtifacts", result.getRequiredArtifacts());
task.put("optionalArtifacts", result.getOptionalArtifacts());

if (context.getTrafficManagement() != null && context.getTrafficManagement().isEnabled()) {
if (context.getTrafficManagement().isEnabled()) {
task.put("services", context.getTrafficManagement().getOptions().getServices());
task.put("enableTraffic", context.getTrafficManagement().getOptions().isEnableTraffic());
task.put("strategy", context.getTrafficManagement().getOptions().getStrategy().name());
Expand All @@ -60,6 +70,21 @@ public TaskResult execute(@Nonnull Stage stage) {
task.put("enableTraffic", true);
}

return manifestEvaluator.buildTaskResult(TASK_NAME, stage, task);
return ImmutableMap.of(TASK_NAME, task);
}

private TaskId executeOperation(Stage stage, ImmutableMap<String, Map> operation) {
return katoService
.requestOperations(getCloudProvider(stage), ImmutableList.of(operation))
.toBlocking()
.first();
}

private ImmutableMap<String, Object> getOutputs(Stage stage, TaskId taskId) {
return new ImmutableMap.Builder<String, Object>()
.put("kato.result.expected", true)
.put("kato.last.task.id", taskId)
.put("deploy.account.name", Objects.requireNonNull(getCredentials(stage)))
.build();
}
}
Loading

0 comments on commit ead2877

Please sign in to comment.