use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by GoogleContainerTools.
the class ImageTarball method dockerWriteTo.
private void dockerWriteTo(OutputStream out) throws IOException {
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();
// Adds all the layers to the tarball and manifest.
for (Layer layer : image.getLayers()) {
String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;
tarStreamBuilder.addBlobEntry(layer.getBlob(), layer.getBlobDescriptor().getSize(), layerName, TAR_ENTRY_MODIFICATION_TIME);
manifestTemplate.addLayerFile(layerName);
}
// Adds the container configuration to the tarball.
JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), CONTAINER_CONFIGURATION_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
// Adds the manifest to tarball.
for (String tag : allTargetImageTags) {
manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
}
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)), MANIFEST_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
tarStreamBuilder.writeAsTarArchiveTo(out);
}
use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by GoogleContainerTools.
the class ImageTarballTest method testWriteTo_docker.
@Test
public void testWriteTo_docker() throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
Image testImage = Image.builder(V22ManifestTemplate.class).addLayer(mockLayer1).addLayer(mockLayer2).build();
ImageTarball imageTarball = new ImageTarball(testImage, ImageReference.parse("my/image:tag"), ImmutableSet.of("tag", "another-tag", "tag3"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
imageTarball.writeTo(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {
// Verifies layer with fileA was added.
TarArchiveEntry headerFileALayer = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals(fakeDigestA.getHash() + ".tar.gz", headerFileALayer.getName());
String fileAString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(Blobs.writeToString(Blobs.from(fileA)), fileAString);
// Verifies layer with fileB was added.
TarArchiveEntry headerFileBLayer = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals(fakeDigestB.getHash() + ".tar.gz", headerFileBLayer.getName());
String fileBString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(Blobs.writeToString(Blobs.from(fileB)), fileBString);
// Verifies container configuration was added.
TarArchiveEntry headerContainerConfiguration = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("config.json", headerContainerConfiguration.getName());
String containerConfigJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
JsonTemplateMapper.readJson(containerConfigJson, ContainerConfigurationTemplate.class);
// Verifies manifest was added.
TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("manifest.json", headerManifest.getName());
String manifestJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
DockerManifestEntryTemplate manifest = JsonTemplateMapper.readListOfJson(manifestJson, DockerManifestEntryTemplate.class).get(0);
Assert.assertEquals(ImmutableList.of("my/image:tag", "my/image:another-tag", "my/image:tag3"), manifest.getRepoTags());
}
}
use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by google.
the class LocalBaseImageSteps method cacheDockerImageTar.
@VisibleForTesting
static LocalImage cacheDockerImageTar(BuildContext buildContext, Path tarPath, ProgressEventDispatcher.Factory progressEventDispatcherFactory, TempDirectoryProvider tempDirectoryProvider) throws IOException, LayerCountMismatchException {
ExecutorService executorService = buildContext.getExecutorService();
Path destination = tempDirectoryProvider.newDirectory();
try (TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), "Extracting tar " + tarPath + " into " + 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();
Path configPath = destination.resolve(loadManifest.getConfig());
ContainerConfigurationTemplate configurationTemplate = JsonTemplateMapper.readJsonFromFile(configPath, ContainerConfigurationTemplate.class);
// Don't compute the digest of the loaded Java JSON instance.
BlobDescriptor originalConfigDescriptor = Blobs.from(configPath).writeTo(ByteStreams.nullOutputStream());
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");
}
buildContext.getBaseImageLayersCache().writeLocalConfig(originalConfigDescriptor.getDigest(), configurationTemplate);
// 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.isEmpty() && isGzipped(destination.resolve(layerFiles.get(0)));
// Process layer blobs
try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("processing base image layers", layerFiles.size())) {
// Start compressing layers in parallel
List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
for (int index = 0; index < layerFiles.size(); index++) {
Path layerFile = destination.resolve(layerFiles.get(index));
DescriptorDigest diffId = configurationTemplate.getLayerDiffId(index);
ProgressEventDispatcher.Factory layerProgressDispatcherFactory = progressEventDispatcher.newChildProducer();
preparedLayers.add(executorService.submit(() -> compressAndCacheTarLayer(buildContext.getBaseImageLayersCache(), diffId, layerFile, layersAreCompressed, layerProgressDispatcherFactory)));
}
return new LocalImage(preparedLayers, configurationTemplate);
}
}
}
use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by google.
the class ImageTarballTest method testWriteTo_docker.
@Test
public void testWriteTo_docker() throws InvalidImageReferenceException, IOException, LayerPropertyNotFoundException {
Image testImage = Image.builder(V22ManifestTemplate.class).addLayer(mockLayer1).addLayer(mockLayer2).build();
ImageTarball imageTarball = new ImageTarball(testImage, ImageReference.parse("my/image:tag"), ImmutableSet.of("tag", "another-tag", "tag3"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
imageTarball.writeTo(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(in)) {
// Verifies layer with fileA was added.
TarArchiveEntry headerFileALayer = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals(fakeDigestA.getHash() + ".tar.gz", headerFileALayer.getName());
String fileAString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(Blobs.writeToString(Blobs.from(fileA)), fileAString);
// Verifies layer with fileB was added.
TarArchiveEntry headerFileBLayer = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals(fakeDigestB.getHash() + ".tar.gz", headerFileBLayer.getName());
String fileBString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(Blobs.writeToString(Blobs.from(fileB)), fileBString);
// Verifies container configuration was added.
TarArchiveEntry headerContainerConfiguration = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("config.json", headerContainerConfiguration.getName());
String containerConfigJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
JsonTemplateMapper.readJson(containerConfigJson, ContainerConfigurationTemplate.class);
// Verifies manifest was added.
TarArchiveEntry headerManifest = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("manifest.json", headerManifest.getName());
String manifestJson = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
DockerManifestEntryTemplate manifest = JsonTemplateMapper.readListOfJson(manifestJson, DockerManifestEntryTemplate.class).get(0);
Assert.assertEquals(ImmutableList.of("my/image:tag", "my/image:another-tag", "my/image:tag3"), manifest.getRepoTags());
}
}
use of com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate in project jib by google.
the class ImageTarball method dockerWriteTo.
private void dockerWriteTo(OutputStream out) throws IOException {
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();
// Adds all the layers to the tarball and manifest.
for (Layer layer : image.getLayers()) {
String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;
tarStreamBuilder.addBlobEntry(layer.getBlob(), layer.getBlobDescriptor().getSize(), layerName, TAR_ENTRY_MODIFICATION_TIME);
manifestTemplate.addLayerFile(layerName);
}
// Adds the container configuration to the tarball.
JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), CONTAINER_CONFIGURATION_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
// Adds the manifest to tarball.
for (String tag : allTargetImageTags) {
manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
}
tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)), MANIFEST_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
tarStreamBuilder.writeAsTarArchiveTo(out);
}
Aggregations