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(kustomize): support git/repo artifact in kustomize bake manifest #449

Merged
merged 12 commits into from
Oct 29, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

public interface ArtifactDownloader {
void downloadArtifact(Artifact artifact, Path targetFile) throws IOException;
InputStream downloadArtifact(Artifact artifact) throws IOException;

void downloadArtifactToFile(Artifact artifact, Path targetFile) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ public ArtifactDownloaderImpl(ClouddriverService clouddriverService) {
this.clouddriverService = clouddriverService;
}

public void downloadArtifact(Artifact artifact, Path targetFile) throws IOException {
public InputStream downloadArtifact(Artifact artifact) throws IOException {
Response response =
retrySupport.retry(() -> clouddriverService.fetchArtifact(artifact), 5, 1000, true);
if (response.getBody() != null) {
return response.getBody().in();
} else {
log.error("Failure to fetch artifact: empty response");
return null;
}
}

public void downloadArtifactToFile(Artifact artifact, Path targetFile) throws IOException {
KathrynLewis marked this conversation as resolved.
Show resolved Hide resolved
try (OutputStream outputStream = Files.newOutputStream(targetFile)) {
Response response =
retrySupport.retry(() -> clouddriverService.fetchArtifact(artifact), 5, 1000, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private Path downloadArtifactToTmpFile(BakeManifestEnvironment env, Artifact art
throws IOException {
String fileName = UUID.randomUUID().toString();
Path targetPath = env.resolvePath(fileName);
artifactDownloader.downloadArtifact(artifact, targetPath);
artifactDownloader.downloadArtifactToFile(artifact, targetPath);
return targetPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -64,11 +65,10 @@ public BakeRecipe buildBakeRecipe(
}

String artifactType = Optional.of(artifact.getType()).orElse("");
switch (artifactType) {
case "git/repo":
return buildBakeRecipeFromGitRepo(env, request, artifact);
default:
return oldBuildBakeRecipe(env, request, artifact);
if ("git/repo".equals(artifactType)) {
return buildBakeRecipeFromGitRepo(env, request, artifact);
} else {
return oldBuildBakeRecipe(env, request, artifact);
}
}

Expand Down Expand Up @@ -120,15 +120,15 @@ private BakeRecipe buildBakeRecipeFromGitRepo(
throw new IllegalArgumentException("The bake request should contain a kustomize file path.");
}

Path tarPath = env.resolvePath("repo.tar.gz");
InputStream inputStream;
try {
artifactDownloader.downloadArtifact(artifact, tarPath);
inputStream = artifactDownloader.downloadArtifact(artifact);
} catch (IOException e) {
throw new IOException("Failed to download git/repo artifact: " + e.getMessage(), e);
}

try {
extractArtifact(tarPath, env.resolvePath(""));
extractArtifact(inputStream, env.resolvePath(""));
} catch (IOException e) {
throw new IOException("Failed to extract git/repo artifact: " + e.getMessage(), e);
}
Expand All @@ -144,24 +144,25 @@ private BakeRecipe buildBakeRecipeFromGitRepo(
}

// This being here is temporary until we find a better way to abstract it
public static void extractArtifact(Path inputTar, Path outputPath) throws IOException {
TarArchiveInputStream tarArchiveInputStream =
private static void extractArtifact(InputStream inputStream, Path outputPath) throws IOException {
try (TarArchiveInputStream tarArchiveInputStream =
new TarArchiveInputStream(
new GzipCompressorInputStream(new BufferedInputStream(Files.newInputStream(inputTar))));

ArchiveEntry archiveEntry = null;
while ((archiveEntry = tarArchiveInputStream.getNextEntry()) != null) {
Path archiveEntryOutput = outputPath.resolve(archiveEntry.getName());
if (archiveEntry.isDirectory()) {
if (!Files.exists(archiveEntryOutput)) {
Files.createDirectory(archiveEntryOutput);
new GzipCompressorInputStream(new BufferedInputStream(inputStream)))) {

ArchiveEntry archiveEntry;
while ((archiveEntry = tarArchiveInputStream.getNextEntry()) != null) {
Path archiveEntryOutput = outputPath.resolve(archiveEntry.getName());
if (archiveEntry.isDirectory()) {
if (!Files.exists(archiveEntryOutput)) {
Files.createDirectory(archiveEntryOutput);
}
} else {
Files.copy(tarArchiveInputStream, archiveEntryOutput);
}
} else {
Files.copy(tarArchiveInputStream, archiveEntryOutput);
}
} catch (Exception e) {
System.out.println(e);
KathrynLewis marked this conversation as resolved.
Show resolved Hide resolved
}

tarArchiveInputStream.close();
}

protected void downloadArtifactToTmpFileStructure(
Expand All @@ -173,7 +174,7 @@ protected void downloadArtifactToTmpFileStructure(
Path artifactFilePath = env.resolvePath(artifactFileName);
Path artifactParentDirectory = artifactFilePath.getParent();
Files.createDirectories(artifactParentDirectory);
artifactDownloader.downloadArtifact(artifact, artifactFilePath);
artifactDownloader.downloadArtifactToFile(artifact, artifactFilePath);
}

private List<Artifact> getArtifacts(Artifact artifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void downloadsArtifactContent() throws IOException {
try (ArtifactDownloaderImplTest.AutoDeletingFile file = new AutoDeletingFile()) {
when(clouddriverService.fetchArtifact(testArtifact))
.thenReturn(successfulResponse(testContent));
artifactDownloader.downloadArtifact(testArtifact, file.path);
artifactDownloader.downloadArtifactToFile(testArtifact, file.path);

Assertions.assertThat(file.path).hasContent(testContent);
}
Expand All @@ -63,7 +63,7 @@ public void retries() throws IOException {
when(clouddriverService.fetchArtifact(testArtifact))
.thenThrow(RetrofitError.networkError("", new IOException("timeout")))
.thenReturn(successfulResponse(testContent));
artifactDownloader.downloadArtifact(testArtifact, file.path);
artifactDownloader.downloadArtifactToFile(testArtifact, file.path);

Assertions.assertThat(file.path).hasContent(testContent);
}
Expand Down