Search in sources :

Example 51 with ImageMetadataTemplate

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

the class CacheStorageReaderTest method setupCachedMetadataV22.

private static void setupCachedMetadataV22(Path cacheDirectory) throws IOException, URISyntaxException {
    Path imageDirectory = cacheDirectory.resolve("images/test/image!tag");
    Files.createDirectories(imageDirectory);
    ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(loadJsonResource("core/json/v22manifest.json", V22ManifestTemplate.class), loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class), "sha256:digest");
    try (OutputStream out = Files.newOutputStream(imageDirectory.resolve("manifests_configs.json"))) {
        JsonTemplateMapper.writeTo(new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig)), out);
    }
}
Also used : Path(java.nio.file.Path) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) OutputStream(java.io.OutputStream) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)

Example 52 with ImageMetadataTemplate

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

the class PullBaseImageStep method pullBaseImages.

/**
 * Pulls the base images specified in the platforms list.
 *
 * @param registryClient to communicate with remote registry
 * @param progressDispatcherFactory the {@link ProgressEventDispatcher.Factory} for emitting
 *     {@link ProgressEvent}s
 * @return the list of pulled base images and a registry client
 * @throws IOException when an I/O exception occurs during the pulling
 * @throws RegistryException if communicating with the registry caused a known error
 * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
 *     information
 * @throws LayerPropertyNotFoundException if adding image layers fails
 * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
 *     format
 */
private List<Image> pullBaseImages(RegistryClient registryClient, ProgressEventDispatcher.Factory progressDispatcherFactory) throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException {
    Cache cache = buildContext.getBaseImageLayersCache();
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    ImageConfiguration baseImageConfig = buildContext.getBaseImageConfiguration();
    try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("pulling base image manifest and container config", 2)) {
        ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest(baseImageConfig.getImageQualifier());
        eventHandlers.dispatch(LogEvent.lifecycle("Using base image with digest: " + manifestAndDigest.getDigest()));
        progressDispatcher1.dispatchProgress(1);
        ProgressEventDispatcher.Factory childProgressDispatcherFactory = progressDispatcher1.newChildProducer();
        ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
        if (manifestTemplate instanceof V21ManifestTemplate) {
            V21ManifestTemplate v21Manifest = (V21ManifestTemplate) manifestTemplate;
            cache.writeMetadata(baseImageConfig.getImage(), v21Manifest);
            return Collections.singletonList(JsonToImageTranslator.toImage(v21Manifest));
        } else if (manifestTemplate instanceof BuildableManifestTemplate) {
            // V22ManifestTemplate or OciManifestTemplate
            BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) manifestTemplate;
            ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(manifestAndDigest, registryClient, childProgressDispatcherFactory);
            PlatformChecker.checkManifestPlatform(buildContext, containerConfig);
            cache.writeMetadata(baseImageConfig.getImage(), imageManifest, containerConfig);
            return Collections.singletonList(JsonToImageTranslator.toImage(imageManifest, containerConfig));
        }
        // TODO: support OciIndexTemplate once AbstractManifestPuller starts to accept it.
        Verify.verify(manifestTemplate instanceof V22ManifestListTemplate);
        List<ManifestAndConfigTemplate> manifestsAndConfigs = new ArrayList<>();
        ImmutableList.Builder<Image> images = ImmutableList.builder();
        Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
        try (ProgressEventDispatcher progressDispatcher2 = childProgressDispatcherFactory.create("pulling platform-specific manifests and container configs", 2L * platforms.size())) {
            // If a manifest list, search for the manifests matching the given platforms.
            for (Platform platform : platforms) {
                String message = "Searching for architecture=%s, os=%s in the base image manifest list";
                eventHandlers.dispatch(LogEvent.info(String.format(message, platform.getArchitecture(), platform.getOs())));
                String manifestDigest = lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestTemplate, platform);
                // TODO: pull multiple manifests (+ container configs) in parallel.
                ManifestAndDigest<?> imageManifestAndDigest = registryClient.pullManifest(manifestDigest);
                progressDispatcher2.dispatchProgress(1);
                BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) imageManifestAndDigest.getManifest();
                ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(imageManifestAndDigest, registryClient, progressDispatcher2.newChildProducer());
                manifestsAndConfigs.add(new ManifestAndConfigTemplate(imageManifest, containerConfig, manifestDigest));
                images.add(JsonToImageTranslator.toImage(imageManifest, containerConfig));
            }
        }
        cache.writeMetadata(baseImageConfig.getImage(), new ImageMetadataTemplate(manifestTemplate, /* manifest list */
        manifestsAndConfigs));
        return images.build();
    }
}
Also used : ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) 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) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 53 with ImageMetadataTemplate

use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate 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 54 with ImageMetadataTemplate

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

the class CacheStorageReader method retrieveMetadata.

/**
 * Retrieves the cached image metadata (a manifest list and a list of manifest/container
 * configuration pairs) for an image reference.
 *
 * @param imageReference the image reference
 * @return the image metadata for the image reference, if found
 * @throws IOException if an I/O exception occurs
 * @throws CacheCorruptedException if the cache is corrupted
 */
Optional<ImageMetadataTemplate> retrieveMetadata(ImageReference imageReference) throws IOException, CacheCorruptedException {
    Path imageDirectory = cacheStorageFiles.getImageDirectory(imageReference);
    Path metadataPath = imageDirectory.resolve("manifests_configs.json");
    if (!Files.exists(metadataPath)) {
        return Optional.empty();
    }
    ImageMetadataTemplate metadata;
    try (LockFile ignored = LockFile.lock(imageDirectory.resolve("lock"))) {
        metadata = JsonTemplateMapper.readJsonFromFile(metadataPath, ImageMetadataTemplate.class);
    }
    verifyImageMetadata(metadata, imageDirectory);
    return Optional.of(metadata);
}
Also used : Path(java.nio.file.Path) LockFile(com.google.cloud.tools.jib.filesystem.LockFile) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)

Example 55 with ImageMetadataTemplate

use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate 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)

Aggregations

ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)82 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)70 Test (org.junit.Test)58 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)52 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)34 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)28 ImageReference (com.google.cloud.tools.jib.api.ImageReference)26 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)22 Path (java.nio.file.Path)22 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)18 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