Skip to content

Commit

Permalink
fix(helm): Fix baking of helm artifacts (#442)
Browse files Browse the repository at this point in the history
We currently throw an exception if a helm artifact doesn't have a
reference. We're only using that reference to generate a file name
to use when downloading the artifact; helm artifacts don't actually
need a reference.

Fix this by just falling back to the name-version if there is no
reference defined.
  • Loading branch information
ezimanyi committed Oct 10, 2019
1 parent c0fdcab commit 746fa9c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions rosco-manifests/rosco-manifests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ dependencies {
implementation "org.yaml:snakeyaml:1.25"

implementation "com.squareup.retrofit:retrofit"
testImplementation "org.assertj:assertj-core"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.platform:junit-platform-runner"
testImplementation "org.mockito:mockito-core"
testImplementation "org.spockframework:spock-core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.xml.bind.DatatypeConverter;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.util.ArrayUtils;
Expand Down Expand Up @@ -115,13 +117,22 @@ private String nameFromReference(String reference) {
}
}

@Nonnull
private String uniqueKey(Artifact artifact) {
if (artifact.getReference() != null) {
return artifact.getReference();
}
if (artifact.getName() != null) {
return String.format(
"%s-%s", artifact.getName(), Optional.ofNullable(artifact.getVersion()).orElse(""));
}
throw new InvalidRequestException("Input artifact has empty 'name' and 'reference' fields.");
}

protected Path downloadArtifactToTmpFile(BakeManifestEnvironment env, Artifact artifact)
throws IOException {
if (artifact.getReference() == null) {
throw new InvalidRequestException("Input artifact has an empty 'reference' field.");
}
File targetFile =
env.getStagingPath().resolve(nameFromReference(artifact.getReference())).toFile();
String uniqueKey = uniqueKey(artifact);
File targetFile = env.getStagingPath().resolve(nameFromReference(uniqueKey)).toFile();
downloadArtifact(artifact, targetFile);
return targetFile.toPath();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 Google, LLC
*
* 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.rosco.manifests.helm;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.rosco.jobs.BakeRecipe;
import com.netflix.spinnaker.rosco.manifests.BakeManifestEnvironment;
import com.netflix.spinnaker.rosco.services.ClouddriverService;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;

@RunWith(JUnitPlatform.class)
final class HelmTemplateUtilsTest {
@Test
public void nullReferenceTest() throws IOException {
ClouddriverService clouddriverService = mock(ClouddriverService.class);
HelmTemplateUtils helmTemplateUtils = new HelmTemplateUtils(clouddriverService);
Artifact chartArtifact = Artifact.builder().name("test-artifact").version("3").build();

HelmBakeManifestRequest bakeManifestRequest = new HelmBakeManifestRequest();
bakeManifestRequest.setInputArtifacts(ImmutableList.of(chartArtifact));
bakeManifestRequest.setOverrides(ImmutableMap.of());

when(clouddriverService.fetchArtifact(chartArtifact)).thenReturn(emptyRepsonse());
try (BakeManifestEnvironment env = BakeManifestEnvironment.create()) {
BakeRecipe recipe = helmTemplateUtils.buildBakeRecipe(env, bakeManifestRequest);
}
}

private Response emptyRepsonse() {
return new Response("", 200, "", ImmutableList.of(), new TypedByteArray(null, new byte[0]));
}
}

0 comments on commit 746fa9c

Please sign in to comment.