-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add ExtractTarStep and SaveDockerStep #1906
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fefd959
Progress
TadCordle 6c12fd9
Progress
TadCordle 55ab992
Simplification
TadCordle 5e5b829
More simplification
TadCordle 05d48ca
Use parameter for destination for test friendliness
TadCordle 2d4a127
Add test for isGzipped and fix isGzipped
TadCordle 0a819b3
Starting on tests
TadCordle 4e46e58
Tests
TadCordle e7f7f94
Cleanup
TadCordle 6bb7fc6
Remove unused cache stuff
TadCordle 6f4c0c0
Clarify comment
TadCordle 7276084
Feedback
TadCordle 64e6d8d
Feedback
TadCordle d4669ba
Delete docker saved tar
TadCordle 1f3362f
Delete extracted layers too
TadCordle bb65456
Nits
TadCordle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/ExtractTarStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* 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.google.cloud.tools.jib.builder.steps; | ||
|
||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.cloud.tools.jib.blob.Blob; | ||
import com.google.cloud.tools.jib.blob.BlobDescriptor; | ||
import com.google.cloud.tools.jib.blob.Blobs; | ||
import com.google.cloud.tools.jib.builder.steps.ExtractTarStep.LocalImage; | ||
import com.google.cloud.tools.jib.cache.CachedLayer; | ||
import com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate; | ||
import com.google.cloud.tools.jib.filesystem.FileOperations; | ||
import com.google.cloud.tools.jib.image.Image; | ||
import com.google.cloud.tools.jib.image.LayerCountMismatchException; | ||
import com.google.cloud.tools.jib.image.json.BadContainerConfigurationFormatException; | ||
import com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate; | ||
import com.google.cloud.tools.jib.image.json.JsonToImageTranslator; | ||
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate; | ||
import com.google.cloud.tools.jib.json.JsonTemplateMapper; | ||
import com.google.cloud.tools.jib.tar.TarExtractor; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.io.ByteStreams; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** Extracts a tar file base image. */ | ||
public class ExtractTarStep implements Callable<LocalImage> { | ||
|
||
/** Contains an {@link Image} and its layers. * */ | ||
static class LocalImage { | ||
final Image baseImage; | ||
final List<PreparedLayer> layers; | ||
|
||
LocalImage(Image baseImage, List<PreparedLayer> layers) { | ||
this.baseImage = baseImage; | ||
this.layers = layers; | ||
} | ||
} | ||
|
||
/** | ||
* Checks the first two bytes of a file to see if it has been gzipped. | ||
* | ||
* @param path the file to check | ||
* @return {@code true} if the file is gzipped, {@code false} if not | ||
* @throws IOException if reading the file fails | ||
* @see <a href="http://www.zlib.org/rfc-gzip.html#file-format">GZIP file format</a> | ||
*/ | ||
@VisibleForTesting | ||
static boolean isGzipped(Path path) throws IOException { | ||
try (InputStream inputStream = Files.newInputStream(path)) { | ||
inputStream.mark(2); | ||
TadCordle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int magic = (inputStream.read() & 0xff) | ((inputStream.read() << 8) & 0xff00); | ||
return magic == GZIPInputStream.GZIP_MAGIC; | ||
} | ||
} | ||
|
||
private final Path tarPath; | ||
private final Path destination; | ||
chanseokoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ExtractTarStep(Path tarPath, Path destination) { | ||
this.tarPath = tarPath; | ||
this.destination = destination; | ||
} | ||
|
||
@Override | ||
public LocalImage call() | ||
throws IOException, LayerCountMismatchException, BadContainerConfigurationFormatException { | ||
Files.createDirectories(destination); | ||
FileOperations.deleteDirectoryRecursiveOnExit(destination); | ||
TarExtractor.extract(tarPath, destination); | ||
|
||
InputStream manifestStream = Files.newInputStream(destination.resolve("manifest.json")); | ||
DockerManifestEntryTemplate loadManifest = | ||
new ObjectMapper() | ||
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) | ||
.readValue(manifestStream, DockerManifestEntryTemplate[].class)[0]; | ||
manifestStream.close(); | ||
ContainerConfigurationTemplate configurationTemplate = | ||
JsonTemplateMapper.readJsonFromFile( | ||
destination.resolve(loadManifest.getConfig()), ContainerConfigurationTemplate.class); | ||
|
||
List<String> layerFiles = loadManifest.getLayerFiles(); | ||
if (configurationTemplate.getLayerCount() != layerFiles.size()) { | ||
throw new LayerCountMismatchException( | ||
"Invalid base image format: manifest contains " | ||
+ layerFiles.size() | ||
+ " layers, but container configuration contains " | ||
+ configurationTemplate.getLayerCount() | ||
+ " layers"); | ||
} | ||
|
||
// Check the first layer to see if the layers are compressed already. 'docker save' output is | ||
// uncompressed, but a jib-built tar has compressed layers. | ||
boolean layersAreCompressed = | ||
layerFiles.size() > 0 && isGzipped(destination.resolve(layerFiles.get(0))); | ||
|
||
// Process layer blobs | ||
// TODO: Optimize; compressing/calculating layer digests is slow | ||
List<PreparedLayer> layers = new ArrayList<>(); | ||
V22ManifestTemplate v22Manifest = new V22ManifestTemplate(); | ||
for (int index = 0; index < layerFiles.size(); index++) { | ||
Path file = destination.resolve(layerFiles.get(index)); | ||
|
||
// Compress layers if necessary and calculate the digest/size | ||
Blob blob = layersAreCompressed ? Blobs.from(file) : Blobs.compress(Blobs.from(file)); | ||
chanseokoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BlobDescriptor blobDescriptor = blob.writeTo(ByteStreams.nullOutputStream()); | ||
TadCordle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 'manifest' contains the layer files in the same order as the diff ids in 'configuration', | ||
// so we don't need to recalculate those. | ||
// https://containers.gitbook.io/build-containers-the-hard-way/#docker-load-format | ||
CachedLayer layer = | ||
CachedLayer.builder() | ||
.setLayerBlob(blob) | ||
.setLayerDigest(blobDescriptor.getDigest()) | ||
.setLayerSize(blobDescriptor.getSize()) | ||
.setLayerDiffId(configurationTemplate.getLayerDiffId(index)) | ||
.build(); | ||
|
||
layers.add(new PreparedLayer.Builder(layer).build()); | ||
v22Manifest.addLayer(blobDescriptor.getSize(), blobDescriptor.getDigest()); | ||
} | ||
|
||
BlobDescriptor configDescriptor = | ||
Blobs.from(configurationTemplate).writeTo(ByteStreams.nullOutputStream()); | ||
v22Manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest()); | ||
Image image = JsonToImageTranslator.toImage(v22Manifest, configurationTemplate); | ||
return new LocalImage(image, layers); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/SaveDockerStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.google.cloud.tools.jib.builder.steps; | ||
|
||
import com.google.cloud.tools.jib.api.ImageReference; | ||
import com.google.cloud.tools.jib.configuration.BuildConfiguration; | ||
import com.google.cloud.tools.jib.docker.DockerClient; | ||
import com.google.cloud.tools.jib.filesystem.FileOperations; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.Callable; | ||
|
||
/** Saves an image from the docker daemon. */ | ||
public class SaveDockerStep implements Callable<Path> { | ||
|
||
private final BuildConfiguration buildConfiguration; | ||
private final DockerClient dockerClient; | ||
|
||
SaveDockerStep(BuildConfiguration buildConfiguration, DockerClient dockerClient) { | ||
this.buildConfiguration = buildConfiguration; | ||
this.dockerClient = dockerClient; | ||
} | ||
|
||
@Override | ||
public Path call() throws IOException, InterruptedException { | ||
Path outputDir = Files.createTempDirectory("jib-docker-save"); | ||
FileOperations.deleteDirectoryRecursiveOnExit(outputDir); | ||
Path outputPath = outputDir.resolve("out.tar"); | ||
ImageReference imageReference = buildConfiguration.getBaseImageConfiguration().getImage(); | ||
dockerClient.save(imageReference, outputPath); | ||
return outputPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this method was moved from
CacheTest
.