Search in sources :

Example 1 with CacheReader

use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.

the class BuildAndCacheApplicationLayersStepTest method testRun.

@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, URISyntaxException, ExecutionException, InterruptedException {
    Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(new TestBuildLogger());
    TestSourceFilesConfiguration testSourceFilesConfiguration = new TestSourceFilesConfiguration();
    Path temporaryCacheDirectory = temporaryFolder.newFolder().toPath();
    ImageLayers<CachedLayer> applicationLayers = new ImageLayers<>();
    try (Cache cache = Cache.init(temporaryCacheDirectory)) {
        BuildAndCacheApplicationLayersStep buildAndCacheApplicationLayersStep = new BuildAndCacheApplicationLayersStep(mockBuildConfiguration, testSourceFilesConfiguration, cache, MoreExecutors.newDirectExecutorService());
        for (ListenableFuture<CachedLayer> applicationLayerFuture : buildAndCacheApplicationLayersStep.call()) {
            applicationLayers.add(applicationLayerFuture.get());
        }
        Assert.assertEquals(3, applicationLayers.size());
    }
    // Re-initialize cache with the updated metadata.
    Cache cache = Cache.init(temporaryCacheDirectory);
    // Verifies that the cached layers are up-to-date.
    CacheReader cacheReader = new CacheReader(cache);
    Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getDependenciesFiles()).getBlobDescriptor());
    Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getResourcesFiles()).getBlobDescriptor());
    Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getClassesFiles()).getBlobDescriptor());
    // Verifies that the cache reader gets the same layers as the newest application layers.
    Assert.assertEquals(applicationLayers.get(0).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getDependenciesFiles()));
    Assert.assertEquals(applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getResourcesFiles()));
    Assert.assertEquals(applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getClassesFiles()));
}
Also used : Path(java.nio.file.Path) ImageLayers(com.google.cloud.tools.jib.image.ImageLayers) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) CacheReader(com.google.cloud.tools.jib.cache.CacheReader) Cache(com.google.cloud.tools.jib.cache.Cache) Test(org.junit.Test)

Example 2 with CacheReader

use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.

the class PullAndCacheBaseImageLayerStep method call.

/**
 * Depends on {@code pullAuthorizationFuture}.
 */
@Override
public CachedLayer call() throws IOException, RegistryException, LayerPropertyNotFoundException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
        RegistryClient registryClient = new RegistryClient(pullAuthorizationFuture.get(), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        // Checks if the layer already exists in the cache.
        CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
        if (cachedLayer != null) {
            return cachedLayer;
        }
        CacheWriter cacheWriter = new CacheWriter(cache);
        CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(layerDigest);
        registryClient.pullBlob(layerDigest, layerOutputStream);
        return cacheWriter.getCachedLayer(layerDigest, layerOutputStream);
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) Timer(com.google.cloud.tools.jib.Timer) CacheWriter(com.google.cloud.tools.jib.cache.CacheWriter) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) CacheReader(com.google.cloud.tools.jib.cache.CacheReader)

Example 3 with CacheReader

use of com.google.cloud.tools.jib.cache.CacheReader in project jib by google.

the class BuildAndCacheApplicationLayersStep method buildAndCacheLayerAsync.

private ListenableFuture<CachedLayer> buildAndCacheLayerAsync(String layerType, List<Path> sourceFiles, String extractionPath) {
    String description = "Building " + layerType + " layer";
    return listeningExecutorService.submit(() -> {
        try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), description)) {
            // Don't build the layer if it exists already.
            CachedLayer cachedLayer = new CacheReader(cache).getUpToDateLayerBySourceFiles(sourceFiles);
            if (cachedLayer != null) {
                return cachedLayer;
            }
            LayerBuilder layerBuilder = new LayerBuilder(sourceFiles, extractionPath, buildConfiguration.getEnableReproducibleBuilds());
            cachedLayer = new CacheWriter(cache).writeLayer(layerBuilder);
            // TODO: Remove
            buildConfiguration.getBuildLogger().debug(description + " built " + cachedLayer.getBlobDescriptor().getDigest());
            return cachedLayer;
        }
    });
}
Also used : Timer(com.google.cloud.tools.jib.Timer) LayerBuilder(com.google.cloud.tools.jib.image.LayerBuilder) CacheWriter(com.google.cloud.tools.jib.cache.CacheWriter) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) CacheReader(com.google.cloud.tools.jib.cache.CacheReader)

Aggregations

CacheReader (com.google.cloud.tools.jib.cache.CacheReader)3 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)3 Timer (com.google.cloud.tools.jib.Timer)2 CacheWriter (com.google.cloud.tools.jib.cache.CacheWriter)2 Cache (com.google.cloud.tools.jib.cache.Cache)1 ImageLayers (com.google.cloud.tools.jib.image.ImageLayers)1 LayerBuilder (com.google.cloud.tools.jib.image.LayerBuilder)1 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)1 CountingOutputStream (com.google.common.io.CountingOutputStream)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1