Skip to content

Commit

Permalink
fix(aws/findimage): Show warning if search results were truncated (#3594
Browse files Browse the repository at this point in the history
)

* fix(aws/findimage): Show warning if search results were truncated

* chore(build): Ghost commit to force build re-run

* fix(was/findimage): Grammar

Co-Authored-By: Rob Zienert <rob@robzienert.com>

* fix(was/findimage): Better guard condition

Co-Authored-By: Rob Zienert <rob@robzienert.com>

Co-authored-by: Rob Zienert <rob@robzienert.com>
  • Loading branch information
german-muzquiz and robzienert committed May 18, 2020
1 parent 097f184 commit a364d72
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public TaskResult execute(StageExecution stage) {
stageData.tags = Collections.emptyMap();
}

List<String> warnings = new ArrayList<>();
Collection<ImageFinder.ImageDetails> imageDetails =
imageFinder.byTags(stage, stageData.packageName, stageData.tags);
imageFinder.byTags(stage, stageData.packageName, stageData.tags, warnings);

if (imageDetails == null || imageDetails.isEmpty()) {
throw new IllegalStateException(
Expand All @@ -82,6 +83,14 @@ public TaskResult execute(StageExecution stage) {
stageOutputs.put("amiDetails", imageDetails);
stageOutputs.put("artifacts", artifacts);

if (!warnings.isEmpty()) {
Map<String, String[]> messages = new HashMap<>();
messages.put("errors", warnings.toArray(new String[0]));
Map<String, Object> details = new HashMap<>();
details.put("details", messages);
stageOutputs.put("exception", details);
}

return TaskResult.builder(ExecutionStatus.SUCCEEDED)
.context(stageOutputs)
.outputs(Collections.singletonMap("deploymentDetails", imageDetails))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ImageFinder {
Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> tags);
StageExecution stage,
String packageName,
Map<String, String> tags,
List<String> warningsCollector);

String getCloudProvider();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public AliCloudImageFinder(OortService oortService, ObjectMapper objectMapper) {

@Override
public Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> tags) {
StageExecution stage,
String packageName,
Map<String, String> tags,
List<String> warningsCollector) {
List<AliCloudImage> allMatchedImages =
oortService.findImage(getCloudProvider(), packageName, null, null, prefixTags(tags))
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

@Component
public class AmazonImageFinder implements ImageFinder {

private static final int MAX_SEARCH_RESULTS = 1000;

@Autowired OortService oortService;

@Autowired ObjectMapper objectMapper;
Expand Down Expand Up @@ -75,7 +78,10 @@ public boolean test(AmazonImage amazonImage) {

@Override
public Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> tags) {
StageExecution stage,
String packageName,
Map<String, String> tags,
List<String> warningsCollector) {
StageData stageData = (StageData) stage.mapTo(StageData.class);
List<AmazonImage> allMatchedImages =
oortService.findImage(getCloudProvider(), packageName, null, null, prefixTags(tags))
Expand All @@ -85,6 +91,11 @@ public Collection<ImageDetails> byTags(
.sorted()
.collect(Collectors.toList());

if (allMatchedImages.size() >= MAX_SEARCH_RESULTS) {
warningsCollector.add(
"Too many results matching search criteria: Consider refining the search.");
}

AppVersionFilter filter = new AppVersionFilter(packageName, stageData.regions);
List<AmazonImage> appversionMatches =
allMatchedImages.stream().filter(filter).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public class EcsImageFinder implements ImageFinder {

@Override
public Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> tags) {
StageExecution stage,
String packageName,
Map<String, String> tags,
List<String> warningsCollector) {
StageData stageData = (StageData) stage.mapTo(StageData.class);

List<Map> result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public class GoogleImageFinder implements ImageFinder {

@Override
public Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> tags) {
StageExecution stage,
String packageName,
Map<String, String> tags,
List<String> warningsCollector) {
List<GoogleImage> allMatchedImages =
oortService.findImage(getCloudProvider(), packageName, null, null, prefixTags(tags))
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public OracleImageFinder(OortService oortService, ObjectMapper objectMapper) {

@Override
public Collection<ImageDetails> byTags(
StageExecution stage, String packageName, Map<String, String> freeformTags) {
StageExecution stage,
String packageName,
Map<String, String> freeformTags,
List<String> warningsCollector) {

StageData stageData = (StageData) stage.mapTo(StageData.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FindImageFromTagTaskSpec extends Specification {
result.status == ExecutionStatus.SUCCEEDED

1 * imageFinder.getCloudProvider() >> 'aws'
1 * imageFinder.byTags(stage, stage.context.packageName, stage.context.tags) >> [imageDetails]
1 * imageFinder.byTags(stage, stage.context.packageName, stage.context.tags, []) >> [imageDetails]
1 * imageDetails.getImageName() >> "somename"
1 * imageDetails.getImageId() >> "someId"
1 * imageDetails.getJenkins() >> new ImageFinder.JenkinsDetails("somehost", "somename", "42")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AmazonImageFinderSpec extends Specification {
def noTags = [:]

when:
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"])
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"], [])

then:
1 * oortService.findImage("aws", "mypackage", null, null, ["tag:engine": "spinnaker"]) >> {
Expand Down Expand Up @@ -137,7 +137,7 @@ class AmazonImageFinderSpec extends Specification {
]

when:
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"])
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"], [])

then:
1 * oortService.findImage("aws", "mypackage", null, null, ["tag:engine": "spinnaker"]) >> {
Expand Down Expand Up @@ -196,7 +196,7 @@ class AmazonImageFinderSpec extends Specification {
]

when:
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"])
def imageDetails = amazonImageFinder.byTags(stage, "mypackage", ["engine": "spinnaker"], [])

then:
1 * oortService.findImage("aws", "mypackage", null, null, ["tag:engine": "spinnaker"]) >> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GoogleImageFinderSpec extends Specification {
]

when:
def imageDetails = googleImageFinder.byTags(null, "mypackage", ["engine": "spinnaker"])
def imageDetails = googleImageFinder.byTags(null, "mypackage", ["engine": "spinnaker"], [])

then:
1 * oortService.findImage("gce", "mypackage", null, null, ["tag:engine": "spinnaker"]) >> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OracleImageFinderSpec extends Specification {

when:
Collection<ImageFinder.ImageDetails> imageDetails = oracleImageFinder.byTags(
stage, "imageName*", ["version": "latest"])
stage, "imageName*", ["version": "latest"], [])

then:
1 * oortService.findImage("oracle", "imageName*", null, null, ["tag:version": "latest"]) >> {
Expand All @@ -66,7 +66,7 @@ class OracleImageFinderSpec extends Specification {
]

when:
def imageDetails = oracleImageFinder.byTags(stage, "image*", ["version": "latest"])
def imageDetails = oracleImageFinder.byTags(stage, "image*", ["version": "latest"], [])

then:
1 * oortService.findImage("oracle", "image*", null, null, ["tag:version": "latest"]) >> {
Expand Down

0 comments on commit a364d72

Please sign in to comment.