Class Cache


  • @Immutable
    public class Cache
    extends java.lang.Object
    Cache for storing data to be shared between Jib executions.

    This class is immutable and safe to use across threads.

    • Method Detail

      • withDirectory

        public static Cache withDirectory​(java.nio.file.Path cacheDirectory)
                                   throws CacheDirectoryCreationException
        Initializes the cache using cacheDirectory for storage.
        Parameters:
        cacheDirectory - the directory for the cache. Creates the directory if it does not exist.
        Returns:
        a new Cache
        Throws:
        CacheDirectoryCreationException - if an I/O exception occurs when creating cache directory
      • writeMetadata

        public void writeMetadata​(ImageReference imageReference,
                                  ImageMetadataTemplate metadata)
                           throws java.io.IOException
        Saves image metadata (a manifest list and a list of manifest/container configuration pairs) for an image reference.
        Parameters:
        imageReference - the image reference to save the metadata for
        metadata - the image metadata
        Throws:
        java.io.IOException - if an I/O exception occurs
      • writeMetadata

        public void writeMetadata​(ImageReference imageReference,
                                  V21ManifestTemplate manifestTemplate)
                           throws java.io.IOException
        Saves a V2.1 image manifest. This is a simple wrapper around writeMetadata(ImageReference, ImageMetadataTemplate) to save a single manifest without a manifest list.
        Parameters:
        imageReference - the image reference to save the manifest for
        manifestTemplate - the V2.1 manifest
        Throws:
        java.io.IOException - if an I/O exception occurs
      • writeCompressedLayer

        public CachedLayer writeCompressedLayer​(Blob compressedLayerBlob)
                                         throws java.io.IOException
        Saves a cache entry with a compressed layer Blob. Use writeUncompressedLayer(Blob, ImmutableList) to save a cache entry with an uncompressed layer Blob and include a selector.
        Parameters:
        compressedLayerBlob - the compressed layer Blob
        Returns:
        the CachedLayer for the written layer
        Throws:
        java.io.IOException - if an I/O exception occurs
      • writeUncompressedLayer

        public CachedLayer writeUncompressedLayer​(Blob uncompressedLayerBlob,
                                                  com.google.common.collect.ImmutableList<com.google.cloud.tools.jib.api.buildplan.FileEntry> layerEntries)
                                           throws java.io.IOException
        Saves a cache entry with an uncompressed layer Blob and an additional selector digest. Use writeCompressedLayer(Blob) to save a compressed layer Blob.
        Parameters:
        uncompressedLayerBlob - the layer Blob
        layerEntries - the layer entries that make up the layer
        Returns:
        the CachedLayer for the written layer
        Throws:
        java.io.IOException - if an I/O exception occurs
      • writeTarLayer

        public CachedLayer writeTarLayer​(DescriptorDigest diffId,
                                         Blob compressedBlob)
                                  throws java.io.IOException
        Caches a layer that was extracted from a local base image, and names the file using the provided diff id.
        Parameters:
        diffId - the diff id
        compressedBlob - the compressed layer blob
        Returns:
        the CachedLayer for the written layer
        Throws:
        java.io.IOException - if an I/O exception occurs
      • writeLocalConfig

        public void writeLocalConfig​(DescriptorDigest imageId,
                                     ContainerConfigurationTemplate containerConfiguration)
                              throws java.io.IOException
        Writes a container configuration to (cache directory)/local/config/(image id). An image ID is a SHA hash of a container configuration JSON. The value is also shown as IMAGE ID in docker images.

        Note: the imageId to the containerConfiguration is a one-way relationship; there is no guarantee that containerConfiguration's SHA will be imageId, since the original container configuration is being rewritten here rather than being moved.

        Parameters:
        imageId - the ID of the image to store the container configuration for
        containerConfiguration - the container configuration
        Throws:
        java.io.IOException - if an I/O exception occurs
      • retrieveMetadata

        public java.util.Optional<ImageMetadataTemplate> retrieveMetadata​(ImageReference imageReference)
                                                                   throws java.io.IOException,
                                                                          CacheCorruptedException
        Retrieves the cached image metadata (a manifest list and a list of manifest/container configuration pairs) for an image reference.
        Parameters:
        imageReference - the image reference
        Returns:
        the image metadata for the image reference, if found
        Throws:
        java.io.IOException - if an I/O exception occurs
        CacheCorruptedException - if the cache is corrupted
      • areAllLayersCached

        public boolean areAllLayersCached​(ManifestTemplate manifest)
        Returns true if all image layers described in a manifest exist in the cache.
        Parameters:
        manifest - the image manifest
        Returns:
        a boolean
      • retrieve

        public java.util.Optional<CachedLayer> retrieve​(com.google.common.collect.ImmutableList<com.google.cloud.tools.jib.api.buildplan.FileEntry> layerEntries)
                                                 throws java.io.IOException,
                                                        CacheCorruptedException
        Retrieves the CachedLayer that was built from the layerEntries.
        Parameters:
        layerEntries - the layer entries to match against
        Returns:
        a CachedLayer that was built from layerEntries, if found
        Throws:
        java.io.IOException - if an I/O exception occurs
        CacheCorruptedException - if the cache is corrupted
      • retrieveLocalConfig

        public java.util.Optional<ContainerConfigurationTemplate> retrieveLocalConfig​(DescriptorDigest imageId)
                                                                               throws java.io.IOException
        Retrieves the ContainerConfigurationTemplate for the image saved from the given image ID. An image ID is a SHA hash of a container configuration JSON. The value is also shown as IMAGE ID in docker images.

        Note: the imageId is only used to find the containerConfiguration, and is not necessarily the actual SHA of containerConfiguration. There is no guarantee that containerConfiguration's SHA will be imageId, since the saved container configuration is not a direct copy of the base image's original configuration.

        Parameters:
        imageId - the image ID
        Returns:
        the ContainerConfigurationTemplate referenced by the image ID, if found
        Throws:
        java.io.IOException - if an I/O exception occurs