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(provider/kubernetes): deploy from artifact #1831

Merged
merged 1 commit into from
Dec 4, 2017
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ allprojects {
group = "com.netflix.spinnaker.orca"

ext {
spinnakerDependenciesVersion = project.hasProperty('spinnakerDependenciesVersion') ? project.property('spinnakerDependenciesVersion') : '0.125.0'
spinnakerDependenciesVersion = project.hasProperty('spinnakerDependenciesVersion') ? project.property('spinnakerDependenciesVersion') : '0.127.0'
}

def checkLocalVersions = [spinnakerDependenciesVersion: spinnakerDependenciesVersion]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,35 @@

import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact;
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 com.netflix.spinnaker.orca.pipeline.model.StageContext;
import com.netflix.spinnaker.orca.pipeline.util.ArtifactResolver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DeployManifestTask extends AbstractCloudProviderAwareTask implements Task {
@Autowired
KatoService kato;

@Autowired
ArtifactResolver artifactResolver;

public static final String TASK_NAME = "deployManifest";

@Nonnull
Expand All @@ -53,24 +56,20 @@ public TaskResult execute(@Nonnull Stage stage) {
String credentials = getCredentials(stage);
String cloudProvider = getCloudProvider(stage);

List<Object> artifacts = new ArrayList<>();
if (stage.getContext() instanceof StageContext) {
artifacts = (List<Object>) ((StageContext) stage.getContext()).getAll("artifacts")
.stream()
.filter(a -> a instanceof List)
.flatMap(x -> ((List) x).stream())
.collect(Collectors.toList());
} else {
log.warn("Unable to read artifacts from unknown context type: {} ({})", stage.getContext().getClass(), stage.getExecution().getId());
List<Artifact> artifacts = artifactResolver.getArtifacts(stage);
Map task = new HashMap(stage.getContext());
String artifactSource = (String) task.get("source");
if (StringUtils.isNotEmpty(artifactSource) && artifactSource.equals("artifact")) {
Artifact manifestArtifact = artifactResolver.getBoundArtifactForId(stage, task.get("manifestArtifactId").toString());
task.put("manifestArtifact", manifestArtifact);
log.info("Using {} as the manifest to be deployed", manifestArtifact);
}

Map task = new HashMap(stage.getContext());
task.put("artifacts", artifacts);
Map<String, Map> operation = new ImmutableMap.Builder<String, Map>()
.put(TASK_NAME, task)
.build();

System.out.println("op = " + operation.keySet());
TaskId taskId = kato.requestOperations(cloudProvider, Collections.singletonList(operation)).toBlocking().first();

Map<String, Object> outputs = new ImmutableMap.Builder<String, Object>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,51 @@ package com.netflix.spinnaker.orca.pipeline.util
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.kork.artifacts.model.Artifact
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.model.StageContext
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import rx.schedulers.Schedulers

@Component
@Slf4j
class ArtifactResolver {

@Autowired
private ObjectMapper objectMapper

List<Artifact> getArtifacts(Stage stage) {
List<Artifact> artifacts = new ArrayList<>()
if (stage.getContext() instanceof StageContext) {
artifacts = (List<Artifact>) ((StageContext) stage.getContext()).getAll("artifacts")
.collect { s -> (List<Artifact>) ((List) s).collect { a -> (Artifact) a }}
.flatten()
} else {
log.warn("Unable to read artifacts from unknown context type: {} ({})", stage.getContext().getClass(), stage.getExecution().getId());
}

return artifacts
}

Artifact getBoundArtifactForId(Stage stage, String id) {
if (!id) {
return null
}

List<ExpectedArtifact> expectedArtifacts = new ArrayList<>()
if (stage.getContext() instanceof StageContext) {
expectedArtifacts = (List<ExpectedArtifact>) ((StageContext) stage.getContext()).getAll("resolvedExpectedArtifacts")
.collect { s -> (List<ExpectedArtifact>) ((List) s).collect { a -> (ExpectedArtifact) a }}
.flatten()
} else {
log.warn("Unable to read resolved expected artifacts from unknown context type: {} ({})", stage.getContext().getClass(), stage.getExecution().getId());
}

return expectedArtifacts.find { e -> e.getId() == id }?.boundArtifact
}

void resolveArtifacts(ExecutionRepository repository, Map pipeline) {
List<ExpectedArtifact> expectedArtifacts = pipeline.expectedArtifacts?.collect { objectMapper.convertValue(it, ExpectedArtifact.class) } ?: []
List<Artifact> receivedArtifacts = pipeline.receivedArtifacts?.collect { objectMapper.convertValue(it, Artifact.class) } ?: []
Expand Down Expand Up @@ -67,6 +101,7 @@ class ArtifactResolver {
if (!resolved) {
throw new IllegalStateException("Unmatched expected artifact ${expectedArtifact} could not be resolved.")
} else {
expectedArtifact.boundArtifact = resolved
resolvedArtifacts.add(resolved)
}
}
Expand All @@ -93,6 +128,7 @@ class ArtifactResolver {
for (ExpectedArtifact expectedArtifact : expectedArtifacts) {
Artifact resolved = resolveSingleArtifact(expectedArtifact, receivedArtifacts)
if (resolved) {
expectedArtifact.boundArtifact = resolved
result.resolvedArtifacts.add(resolved)
} else {
result.unresolvedExpectedArtifacts.add(expectedArtifact)
Expand Down