Search in sources :

Example 46 with ManifestAndConfigTemplate

use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.

the class Cache method writeMetadata.

/**
 * Saves a schema 2 manifest for an image reference. This is a simple wrapper around {@link
 * #writeMetadata(ImageReference, ImageMetadataTemplate)} to save a single manifest without a
 * manifest list.
 *
 * @param imageReference the image reference to save the manifest for
 * @param manifest the V2.2 or OCI manifest
 * @param containerConfiguration the container configuration
 * @throws IOException if an I/O exception occurs
 */
public void writeMetadata(ImageReference imageReference, BuildableManifestTemplate manifest, ContainerConfigurationTemplate containerConfiguration) throws IOException {
    List<ManifestAndConfigTemplate> singleton = Collections.singletonList(new ManifestAndConfigTemplate(manifest, containerConfiguration));
    cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(null, singleton));
}
Also used : ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)

Example 47 with ManifestAndConfigTemplate

use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.

the class CacheStorageReader method verifyImageMetadata.

@VisibleForTesting
static void verifyImageMetadata(ImageMetadataTemplate metadata, Path metadataCacheDirectory) throws CacheCorruptedException {
    List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.getManifestsAndConfigs();
    if (manifestsAndConfigs.isEmpty()) {
        throw new CacheCorruptedException(metadataCacheDirectory, "Manifest cache empty");
    }
    if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifest() == null)) {
        throw new CacheCorruptedException(metadataCacheDirectory, "Manifest(s) missing");
    }
    if (metadata.getManifestList() == null && manifestsAndConfigs.size() != 1) {
        throw new CacheCorruptedException(metadataCacheDirectory, "Manifest list missing");
    }
    ManifestTemplate firstManifest = manifestsAndConfigs.get(0).getManifest();
    if (firstManifest instanceof V21ManifestTemplate) {
        if (metadata.getManifestList() != null || manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() != null)) {
            throw new CacheCorruptedException(metadataCacheDirectory, "Schema 1 manifests corrupted");
        }
    } else if (firstManifest instanceof BuildableManifestTemplate) {
        if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() == null)) {
            throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
        }
        if (metadata.getManifestList() != null && manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifestDigest() == null)) {
            throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
        }
    } else {
        throw new CacheCorruptedException(metadataCacheDirectory, "Unknown manifest type: " + firstManifest);
    }
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Files(java.nio.file.Files) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) JsonTemplateMapper(com.google.cloud.tools.jib.json.JsonTemplateMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) List(java.util.List) Stream(java.util.stream.Stream) LockFile(com.google.cloud.tools.jib.filesystem.LockFile) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) DigestException(java.security.DigestException) Optional(java.util.Optional) Blobs(com.google.cloud.tools.jib.blob.Blobs) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 48 with ManifestAndConfigTemplate

use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.

the class CacheStorageWriter method verifyImageMetadata.

private static void verifyImageMetadata(ImageMetadataTemplate metadata) {
    Predicate<ManifestAndConfigTemplate> isManifestNull = pair -> pair.getManifest() == null;
    Predicate<ManifestAndConfigTemplate> isConfigNull = pair -> pair.getConfig() == null;
    Predicate<ManifestAndConfigTemplate> isDigestNull = pair -> pair.getManifestDigest() == null;
    List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.getManifestsAndConfigs();
    Preconditions.checkArgument(!manifestsAndConfigs.isEmpty(), "no manifests given");
    Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isManifestNull), "null manifest(s)");
    Preconditions.checkArgument(metadata.getManifestList() != null || manifestsAndConfigs.size() == 1, "manifest list missing while multiple manifests given");
    ManifestTemplate firstManifest = manifestsAndConfigs.get(0).getManifest();
    if (firstManifest instanceof V21ManifestTemplate) {
        Preconditions.checkArgument(metadata.getManifestList() == null, "manifest list given for schema 1");
        Preconditions.checkArgument(isConfigNull.test(manifestsAndConfigs.get(0)), "container config given for schema 1");
    } else if (firstManifest instanceof BuildableManifestTemplate) {
        Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isConfigNull), "null container config(s)");
        if (metadata.getManifestList() != null) {
            Preconditions.checkArgument(manifestsAndConfigs.stream().noneMatch(isDigestNull), "null manifest digest(s)");
        }
    } else {
        throw new IllegalArgumentException("Unknown manifest type: " + firstManifest);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) BufferedOutputStream(java.io.BufferedOutputStream) StandardCopyOption(java.nio.file.StandardCopyOption) Path(java.nio.file.Path) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Files(java.nio.file.Files) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) Predicate(java.util.function.Predicate) FileSystemException(java.nio.file.FileSystemException) Action(com.google.cloud.tools.jib.cache.Retry.Action) JsonTemplateMapper(com.google.cloud.tools.jib.json.JsonTemplateMapper) IOException(java.io.IOException) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) StandardCharsets(java.nio.charset.StandardCharsets) Digests(com.google.cloud.tools.jib.hash.Digests) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LockFile(com.google.cloud.tools.jib.filesystem.LockFile) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) Blobs(com.google.cloud.tools.jib.blob.Blobs) Preconditions(com.google.common.base.Preconditions) GZIPOutputStream(java.util.zip.GZIPOutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InputStream(java.io.InputStream) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate)

