1
1
package io .fabric8 .maven .docker .service ;
2
2
3
- import java .io .File ;
4
- import java .io .IOException ;
5
- import java .io .Serializable ;
6
- import java .nio .file .Files ;
7
- import java .util .Collections ;
8
- import java .util .HashMap ;
9
- import java .util .LinkedList ;
10
- import java .util .List ;
11
- import java .util .Map ;
12
- import java .util .Properties ;
13
- import java .util .regex .PatternSyntaxException ;
14
-
15
- import org .apache .maven .plugin .MojoExecutionException ;
16
3
import com .google .common .collect .ImmutableMap ;
17
4
import com .google .gson .JsonObject ;
18
5
import com .google .gson .JsonParseException ;
19
-
20
6
import io .fabric8 .maven .docker .access .BuildOptions ;
21
7
import io .fabric8 .maven .docker .access .DockerAccess ;
22
8
import io .fabric8 .maven .docker .access .DockerAccessException ;
34
20
import io .fabric8 .maven .docker .util .Logger ;
35
21
import io .fabric8 .maven .docker .util .MojoParameters ;
36
22
import io .fabric8 .maven .docker .util .NamePatternUtil ;
23
+ import org .apache .maven .plugin .MojoExecutionException ;
24
+
25
+ import java .io .File ;
26
+ import java .io .IOException ;
27
+ import java .io .Serializable ;
28
+ import java .nio .file .Files ;
29
+ import java .util .Collections ;
30
+ import java .util .HashMap ;
31
+ import java .util .LinkedList ;
32
+ import java .util .List ;
33
+ import java .util .Map ;
34
+ import java .util .Properties ;
35
+ import java .util .regex .PatternSyntaxException ;
37
36
38
37
public class BuildService {
39
38
@@ -56,7 +55,7 @@ public class BuildService {
56
55
/**
57
56
* Pull the base image if needed and run the build.
58
57
*
59
- * @param imageConfig the image configuration
58
+ * @param imageConfig the image configuration
60
59
* @param buildContext the build context
61
60
* @throws DockerAccessException
62
61
* @throws MojoExecutionException
@@ -66,6 +65,7 @@ public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePul
66
65
67
66
if (imagePullManager != null ) {
68
67
autoPullBaseImage (imageConfig , imagePullManager , buildContext );
68
+ autoPullCacheFromImage (imageConfig , imagePullManager , buildContext );
69
69
}
70
70
71
71
buildImage (imageConfig , buildContext .getMojoParameters (), checkForNocache (imageConfig ), checkForSquash (imageConfig ), addBuildArgs (buildContext ), buildArchiveFile );
@@ -75,9 +75,9 @@ public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePul
75
75
* Create docker archive for building image
76
76
*
77
77
* @param imageConfiguration image configuration
78
- * @param buildContext docker build context
79
- * @param archivePath build archive only flag, it can have values TRUE or FALSE and also
80
- * it can hold path to archive where it might get copied over
78
+ * @param buildContext docker build context
79
+ * @param archivePath build archive only flag, it can have values TRUE or FALSE and also
80
+ * it can hold path to archive where it might get copied over
81
81
* @return tarball for docker image
82
82
* @throws MojoExecutionException in case any exception comes during building tarball
83
83
*/
@@ -130,9 +130,9 @@ public void tagImage(ImageConfiguration imageConfig) throws DockerAccessExceptio
130
130
* Build an image
131
131
*
132
132
* @param imageConfig the image configuration
133
- * @param params mojo params for the project
134
- * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
135
- * @param buildArgs docker build args
133
+ * @param params mojo params for the project
134
+ * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
135
+ * @param buildArgs docker build args
136
136
* @throws DockerAccessException
137
137
* @throws MojoExecutionException
138
138
*/
@@ -160,7 +160,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params,
160
160
docker .loadImage (imageName , tarArchive );
161
161
log .info ("%s: Loaded tarball in %s" , buildConfig .getDockerArchive (), EnvUtil .formatDurationTill (time ));
162
162
163
- if (archiveImageName != null && !archiveImageName .equals (imageName )) {
163
+ if (archiveImageName != null && !archiveImageName .equals (imageName )) {
164
164
docker .tag (archiveImageName , imageName , true );
165
165
}
166
166
@@ -214,7 +214,7 @@ private Map<String, String> prepareBuildArgs(Map<String, String> buildArgs, Buil
214
214
}
215
215
216
216
private String getArchiveImageName (BuildImageConfiguration buildConfig , File tarArchive ) throws MojoExecutionException {
217
- if (buildConfig .getLoadNamePattern () == null || buildConfig .getLoadNamePattern ().length () == 0 ) {
217
+ if (buildConfig .getLoadNamePattern () == null || buildConfig .getLoadNamePattern ().length () == 0 ) {
218
218
return null ;
219
219
}
220
220
@@ -229,11 +229,11 @@ private String getArchiveImageName(BuildImageConfiguration buildConfig, File tar
229
229
230
230
try {
231
231
archiveImageName = matchArchiveImagesToPattern (buildConfig .getLoadNamePattern (), manifest );
232
- } catch (PatternSyntaxException e ) {
232
+ } catch (PatternSyntaxException e ) {
233
233
throw new MojoExecutionException ("Unable to interpret loadNamePattern " + buildConfig .getLoadNamePattern (), e );
234
234
}
235
235
236
- if (archiveImageName == null ) {
236
+ if (archiveImageName == null ) {
237
237
throw new MojoExecutionException ("No image in the archive has a tag that matches pattern " + buildConfig .getLoadNamePattern ());
238
238
}
239
239
@@ -248,10 +248,10 @@ private ImageArchiveManifest readArchiveManifest(File tarArchive) throws IOExcep
248
248
log .info ("%s: Read archive manifest in %s" , tarArchive , EnvUtil .formatDurationTill (time ));
249
249
250
250
// Show the results of reading the manifest to users trying to debug their configuration
251
- if (log .isDebugEnabled ()) {
252
- for (ImageArchiveManifestEntry entry : manifest .getEntries ()) {
251
+ if (log .isDebugEnabled ()) {
252
+ for (ImageArchiveManifestEntry entry : manifest .getEntries ()) {
253
253
log .debug ("Entry ID: %s has %d repo tag(s)" , entry .getId (), entry .getRepoTags ().size ());
254
- for (String repoTag : entry .getRepoTags ()) {
254
+ for (String repoTag : entry .getRepoTags ()) {
255
255
log .debug ("Repo Tag: %s" , repoTag );
256
256
}
257
257
}
@@ -267,16 +267,16 @@ private String matchArchiveImagesToPattern(String imageNamePattern, ImageArchive
267
267
Map <String , ImageArchiveManifestEntry > entries = ImageArchiveUtil .findEntriesByRepoTagPattern (imageNameRegex , manifest );
268
268
269
269
// Show the matches from the manifest to users trying to debug their configuration
270
- if (log .isDebugEnabled ()) {
271
- for (Map .Entry <String , ImageArchiveManifestEntry > entry : entries .entrySet ()) {
270
+ if (log .isDebugEnabled ()) {
271
+ for (Map .Entry <String , ImageArchiveManifestEntry > entry : entries .entrySet ()) {
272
272
log .debug ("Repo tag pattern matched %s referring to image %s" , entry .getKey (), entry .getValue ().getId ());
273
273
}
274
274
}
275
275
276
- if (!entries .isEmpty ()) {
276
+ if (!entries .isEmpty ()) {
277
277
Map .Entry <String , ImageArchiveManifestEntry > matchedEntry = entries .entrySet ().iterator ().next ();
278
278
279
- if (ImageArchiveUtil .mapEntriesById (entries .values ()).size () > 1 ) {
279
+ if (ImageArchiveUtil .mapEntriesById (entries .values ()).size () > 1 ) {
280
280
log .warn ("Multiple image ids matched pattern %s: using tag %s associated with id %s" ,
281
281
imageNamePattern , matchedEntry .getKey (), matchedEntry .getValue ().getId ());
282
282
} else {
@@ -344,16 +344,16 @@ private Map<String, String> addBuildArgsFromDockerConfig() {
344
344
JsonObject proxies = dockerConfig .getAsJsonObject ("proxies" );
345
345
if (proxies .has ("default" )) {
346
346
JsonObject defaultProxyObj = proxies .getAsJsonObject ("default" );
347
- String [] proxyMapping = new String [] {
347
+ String [] proxyMapping = new String []{
348
348
"httpProxy" , "http_proxy" ,
349
349
"httpsProxy" , "https_proxy" ,
350
350
"noProxy" , "no_proxy" ,
351
351
"ftpProxy" , "ftp_proxy"
352
352
};
353
353
354
- for (int index = 0 ; index < proxyMapping .length ; index += 2 ) {
354
+ for (int index = 0 ; index < proxyMapping .length ; index += 2 ) {
355
355
if (defaultProxyObj .has (proxyMapping [index ])) {
356
- buildArgs .put (proxyMapping [index + 1 ], defaultProxyObj .get (proxyMapping [index ]).getAsString ());
356
+ buildArgs .put (proxyMapping [index + 1 ], defaultProxyObj .get (proxyMapping [index ]).getAsString ());
357
357
}
358
358
}
359
359
}
@@ -377,7 +377,7 @@ private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager
377
377
} else {
378
378
fromImages = new LinkedList <>();
379
379
String baseImage = extractBaseFromConfiguration (buildConfig );
380
- if (baseImage != null ) {
380
+ if (baseImage != null ) {
381
381
fromImages .add (extractBaseFromConfiguration (buildConfig ));
382
382
}
383
383
}
@@ -388,6 +388,20 @@ private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager
388
388
}
389
389
}
390
390
391
+ private void autoPullCacheFromImage (ImageConfiguration imageConfig , ImagePullManager imagePullManager , BuildContext buildContext ) throws MojoExecutionException {
392
+ if (imageConfig .getBuildConfiguration ().getCacheFrom () == null ) {
393
+ return ;
394
+ }
395
+
396
+ for (String cacheFromImage : imageConfig .getBuildConfiguration ().getCacheFrom ()) {
397
+ try {
398
+ registryService .pullImageWithPolicy (cacheFromImage , imagePullManager , buildContext .getRegistryConfig (), queryService .hasImage (cacheFromImage ));
399
+ } catch (DockerAccessException e ) {
400
+ log .warn ("Could not pull cacheFrom image: '%s'. Reason: %s" , cacheFromImage , e .getMessage ());
401
+ }
402
+ }
403
+ }
404
+
391
405
private String extractBaseFromConfiguration (BuildImageConfiguration buildConfig ) {
392
406
String fromImage ;
393
407
fromImage = buildConfig .getFrom ();
@@ -405,9 +419,9 @@ private List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConf
405
419
try {
406
420
File fullDockerFilePath = buildConfig .getAbsoluteDockerFilePath (buildContext .getMojoParameters ());
407
421
fromImage = DockerFileUtil .extractBaseImages (
408
- fullDockerFilePath ,
409
- DockerFileUtil .createInterpolator (buildContext .getMojoParameters (), buildConfig .getFilter ()),
410
- buildConfig .getArgs ());
422
+ fullDockerFilePath ,
423
+ DockerFileUtil .createInterpolator (buildContext .getMojoParameters (), buildConfig .getFilter ()),
424
+ buildConfig .getArgs ());
411
425
} catch (IOException e ) {
412
426
// Cant extract base image, so we wont try an auto pull. An error will occur later anyway when
413
427
// building the image, so we are passive here.
0 commit comments