Package com.google.cloud.tools.jib.cache
Class Cache
- java.lang.Object
-
- com.google.cloud.tools.jib.cache.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
areAllLayersCached(ManifestTemplate manifest)
Returnstrue
if all image layers described in a manifest exist in the cache.java.util.Optional<CachedLayer>
retrieve(DescriptorDigest layerDigest)
Retrieves theCachedLayer
for the layer with digestlayerDigest
.java.util.Optional<CachedLayer>
retrieve(com.google.common.collect.ImmutableList<com.google.cloud.tools.jib.api.buildplan.FileEntry> layerEntries)
Retrieves theCachedLayer
that was built from thelayerEntries
.java.util.Optional<ContainerConfigurationTemplate>
retrieveLocalConfig(DescriptorDigest imageId)
Retrieves theContainerConfigurationTemplate
for the image saved from the given image ID.java.util.Optional<ImageMetadataTemplate>
retrieveMetadata(ImageReference imageReference)
Retrieves the cached image metadata (a manifest list and a list of manifest/container configuration pairs) for an image reference.java.util.Optional<CachedLayer>
retrieveTarLayer(DescriptorDigest diffId)
Retrieves aCachedLayer
for a local base image layer with the given diff id.static Cache
withDirectory(java.nio.file.Path cacheDirectory)
Initializes the cache usingcacheDirectory
for storage.CachedLayer
writeCompressedLayer(Blob compressedLayerBlob)
Saves a cache entry with a compressed layerBlob
.void
writeLocalConfig(DescriptorDigest imageId, ContainerConfigurationTemplate containerConfiguration)
Writes a container configuration to(cache directory)/local/config/(image id)
.void
writeMetadata(ImageReference imageReference, BuildableManifestTemplate manifest, ContainerConfigurationTemplate containerConfiguration)
Saves a schema 2 manifest for an image reference.void
writeMetadata(ImageReference imageReference, ImageMetadataTemplate metadata)
Saves image metadata (a manifest list and a list of manifest/container configuration pairs) for an image reference.void
writeMetadata(ImageReference imageReference, V21ManifestTemplate manifestTemplate)
Saves a V2.1 image manifest.CachedLayer
writeTarLayer(DescriptorDigest diffId, Blob compressedBlob)
Caches a layer that was extracted from a local base image, and names the file using the provided diff id.CachedLayer
writeUncompressedLayer(Blob uncompressedLayerBlob, com.google.common.collect.ImmutableList<com.google.cloud.tools.jib.api.buildplan.FileEntry> layerEntries)
Saves a cache entry with an uncompressed layerBlob
and an additional selector digest.
-
-
-
Method Detail
-
withDirectory
public static Cache withDirectory(java.nio.file.Path cacheDirectory) throws CacheDirectoryCreationException
Initializes the cache usingcacheDirectory
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 formetadata
- the image metadata- Throws:
java.io.IOException
- if an I/O exception occurs
-
writeMetadata
public void writeMetadata(ImageReference imageReference, BuildableManifestTemplate manifest, ContainerConfigurationTemplate containerConfiguration) throws java.io.IOException
Saves a schema 2 manifest for an image reference. This is a simple wrapper aroundwriteMetadata(ImageReference, ImageMetadataTemplate)
to save a single manifest without a manifest list.- Parameters:
imageReference
- the image reference to save the manifest formanifest
- the V2.2 or OCI manifestcontainerConfiguration
- the container configuration- 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 aroundwriteMetadata(ImageReference, ImageMetadataTemplate)
to save a single manifest without a manifest list.- Parameters:
imageReference
- the image reference to save the manifest formanifestTemplate
- 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 layerBlob
. UsewriteUncompressedLayer(Blob, ImmutableList)
to save a cache entry with an uncompressed layerBlob
and include a selector.- Parameters:
compressedLayerBlob
- the compressed layerBlob
- 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 layerBlob
and an additional selector digest. UsewriteCompressedLayer(Blob)
to save a compressed layerBlob
.- Parameters:
uncompressedLayerBlob
- the layerBlob
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 idcompressedBlob
- 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 indocker images
.Note: the
imageId
to thecontainerConfiguration
is a one-way relationship; there is no guarantee thatcontainerConfiguration
's SHA will beimageId
, 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 forcontainerConfiguration
- 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 occursCacheCorruptedException
- if the cache is corrupted
-
areAllLayersCached
public boolean areAllLayersCached(ManifestTemplate manifest)
Returnstrue
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 theCachedLayer
that was built from thelayerEntries
.- Parameters:
layerEntries
- the layer entries to match against- Returns:
- a
CachedLayer
that was built fromlayerEntries
, if found - Throws:
java.io.IOException
- if an I/O exception occursCacheCorruptedException
- if the cache is corrupted
-
retrieve
public java.util.Optional<CachedLayer> retrieve(DescriptorDigest layerDigest) throws java.io.IOException, CacheCorruptedException
Retrieves theCachedLayer
for the layer with digestlayerDigest
.- Parameters:
layerDigest
- the layer digest- Returns:
- the
CachedLayer
referenced by the layer digest, if found - Throws:
CacheCorruptedException
- if the cache was found to be corruptedjava.io.IOException
- if an I/O exception occurs
-
retrieveTarLayer
public java.util.Optional<CachedLayer> retrieveTarLayer(DescriptorDigest diffId) throws java.io.IOException, CacheCorruptedException
Retrieves aCachedLayer
for a local base image layer with the given diff id.- Parameters:
diffId
- the diff id- Returns:
- the
CachedLayer
with the given diff id - Throws:
CacheCorruptedException
- if the cache was found to be corruptedjava.io.IOException
- if an I/O exception occurs
-
retrieveLocalConfig
public java.util.Optional<ContainerConfigurationTemplate> retrieveLocalConfig(DescriptorDigest imageId) throws java.io.IOException
Retrieves theContainerConfigurationTemplate
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 indocker images
.Note: the
imageId
is only used to find thecontainerConfiguration
, and is not necessarily the actual SHA ofcontainerConfiguration
. There is no guarantee thatcontainerConfiguration
's SHA will beimageId
, 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
-
-