Example 49 with ManifestAndConfigTemplate

use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.

the class PullBaseImageStepTest method testGetCachedBaseImages_v21ManifestCached.

@Test
public void testGetCachedBaseImages_v21ManifestCached() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException, DigestException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
    DescriptorDigest layerDigest = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
    V21ManifestTemplate v21Manifest = Mockito.mock(V21ManifestTemplate.class);
    Mockito.when(v21Manifest.getLayerDigests()).thenReturn(Arrays.asList(layerDigest));
    ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(null, Arrays.asList(new ManifestAndConfigTemplate(v21Manifest, null)));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    List<Image> images = pullBaseImageStep.getCachedBaseImages();
    Assert.assertEquals(1, images.size());
    Assert.assertEquals(1, images.get(0).getLayers().size());
    Assert.assertEquals("1111111111111111111111111111111111111111111111111111111111111111", images.get(0).getLayers().get(0).getBlobDescriptor().getDigest().getHash());
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Test(org.junit.Test)

Example 50 with ManifestAndConfigTemplate

use of com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate in project jib by google.

the class PullBaseImageStepTest method testGetCachedBaseImages_v22ManifestListCached_partialMatches.

@Test
public void testGetCachedBaseImages_v22ManifestListCached_partialMatches() throws InvalidImageReferenceException, IOException, CacheCorruptedException, UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException, LayerCountMismatchException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration()).thenReturn(ImageConfiguration.builder(imageReference).build());
    V22ManifestListTemplate manifestList = Mockito.mock(V22ManifestListTemplate.class);
    Mockito.when(manifestList.getDigestsForPlatform("arch1", "os1")).thenReturn(Arrays.asList("sha256:digest1"));
    Mockito.when(manifestList.getDigestsForPlatform("arch2", "os2")).thenReturn(Arrays.asList("sha256:digest2"));
    ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(manifestList, Arrays.asList(new ManifestAndConfigTemplate(new V22ManifestTemplate(), new ContainerConfigurationTemplate(), "sha256:digest1")));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(containerConfig.getPlatforms()).thenReturn(ImmutableSet.of(new Platform("arch1", "os1"), new Platform("arch2", "os2")));
    Assert.assertEquals(Arrays.asList(), pullBaseImageStep.getCachedBaseImages());
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Test(org.junit.Test)

Aggregations

ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)70 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)70 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)50 Test (org.junit.Test)48 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)32 ImageReference (com.google.cloud.tools.jib.api.ImageReference)26 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)26 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)22 Path (java.nio.file.Path)20 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)16 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)14 Image (com.google.cloud.tools.jib.image.Image)12 OutputStream (java.io.OutputStream)12 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)10 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)8 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)6 Blobs (com.google.cloud.tools.jib.blob.Blobs)6 ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)6 OciIndexTemplate (com.google.cloud.tools.jib.image.json.OciIndexTemplate)6 JsonTemplateMapper (com.google.cloud.tools.jib.json.JsonTemplateMapper)6