From 274420afedbfb682d9b42df52b29b0fe54abbbab Mon Sep 17 00:00:00 2001 From: Eric Zimanyi Date: Thu, 5 Apr 2018 12:14:38 -0400 Subject: [PATCH] feat(artifacts): Orca sends an an artifact to clouddriver (#2121) When a deploy description sets the image to deploy to an artifact, Orca should send the entire artifact to clouddriver, rather than just the name of the image. --- .../gce/GoogleServerGroupCreator.groovy | 44 +++++++++++-------- .../gce/GoogleServerGroupCreatorSpec.groovy | 22 +++++----- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/orca-clouddriver/src/main/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreator.groovy b/orca-clouddriver/src/main/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreator.groovy index 95c472fd26..12b6aba507 100644 --- a/orca-clouddriver/src/main/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreator.groovy +++ b/orca-clouddriver/src/main/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreator.groovy @@ -16,6 +16,7 @@ package com.netflix.spinnaker.orca.clouddriver.tasks.providers.gce +import com.netflix.spinnaker.kork.artifacts.model.Artifact import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.ServerGroupCreator import com.netflix.spinnaker.orca.kato.tasks.DeploymentDetailsAware import com.netflix.spinnaker.orca.pipeline.model.Stage @@ -49,34 +50,39 @@ class GoogleServerGroupCreator implements ServerGroupCreator, DeploymentDetailsA operation.credentials = operation.account } - operation.image = operation.image ?: getImage(stage) - - if (!operation.image) { - throw new IllegalStateException("No image could be found in ${stage.context.region}.") + if (stage.context.imageSource == "artifact") { + operation.imageSource = "ARTIFACT" + operation.imageArtifact = getImageArtifact(stage) + } else { + operation.imageSource = "STRING" + operation.image = operation.image ?: getImage(stage) + if (!operation.image) { + throw new IllegalStateException("No image could be found in ${stage.context.region}.") + } } return [[(ServerGroupCreator.OPERATION): operation]] } - private String getImage(Stage stage) { + private Artifact getImageArtifact(Stage stage) { def stageContext = stage.getContext() + + def artifactId = stageContext.imageArtifactId as String + if (artifactId == null) { + throw new IllegalArgumentException("Image source was set to artifact but no artifact was specified.") + } + return artifactResolver.getBoundArtifactForId(stage, artifactId) + } + + private String getImage(Stage stage) { String image - if (stageContext.imageSource == "artifact") { - def artifactId = stageContext.imageArtifactId - if (artifactId == null) { - throw new IllegalStateException("Image source was set to artifact but no artifact was specified.") - } - def resolvedArtifact = artifactResolver.getBoundArtifactForId(stage, artifactId) - image = resolvedArtifact.getName() - } else { - withImageFromPrecedingStage(stage, null, cloudProvider) { - image = image ?: it.imageId - } + withImageFromPrecedingStage(stage, null, cloudProvider) { + image = image ?: it.imageId + } - withImageFromDeploymentDetails(stage, null, cloudProvider) { - image = image ?: it.imageId - } + withImageFromDeploymentDetails(stage, null, cloudProvider) { + image = image ?: it.imageId } return image diff --git a/orca-clouddriver/src/test/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreatorSpec.groovy b/orca-clouddriver/src/test/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreatorSpec.groovy index 912fc1ee09..483882a5c7 100644 --- a/orca-clouddriver/src/test/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreatorSpec.groovy +++ b/orca-clouddriver/src/test/groovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/GoogleServerGroupCreatorSpec.groovy @@ -50,6 +50,7 @@ class GoogleServerGroupCreatorSpec extends Specification { account : "abc", credentials : "abc", image : "specifiedImage", + imageSource : "STRING", region : "north-pole", zone : "north-pole-1", ], @@ -70,6 +71,7 @@ class GoogleServerGroupCreatorSpec extends Specification { account : "abc", credentials : "abc", image : "testImageId", + imageSource : "STRING", region : "north-pole", zone : "north-pole-1", deploymentDetails: [[imageId: "testImageId", region: "north-pole"]], @@ -93,6 +95,7 @@ class GoogleServerGroupCreatorSpec extends Specification { account : "abc", credentials : "abc", image : "testImageId", + imageSource : "STRING", region : "south-pole", zone : "south-pole-1", deploymentDetails: [[imageId: "testImageId", region: "north-pole"]], @@ -107,22 +110,19 @@ class GoogleServerGroupCreatorSpec extends Specification { stage = ExecutionBuilder.stage { context.putAll(ctx) } - artifactResolver.getBoundArtifactForId(*_) >> { - Artifact artifact = new Artifact(); - artifact.setName("santaImage") - return artifact - } + Artifact buildArtifact = Artifact.ArtifactBuilder.newInstance().name("santaImage").build(); + artifactResolver.getBoundArtifactForId(*_) >> buildArtifact ops = new GoogleServerGroupCreator(artifactResolver: artifactResolver).getOperations(stage) then: ops == [ [ "createServerGroup": [ - imageSource : "artifact", + imageSource : "ARTIFACT", + imageArtifact : buildArtifact, imageArtifactId : "b3d33e5a-0423-4bdc-8e37-dea923b57c9a", account : "abc", credentials : "abc", - image : "santaImage", region : "north-pole", zone : "north-pole-1", deploymentDetails: [[imageId: "testImageId", region: "north-pole"]], @@ -144,8 +144,8 @@ class GoogleServerGroupCreatorSpec extends Specification { ops = new GoogleServerGroupCreator(artifactResolver: artifactResolver).getOperations(stage) then: - IllegalStateException ise = thrown() - ise.message == "Image source was set to artifact but no artifact was specified." + IllegalArgumentException illegalArgumentException = thrown() + illegalArgumentException.message == "Image source was set to artifact but no artifact was specified." when: "throw error if no image found" ctx = [:] << basectx @@ -156,7 +156,7 @@ class GoogleServerGroupCreatorSpec extends Specification { new GoogleServerGroupCreator(artifactResolver: artifactResolver).getOperations(stage) then: - ise = thrown() - ise.message == "No image could be found in north-pole." + IllegalStateException illegalStagteException = thrown() + illegalStagteException.message == "No image could be found in north-pole." } }