-
Notifications
You must be signed in to change notification settings - Fork 634
/
BuildImageConfiguration.java
799 lines (644 loc) · 20.6 KB
/
BuildImageConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
package io.fabric8.maven.docker.config;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import io.fabric8.maven.docker.util.DeepCopy;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugins.annotations.Parameter;
/**
* @author roland
* @since 02.09.14
*/
public class BuildImageConfiguration implements Serializable {
public static final String DEFAULT_FILTER = "${*}";
public static final String DEFAULT_CLEANUP = "try";
/**
* Directory is used as build context.
* If not specified, dockerfile's parent directory is used as build context.
*/
@Parameter
private String contextDir;
/**
* Directory holding an external Dockerfile which is used to build the
* image. This Dockerfile will be enriched by the addition build configuration
*/
@Parameter
@Deprecated
private String dockerFileDir;
/**
* Path to a dockerfile to use.
* Multiple different Dockerfiles can be specified that way. If set overwrites a possibly givem
* <code>dockerFileDir</code>
*/
@Parameter
private String dockerFile;
/**
* Path to a docker archive to load an image instead of building from scratch.
* Note only either dockerFile/dockerFileDir or
* dockerArchive can be used.
*/
@Parameter
private String dockerArchive;
/**
* Pattern for the image name we expect to find in the dockerArchive.
*
* If set, the archive is scanned prior to sending to Docker and checked to
* ensure a matching name is found linked to one of the images in the archive.
* After loading, the image with the matching name will be tagged with the
* image name configured in this project.
*/
@Parameter
private String loadNamePattern;
/**
* How interpolation of a dockerfile should be performed
*/
@Parameter
private String filter;
/**
* Base Image
*/
@Parameter
private String from;
/**
* Extended version for <from>
*/
@Parameter
private Map<String, String> fromExt;
@Parameter
private List<String> cacheFrom;
@Parameter
private String registry;
@Parameter
private String maintainer;
@Parameter
private String network;
@Parameter
private List<String> ports;
/**
* Policy for pulling the base images
*/
@Parameter
private String imagePullPolicy;
/**
* SHELL excutable with params
*/
@Parameter
private Arguments shell;
/**
* RUN Commands within Build/Image
*/
@Parameter
private List<String> runCmds;
@Parameter
private String cleanup;
@Deprecated
@Parameter
private Boolean nocache;
@Parameter
private Boolean noCache;
@Parameter
private Boolean squash;
@Parameter
private Boolean optimise;
@Parameter
private List<String> volumes;
@Parameter
private List<String> tags;
@Parameter
private Map<String, String> env;
@Parameter
private Map<String, String> labels;
@Parameter
private Map<String, String> args;
@Parameter
private Arguments entryPoint;
@Parameter
@Deprecated
private String command;
@Parameter
private String workdir;
@Parameter
private Arguments cmd;
@Parameter
private String user;
@Parameter
private HealthCheckConfiguration healthCheck;
@Parameter
private AssemblyConfiguration assembly;
@Parameter
private List<AssemblyConfiguration> assemblies;
@Parameter
private Boolean skip;
@Parameter
private Boolean skipPush;
@Parameter
private ArchiveCompression compression = ArchiveCompression.none;
@Parameter
private Map<String,String> buildOptions;
// Path to Dockerfile to use, initialized lazily ....
private File dockerFileFile, dockerArchiveFile;
public BuildImageConfiguration() {}
public boolean isDockerFileMode() {
return dockerFileFile != null;
}
public String getLoadNamePattern() {
return loadNamePattern;
}
public File getContextDir() {
if (!isDockerFileMode()) {
return null;
}
if (contextDir != null) {
return new File(contextDir);
}
if (getDockerFile().getParentFile() == null) {
return new File("");
}
return getDockerFile().getParentFile();
}
public String getContextDirRaw() {
return contextDir;
}
public File getDockerFile() {
return dockerFileFile;
}
public File getDockerArchive() {
return dockerArchiveFile;
}
public String getDockerFileRaw() {
return dockerFile;
}
public String getDockerArchiveRaw() {
return dockerArchive;
}
public String getDockerFileDirRaw() {
return dockerFileDir;
}
public String getFilter() {
return filter != null ? filter : DEFAULT_FILTER;
}
public String getFilterRaw() {
return filter;
}
public String getFrom() {
if (from == null && getFromExt() != null) {
return getFromExt().get("name");
}
return from;
}
public Map<String, String> getFromExt() {
return fromExt;
}
public List<String> getCacheFrom() {
return cacheFrom;
}
public String getNetwork() {
return network;
}
public String getRegistry() {
return registry;
}
public String getMaintainer() {
return maintainer;
}
public String getWorkdir() {
return workdir;
}
/**
* @deprecated Use {@link #getAssemblyConfigurations()} instead.
*/
@Deprecated
public AssemblyConfiguration getAssemblyConfiguration() {
return assembly;
}
@Nonnull
public List<AssemblyConfiguration> getAssemblyConfigurations() {
final List<AssemblyConfiguration> assemblyConfigurations = new ArrayList<>();
if (assemblies != null) {
for (AssemblyConfiguration config : assemblies) {
if (config != null) {
assemblyConfigurations.add(config);
}
}
}
if (assembly != null) {
assemblyConfigurations.add(assembly);
}
return assemblyConfigurations;
}
@Nonnull
public List<String> getPorts() {
return EnvUtil.removeEmptyEntries(ports);
}
public String getImagePullPolicy() {
return imagePullPolicy;
}
@Nonnull
public List<String> getVolumes() {
return EnvUtil.removeEmptyEntries(volumes);
}
@Nonnull
public List<String> getTags() {
return EnvUtil.removeEmptyEntries(tags);
}
public Map<String, String> getEnv() {
return env;
}
public Map<String, String> getLabels() {
return labels;
}
public Arguments getCmd() {
return cmd;
}
@Deprecated
public String getCommand() {
return command;
}
public String getCleanup() {
return cleanup;
}
public CleanupMode cleanupMode() {
return CleanupMode.parse(cleanup != null ? cleanup : DEFAULT_CLEANUP);
}
public boolean noCache() {
if (noCache != null) {
return noCache;
}
if (nocache != null) {
return nocache;
}
return false;
}
public boolean squash() {
if (squash != null) {
return squash;
}
return false;
}
public boolean optimise() {
return optimise != null ? optimise : false;
}
public boolean skip() {
return skip != null ? skip : false;
}
public boolean skipPush() {
return skipPush != null ? skipPush : false;
}
public Boolean getNoCache() {
return noCache != null ? noCache : nocache;
}
public Boolean getSquash() {
return squash != null ? squash : false;
}
public Boolean getOptimise() {
return optimise;
}
public Boolean getSkip() {
return skip;
}
public Boolean getSkipPush() {
return skipPush;
}
public ArchiveCompression getCompression() {
return compression;
}
public Map<String, String> getBuildOptions() {
return buildOptions;
}
public Arguments getEntryPoint() {
return entryPoint;
}
public Arguments getShell() {
return shell;
}
@Nonnull
public List<String> getRunCmds() {
return EnvUtil.removeEmptyEntries(runCmds);
}
public String getUser() {
return user;
}
public HealthCheckConfiguration getHealthCheck() {
return healthCheck;
}
public Map<String, String> getArgs() {
return args;
}
public File getAbsoluteContextDirPath(MojoParameters mojoParams) {
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getContextDir().getPath());
}
public File getAbsoluteDockerFilePath(MojoParameters mojoParams) {
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerFile().getPath());
}
public File getAbsoluteDockerTarPath(MojoParameters mojoParams) {
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerArchive().getPath());
}
public void initTags(ConfigHelper.NameFormatter nameFormatter) {
if (tags != null) {
tags = tags.stream().map(nameFormatter::format).collect(Collectors.toList());
}
}
public static class Builder {
private final BuildImageConfiguration config;
public Builder() {
this(null);
}
public Builder(BuildImageConfiguration that) {
if (that == null) {
this.config = new BuildImageConfiguration();
} else {
this.config = DeepCopy.copy(that);
}
}
public Builder contextDir(String dir) {
config.contextDir = dir;
return this;
}
public Builder dockerFileDir(String dir) {
config.dockerFileDir = dir;
return this;
}
public Builder dockerFile(String file) {
config.dockerFile = file;
return this;
}
public Builder dockerArchive(String archive) {
config.dockerArchive = archive;
return this;
}
public Builder loadNamePattern(String archiveEntryRepoTagPattern) {
config.loadNamePattern = archiveEntryRepoTagPattern;
return this;
}
public Builder filter(String filter) {
config.filter = filter;
return this;
}
public Builder from(String from) {
config.from = from;
return this;
}
public Builder fromExt(Map<String, String> fromExt) {
config.fromExt = fromExt;
return this;
}
public Builder cacheFrom(String cacheFrom, String ...more) {
if (more == null || more.length == 0) {
return cacheFrom(Collections.singletonList(cacheFrom));
}
List<String> list = new ArrayList<>();
list.add(cacheFrom);
list.addAll(Arrays.asList(more));
return cacheFrom(list);
}
public Builder cacheFrom(Collection<String> cacheFrom) {
config.cacheFrom = cacheFrom != null ? new ArrayList<>(cacheFrom) : null;
return this;
}
public Builder registry(String registry) {
config.registry = registry;
return this;
}
public Builder maintainer(String maintainer) {
config.maintainer = maintainer;
return this;
}
public Builder network(String network) {
config.network = network;
return this;
}
public Builder workdir(String workdir) {
config.workdir = workdir;
return this;
}
public Builder assembly(AssemblyConfiguration assembly) {
config.assembly = assembly;
return this;
}
public Builder assemblies(List<AssemblyConfiguration> assemblies) {
config.assemblies = assemblies;
return this;
}
public Builder ports(List<String> ports) {
config.ports = ports;
return this;
}
public Builder imagePullPolicy(String imagePullPolicy) {
config.imagePullPolicy = imagePullPolicy;
return this;
}
public Builder shell(Arguments shell) {
if(shell != null) {
config.shell = shell;
}
return this;
}
public Builder runCmds(List<String> theCmds) {
if (theCmds == null) {
config.runCmds = new ArrayList<>();
} else {
config.runCmds = theCmds;
}
return this;
}
public Builder volumes(List<String> volumes) {
config.volumes = volumes;
return this;
}
public Builder tags(List<String> tags) {
config.tags = tags;
return this;
}
public Builder env(Map<String, String> env) {
config.env = env;
return this;
}
public Builder args(Map<String, String> args) {
config.args = args;
return this;
}
public Builder labels(Map<String, String> labels) {
config.labels = labels;
return this;
}
public Builder cmd(Arguments cmd) {
if (cmd != null) {
config.cmd = cmd;
}
return this;
}
public Builder cleanup(String cleanup) {
config.cleanup = cleanup;
return this;
}
public Builder compression(String compression) {
if (compression == null) {
config.compression = ArchiveCompression.none;
} else {
config.compression = ArchiveCompression.valueOf(compression);
}
return this;
}
public Builder noCache(Boolean noCache) {
config.noCache = noCache;
return this;
}
public Builder squash(Boolean squash) {
config.squash = squash;
return this;
}
public Builder optimise(Boolean optimise) {
config.optimise = optimise;
return this;
}
public Builder entryPoint(Arguments entryPoint) {
if (entryPoint != null) {
config.entryPoint = entryPoint;
}
return this;
}
public Builder user(String user) {
config.user = user;
return this;
}
public Builder healthCheck(HealthCheckConfiguration healthCheck) {
config.healthCheck = healthCheck;
return this;
}
public Builder skip(Boolean skip) {
config.skip = skip;
return this;
}
public Builder skipPush(Boolean skipPush) {
config.skipPush = skipPush;
return this;
}
public Builder buildOptions(Map<String,String> buildOptions) {
config.buildOptions = buildOptions;
return this;
}
public BuildImageConfiguration build() {
return config;
}
}
public String initAndValidate(Logger log) throws IllegalArgumentException {
if (entryPoint != null) {
entryPoint.validate();
}
if (cmd != null) {
cmd.validate();
}
if (healthCheck != null) {
healthCheck.validate();
}
ensureUniqueAssemblyNames(log);
if (command != null) {
log.warn("<command> in the <build> configuration is deprecated and will be be removed soon");
log.warn("Please use <cmd> with nested <shell> or <exec> sections instead.");
log.warn("");
log.warn("More on this is explained in the user manual: ");
log.warn("https://github.com/fabric8io/docker-maven-plugin/blob/master/doc/manual.md#start-up-arguments");
log.warn("");
log.warn("Migration is trivial, see changelog to version 0.12.0 -->");
log.warn("https://github.com/fabric8io/docker-maven-plugin/blob/master/doc/changelog.md");
log.warn("");
log.warn("For now, the command is automatically translated for you to the shell form:");
log.warn(" <cmd>%s</cmd>", command);
}
initDockerFileFile(log);
if (cacheFrom != null && !cacheFrom.isEmpty()) {
// cachefrom query param was introduced in v1.25
return "1.25";
} else if (healthCheck != null) {
// HEALTHCHECK support added later
return "1.24";
} else if (args != null) {
// ARG support came in later
return "1.21";
} else {
return null;
}
}
private void ensureUniqueAssemblyNames(Logger log) {
List<AssemblyConfiguration> assemblyConfigurations = getAssemblyConfigurations();
Set<String> assemblyNames = new HashSet<>();
for (AssemblyConfiguration config : assemblyConfigurations) {
boolean wasElementAbsent = assemblyNames.add(config.getName());
if (!wasElementAbsent) {
log.error("Multiple assemblies use the name %s. Please assign each assembly a unique name.");
throw new IllegalArgumentException("Assembly names must be unique");
}
}
}
// Initialize the dockerfile location and the build mode
private void initDockerFileFile(Logger log) {
// can't have dockerFile/dockerFileDir and dockerArchive
if ((dockerFile != null || dockerFileDir != null) && dockerArchive != null) {
throw new IllegalArgumentException("Both <dockerFile> (<dockerFileDir>) and <dockerArchive> are set. " +
"Only one of them can be specified.");
}
dockerFileFile = findDockerFileFile(log);
if (dockerArchive != null) {
dockerArchiveFile = new File(dockerArchive);
}
}
private File findDockerFileFile(Logger log) {
if(dockerFileDir != null && contextDir != null) {
log.warn("Both contextDir (%s) and deprecated dockerFileDir (%s) are configured. Using contextDir.", contextDir, dockerFileDir);
}
if (dockerFile != null) {
File dFile = new File(dockerFile);
if (dockerFileDir == null && contextDir == null) {
return dFile;
} else {
if(contextDir != null) {
if (dFile.isAbsolute()) {
return dFile;
}
return new File(contextDir, dockerFile);
}
if (dockerFileDir != null) {
if (dFile.isAbsolute()) {
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
}
log.warn("dockerFileDir parameter is deprecated, please migrate to contextDir");
return new File(dockerFileDir, dockerFile);
}
}
}
if (contextDir != null) {
return new File(contextDir, "Dockerfile");
}
if (dockerFileDir != null) {
return new File(dockerFileDir, "Dockerfile");
}
// TODO: Remove the following deprecated handling section
if (dockerArchive == null) {
Optional<String> deprecatedDockerFileDir =
getAssemblyConfigurations().stream()
.map(AssemblyConfiguration::getDockerFileDir)
.filter(Objects::nonNull)
.findFirst();
if (deprecatedDockerFileDir.isPresent()) {
log.warn("<dockerFileDir> in the <assembly> section of a <build> configuration is deprecated");
log.warn("Please use <dockerFileDir> or <dockerFile> directly within the <build> configuration instead");
return new File(deprecatedDockerFileDir.get(),"Dockerfile");
}
}
// No dockerfile mode
return null;
}
}