Skip to content

Commit 06ebd93

Browse files
authored
Merge branch 'master' into conduct
2 parents e84f581 + 318d66b commit 06ebd93

25 files changed

Lines changed: 519 additions & 144 deletions

‎jib-core/build.gradle‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id 'com.github.sherter.google-java-format' version '0.7.1'
55
id 'net.ltgt.apt' version '0.13'
66
id 'net.ltgt.errorprone' version '0.0.13'
7+
id 'biz.aQute.bnd.builder' version '4.0.0'
78
}
89

910
group 'com.google.cloud.tools'
@@ -51,6 +52,14 @@ dependencies {
5152
jar {
5253
manifest {
5354
attributes 'Implementation-Version': version
55+
56+
// OSGi metadata
57+
attributes 'Bundle-SymbolicName': 'com.google.cloud.tools.jib'
58+
attributes 'Bundle-Name': 'Jib library for building Docker and OCI images'
59+
attributes 'Bundle-Vendor': 'Google LLC'
60+
attributes 'Bundle-DocURL': 'https://github.com/GoogleContainerTools/jib'
61+
attributes 'Bundle-License': 'https://www.apache.org/licenses/LICENSE-2.0'
62+
attributes 'Export-Package': 'com.google.cloud.tools.jib.*'
5463
}
5564
}
5665

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildConfiguration.java‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.tools.jib.builder;
1818

1919
import com.google.cloud.tools.jib.configuration.CacheConfiguration;
20+
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
2021
import com.google.cloud.tools.jib.image.ImageReference;
2122
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
2223
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
@@ -52,6 +53,7 @@ public static class Builder {
5253
@Nullable private CacheConfiguration applicationLayersCacheConfiguration;
5354
@Nullable private CacheConfiguration baseImageLayersCacheConfiguration;
5455
private boolean allowHttp = false;
56+
@Nullable private LayerConfiguration extraFilesLayerConfiguration;
5557

5658
private BuildLogger buildLogger;
5759

@@ -169,6 +171,18 @@ public Builder setAllowHttp(boolean allowHttp) {
169171
return this;
170172
}
171173

174+
/**
175+
* Sets the {@link LayerConfiguration} for an extra layer.
176+
*
177+
* @param extraFilesLayerConfiguration the layer configuration for the extra layer
178+
* @return this
179+
*/
180+
public Builder setExtraFilesLayerConfiguration(
181+
@Nullable LayerConfiguration extraFilesLayerConfiguration) {
182+
this.extraFilesLayerConfiguration = extraFilesLayerConfiguration;
183+
return this;
184+
}
185+
172186
/** @return the corresponding build configuration */
173187
public BuildConfiguration build() {
174188
// Validates the parameters.
@@ -210,7 +224,8 @@ public BuildConfiguration build() {
210224
targetFormat,
211225
applicationLayersCacheConfiguration,
212226
baseImageLayersCacheConfiguration,
213-
allowHttp);
227+
allowHttp,
228+
extraFilesLayerConfiguration);
214229

215230
case 1:
216231
throw new IllegalStateException(errorMessages.get(0));
@@ -269,6 +284,7 @@ public static Builder builder(BuildLogger buildLogger) {
269284
@Nullable private final CacheConfiguration applicationLayersCacheConfiguration;
270285
@Nullable private final CacheConfiguration baseImageLayersCacheConfiguration;
271286
private final boolean allowHttp;
287+
@Nullable private final LayerConfiguration extraFilesLayerConfiguration;
272288

273289
/** Instantiate with {@link Builder#build}. */
274290
private BuildConfiguration(
@@ -287,7 +303,8 @@ private BuildConfiguration(
287303
Class<? extends BuildableManifestTemplate> targetFormat,
288304
@Nullable CacheConfiguration applicationLayersCacheConfiguration,
289305
@Nullable CacheConfiguration baseImageLayersCacheConfiguration,
290-
boolean allowHttp) {
306+
boolean allowHttp,
307+
@Nullable LayerConfiguration extraFilesLayerConfiguration) {
291308
this.buildLogger = buildLogger;
292309
this.baseImageReference = baseImageReference;
293310
this.baseImageCredentialHelperName = baseImageCredentialHelperName;
@@ -304,6 +321,7 @@ private BuildConfiguration(
304321
this.applicationLayersCacheConfiguration = applicationLayersCacheConfiguration;
305322
this.baseImageLayersCacheConfiguration = baseImageLayersCacheConfiguration;
306323
this.allowHttp = allowHttp;
324+
this.extraFilesLayerConfiguration = extraFilesLayerConfiguration;
307325
}
308326

309327
public BuildLogger getBuildLogger() {
@@ -414,4 +432,14 @@ public CacheConfiguration getBaseImageLayersCacheConfiguration() {
414432
public boolean getAllowHttp() {
415433
return allowHttp;
416434
}
435+
436+
/**
437+
* Gets the {@link LayerConfiguration} for an extra layer.
438+
*
439+
* @return the layer configuration
440+
*/
441+
@Nullable
442+
public LayerConfiguration getExtraFilesLayerConfiguration() {
443+
return extraFilesLayerConfiguration;
444+
}
417445
}

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildAndCacheApplicationLayerStep.java‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import com.google.cloud.tools.jib.cache.CacheReader;
2626
import com.google.cloud.tools.jib.cache.CacheWriter;
2727
import com.google.cloud.tools.jib.cache.CachedLayerWithMetadata;
28+
import com.google.cloud.tools.jib.image.LayerEntry;
2829
import com.google.cloud.tools.jib.image.ReproducibleLayerBuilder;
2930
import com.google.common.collect.ImmutableList;
3031
import com.google.common.util.concurrent.ListenableFuture;
3132
import com.google.common.util.concurrent.ListeningExecutorService;
3233
import java.io.IOException;
33-
import java.nio.file.Path;
3434
import java.util.concurrent.Callable;
3535

3636
/** Builds and caches application layers. */
@@ -54,30 +54,35 @@ static ImmutableList<BuildAndCacheApplicationLayerStep> makeList(
5454
"dependencies",
5555
listeningExecutorService,
5656
buildConfiguration,
57-
sourceFilesConfiguration.getDependenciesFiles(),
58-
sourceFilesConfiguration.getDependenciesPathOnImage(),
57+
ImmutableList.of(
58+
new LayerEntry(
59+
sourceFilesConfiguration.getDependenciesFiles(),
60+
sourceFilesConfiguration.getDependenciesPathOnImage())),
5961
cache),
6062
new BuildAndCacheApplicationLayerStep(
6163
"resources",
6264
listeningExecutorService,
6365
buildConfiguration,
64-
sourceFilesConfiguration.getResourcesFiles(),
65-
sourceFilesConfiguration.getResourcesPathOnImage(),
66+
ImmutableList.of(
67+
new LayerEntry(
68+
sourceFilesConfiguration.getResourcesFiles(),
69+
sourceFilesConfiguration.getResourcesPathOnImage())),
6670
cache),
6771
new BuildAndCacheApplicationLayerStep(
6872
"classes",
6973
listeningExecutorService,
7074
buildConfiguration,
71-
sourceFilesConfiguration.getClassesFiles(),
72-
sourceFilesConfiguration.getClassesPathOnImage(),
75+
ImmutableList.of(
76+
new LayerEntry(
77+
sourceFilesConfiguration.getClassesFiles(),
78+
sourceFilesConfiguration.getClassesPathOnImage())),
7379
cache));
7480
}
7581
}
7682

7783
private final String layerType;
7884
private final BuildConfiguration buildConfiguration;
79-
private final ImmutableList<Path> sourceFiles;
80-
private final String extractionPath;
85+
private final ImmutableList<LayerEntry> layerEntries;
8186
private final Cache cache;
8287

8388
private final ListenableFuture<CachedLayerWithMetadata> listenableFuture;
@@ -86,13 +91,11 @@ private BuildAndCacheApplicationLayerStep(
8691
String layerType,
8792
ListeningExecutorService listeningExecutorService,
8893
BuildConfiguration buildConfiguration,
89-
ImmutableList<Path> sourceFiles,
90-
String extractionPath,
94+
ImmutableList<LayerEntry> layerEntries,
9195
Cache cache) {
9296
this.layerType = layerType;
9397
this.buildConfiguration = buildConfiguration;
94-
this.sourceFiles = sourceFiles;
95-
this.extractionPath = extractionPath;
98+
this.layerEntries = layerEntries;
9699
this.cache = cache;
97100

98101
listenableFuture = listeningExecutorService.submit(this);
@@ -112,13 +115,16 @@ public CachedLayerWithMetadata call() throws IOException, CacheMetadataCorrupted
112115
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), description)) {
113116
// Don't build the layer if it exists already.
114117
CachedLayerWithMetadata cachedLayer =
115-
new CacheReader(cache).getUpToDateLayerBySourceFiles(sourceFiles);
118+
new CacheReader(cache).getUpToDateLayerByLayerEntries(layerEntries);
116119
if (cachedLayer != null) {
117120
return cachedLayer;
118121
}
119122

120-
ReproducibleLayerBuilder reproducibleLayerBuilder =
121-
new ReproducibleLayerBuilder().addFiles(sourceFiles, extractionPath);
123+
ReproducibleLayerBuilder reproducibleLayerBuilder = new ReproducibleLayerBuilder();
124+
for (LayerEntry layerEntry : layerEntries) {
125+
reproducibleLayerBuilder.addFiles(
126+
layerEntry.getSourceFiles(), layerEntry.getExtractionPath());
127+
}
122128

123129
cachedLayer = new CacheWriter(cache).writeLayer(reproducibleLayerBuilder);
124130

‎jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheFiles.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
/** Methods for getting static cache filename properties. */
2323
class CacheFiles {
2424

25-
static final String METADATA_FILENAME = "metadata.json";
25+
/** Increment the version prefix when the metadata format changes. */
26+
static final String METADATA_FILENAME = "metadata-v2.json";
27+
2628
private static final String LAYER_FILE_EXTENSION = ".tar.gz";
2729

2830
static Path getMetadataFile(Path cacheDirectory) {

‎jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheMetadata.java‎

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package com.google.cloud.tools.jib.cache;
1818

1919
import com.google.cloud.tools.jib.image.ImageLayers;
20+
import com.google.cloud.tools.jib.image.LayerEntry;
2021
import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException;
22+
import com.google.common.annotations.VisibleForTesting;
2123
import com.google.common.collect.ImmutableList;
22-
import java.nio.file.Path;
24+
import com.google.common.collect.Streams;
2325
import java.nio.file.Paths;
24-
import java.util.ArrayList;
25-
import java.util.List;
2626
import javax.annotation.Nullable;
2727

2828
/**
@@ -60,17 +60,56 @@ CacheMetadata build() {
6060
/** Can be used to filter layers in the metadata. */
6161
static class LayerFilter {
6262

63+
/**
64+
* Checks if the layer entries matches the metadata layer entries
65+
*
66+
* @param layerEntries the layer entries to check
67+
* @param metadataEntries the metadata entries to match against
68+
* @return {@code true} if the layer entries match the metadata entries; {@code false} otherwise
69+
*/
70+
@VisibleForTesting
71+
static boolean doLayerEntriesMatchMetadataEntries(
72+
ImmutableList<LayerEntry> layerEntries,
73+
ImmutableList<LayerMetadata.LayerMetadataEntry> metadataEntries) {
74+
// Checks the layer entries are the same as the metadata layer entries.
75+
if (layerEntries.size() != metadataEntries.size()) {
76+
return false;
77+
}
78+
return Streams.zip(
79+
layerEntries.stream(),
80+
metadataEntries.stream(),
81+
(layerEntry, metadataEntry) -> {
82+
// Checks extraction path not equal.
83+
if (!layerEntry.getExtractionPath().equals(metadataEntry.getExtractionPath())) {
84+
return true;
85+
}
86+
87+
// Checks source files not equal.
88+
if (layerEntry.getSourceFiles().size()
89+
!= metadataEntry.getSourceFilesStrings().size()) {
90+
return true;
91+
}
92+
return Streams.zip(
93+
layerEntry.getSourceFiles().stream(),
94+
metadataEntry.getSourceFilesStrings().stream(),
95+
(sourceFile, sourceFileString) ->
96+
!sourceFile.equals(Paths.get(sourceFileString)))
97+
.anyMatch(isNotEqual -> isNotEqual);
98+
})
99+
.noneMatch(isNotEqual -> isNotEqual);
100+
}
101+
63102
private final ImageLayers<CachedLayerWithMetadata> layers;
64103

65-
@Nullable private ImmutableList<Path> sourceFiles;
104+
@Nullable private ImmutableList<LayerEntry> layerEntries;
66105

67106
private LayerFilter(ImageLayers<CachedLayerWithMetadata> layers) {
68107
this.layers = layers;
69108
}
70109

71-
/** Filters to a certain list of source files. */
72-
LayerFilter bySourceFiles(ImmutableList<Path> sourceFiles) {
73-
this.sourceFiles = sourceFiles;
110+
/** Filters to a certain list of {@link LayerEntry}s. */
111+
LayerFilter byLayerEntries(ImmutableList<LayerEntry> layerEntries) {
112+
this.layerEntries = layerEntries;
74113
return this;
75114
}
76115

@@ -80,19 +119,16 @@ ImageLayers<CachedLayerWithMetadata> filter() throws CacheMetadataCorruptedExcep
80119
ImageLayers.Builder<CachedLayerWithMetadata> filteredLayersBuilder = ImageLayers.builder();
81120

82121
for (CachedLayerWithMetadata layer : layers) {
83-
if (sourceFiles != null) {
122+
if (layerEntries != null) {
84123
if (layer.getMetadata() == null) {
124+
// There is no metadata, so it doesn't pass the filter.
85125
continue;
86126
}
87-
List<String> cachedLayerSourceFilePaths = layer.getMetadata().getSourceFiles();
88-
if (cachedLayerSourceFilePaths != null) {
89-
List<Path> cachedLayerSourceFiles = new ArrayList<>();
90-
for (String sourceFile : cachedLayerSourceFilePaths) {
91-
cachedLayerSourceFiles.add(Paths.get(sourceFile));
92-
}
93-
if (!cachedLayerSourceFiles.equals(sourceFiles)) {
94-
continue;
95-
}
127+
128+
if (!doLayerEntriesMatchMetadataEntries(
129+
layerEntries, layer.getMetadata().getEntries())) {
130+
// The layer entries do not match.
131+
continue;
96132
}
97133
}
98134

0 commit comments

Comments
 (0)