/
ContainerBuildPlan.java
507 lines (459 loc) 路 15.8 KB
/
ContainerBuildPlan.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
/*
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.cloud.tools.jib.api.buildplan;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/** Describes a plan to build a container. */
@Immutable
public class ContainerBuildPlan {
/** Builder for {@link ContainerBuildPlan}. */
public static class Builder {
private String baseImage = "scratch";
private Instant creationTime = Instant.EPOCH;
private ImageFormat format = ImageFormat.Docker;
// LinkedHashSet to preserve the order
private Set<Platform> platforms =
new LinkedHashSet<>(Collections.singleton(new Platform("amd64", "linux")));
// image execution parameters
private Map<String, String> environment = new HashMap<>();
private Map<String, String> labels = new HashMap<>();
private Set<AbsoluteUnixPath> volumes = new HashSet<>();
private Set<Port> exposedPorts = new HashSet<>();
@Nullable private String user;
@Nullable private AbsoluteUnixPath workingDirectory;
@Nullable private List<String> entrypoint;
@Nullable private List<String> cmd;
private List<LayerObject> layers = new ArrayList<>();
private Builder() {}
/**
* Image reference to a base image. The default is {@code scratch}.
*
* @param baseImage image reference to a base image
* @return this
*/
public Builder setBaseImage(String baseImage) {
this.baseImage = baseImage;
return this;
}
/**
* Adds a desired image platform (OS and architecture pair). If the base image reference is a
* Docker manifest list or an OCI image index, an image builder may select the base image
* matching the given platform. If the base image reference is an image manifest, an image
* builder may ignore the given platform and use the platform of the base image or may decide to
* raise on error.
*
* <p>Note that a new build plan starts with "amd64/linux" as the default platform. If you want
* to reset the default platform instead of adding a new one, use {@link #setPlatforms(Set)}.
*
* @param architecture architecture (for example, {@code amd64}) to select a base image in case
* of a manifest list
* @param os OS (for example, {@code linux}) to select a base image in case of a manifest list
* @return this
*/
public Builder addPlatform(String architecture, String os) {
platforms.add(new Platform(architecture, os));
return this;
}
/**
* Sets a desired platform (properties including OS and architecture) list. If the base image
* reference is a Docker manifest list or an OCI image index, an image builder may select the
* base images matching the given platforms. If the base image reference is an image manifest,
* an image builder may ignore the given platforms and use the platform of the base image or may
* decide to raise on error.
*
* <p>Note that a new build plan starts with "amd64/linux" as the default platform.
*
* @param platforms list of platforms to select base images in case of a manifest list
* @return this
*/
public Builder setPlatforms(Set<Platform> platforms) {
if (platforms.isEmpty()) {
throw new IllegalArgumentException("platforms set cannot be empty");
}
this.platforms = new LinkedHashSet<>(platforms);
return this;
}
/**
* Sets the container image creation time. The default is {@link Instant#EPOCH}.
*
* @param creationTime the container image creation time
* @return this
*/
public Builder setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
return this;
}
/**
* Sets the format to build the container image as. Use {@link ImageFormat#Docker} for Docker
* V2.2 or {@link ImageFormat#OCI} for OCI.
*
* @param format the {@link ImageFormat}
* @return this
*/
public Builder setFormat(ImageFormat format) {
this.format = format;
return this;
}
/**
* Sets the container environment. These environment variables are available to the program
* launched by the container entrypoint command. This replaces any previously-set environment
* variables. Note that these values are added to the base image values.
*
* <p>This is similar to <a href="https://docs.docker.com/engine/reference/builder/#env">{@code
* ENV} in Dockerfiles</a> or {@code env} in the <a
* href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#container-v1-core">Kubernetes
* Container spec</a>.
*
* @param environment a map of environment variable names to values
* @return this
*/
public Builder setEnvironment(Map<String, String> environment) {
this.environment = new HashMap<>(environment);
return this;
}
/**
* Adds a variable in the container environment.
*
* @param name the environment variable name
* @param value the environment variable value
* @return this
* @see #setEnvironment
*/
public Builder addEnvironmentVariable(String name, String value) {
environment.put(name, value);
return this;
}
/**
* Sets the directories that may hold externally mounted volumes. Note that these values are
* added to the base image values.
*
* <p>This is similar to <a
* href="https://docs.docker.com/engine/reference/builder/#volume">{@code VOLUME} in
* Dockerfiles</a>.
*
* @param volumes the directory paths on the container filesystem to set as volumes
* @return this
*/
public Builder setVolumes(Set<AbsoluteUnixPath> volumes) {
this.volumes = new HashSet<>(volumes);
return this;
}
/**
* Adds a directory that may hold an externally mounted volume.
*
* @param volume a directory path on the container filesystem to represent a volume
* @return this
* @see #setVolumes(Set)
*/
public Builder addVolume(AbsoluteUnixPath volume) {
volumes.add(volume);
return this;
}
/**
* Sets the labels for the container. This replaces any previously-set labels. Note that these
* values are added to the base image values.
*
* <p>This is similar to <a
* href="https://docs.docker.com/engine/reference/builder/#label">{@code LABEL} in
* Dockerfiles</a>.
*
* @param labels a map of label keys to values
* @return this
*/
public Builder setLabels(Map<String, String> labels) {
this.labels = new HashMap<>(labels);
return this;
}
/**
* Sets a label for the container.
*
* @param key the label key
* @param value the label value
* @return this
*/
public Builder addLabel(String key, String value) {
labels.put(key, value);
return this;
}
/**
* Sets the ports to expose from the container. Ports exposed will allow ingress traffic. This
* replaces any previously-set exposed ports. Note that these values are added to the base image
* values.
*
* <p>Use {@link Port#tcp} to expose a port for TCP traffic and {@link Port#udp} to expose a
* port for UDP traffic.
*
* <p>This is similar to <a
* href="https://docs.docker.com/engine/reference/builder/#expose">{@code EXPOSE} in
* Dockerfiles</a> or {@code ports} in the <a
* href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#container-v1-core">Kubernetes
* Container spec</a>.
*
* @param exposedPorts the ports to expose
* @return this
*/
public Builder setExposedPorts(Set<Port> exposedPorts) {
this.exposedPorts = new HashSet<>(exposedPorts);
return this;
}
/**
* Adds a port to expose from the container.
*
* @param exposedPort the port to expose
* @return this
* @see #setExposedPorts(Set)
*/
public Builder addExposedPort(Port exposedPort) {
exposedPorts.add(exposedPort);
return this;
}
/**
* Sets the user and group to run the container as. {@code user} can be a username or UID along
* with an optional groupname or GID. {@code null} signals to use the base image value.
*
* <p>The following are valid formats for {@code user}
*
* <ul>
* <li>{@code user}
* <li>{@code uid}
* <li>{@code :group}
* <li>{@code :gid}
* <li>{@code user:group}
* <li>{@code uid:gid}
* <li>{@code uid:group}
* <li>{@code user:gid}
* </ul>
*
* @param user the user to run the container as
* @return this
*/
public Builder setUser(@Nullable String user) {
this.user = user;
return this;
}
/**
* Sets the working directory in the container. {@code null} signals to use the base image
* value.
*
* @param workingDirectory the working directory
* @return this
*/
public Builder setWorkingDirectory(@Nullable AbsoluteUnixPath workingDirectory) {
this.workingDirectory = workingDirectory;
return this;
}
/**
* Sets the container entrypoint. This is the beginning of the command that is run when the
* container starts. {@link #setCmd} sets additional tokens. {@code null} signals to use the
* base image value.
*
* <p>This is similar to <a
* href="https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example">{@code
* ENTRYPOINT} in Dockerfiles</a> or {@code command} in the <a
* href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#container-v1-core">Kubernetes
* Container spec</a>.
*
* @param entrypoint a list of the entrypoint command
* @return this
*/
public Builder setEntrypoint(@Nullable List<String> entrypoint) {
if (entrypoint == null) {
this.entrypoint = null;
} else {
this.entrypoint = new ArrayList<>(entrypoint);
}
return this;
}
/**
* Sets the container entrypoint program arguments. These are additional tokens added to the end
* of the entrypoint command. {@code null} signals to use the base image value (only when
* entrypoint is also {@code null}).
*
* <p>This is similar to <a href="https://docs.docker.com/engine/reference/builder/#cmd">{@code
* CMD} in Dockerfiles</a> or {@code args} in the <a
* href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#container-v1-core">Kubernetes
* Container spec</a>.
*
* <p>For example, if the entrypoint was {@code myprogram --flag subcommand} and program
* arguments were {@code hello world}, then the command that run when the container starts is
* {@code myprogram --flag subcommand hello world}.
*
* @param cmd a list of program argument tokens
* @return this
*/
public Builder setCmd(@Nullable List<String> cmd) {
if (cmd == null) {
this.cmd = null;
} else {
this.cmd = new ArrayList<>(cmd);
}
return this;
}
public Builder addLayer(LayerObject layer) {
layers.add(layer);
return this;
}
public Builder setLayers(List<? extends LayerObject> layer) {
layers = new ArrayList<>(layer);
return this;
}
/**
* Returns the built {@link ContainerBuildPlan}.
*
* @return container build plan
*/
public ContainerBuildPlan build() {
return new ContainerBuildPlan(
baseImage,
platforms,
creationTime,
format,
environment,
labels,
volumes,
exposedPorts,
user,
workingDirectory,
entrypoint,
cmd,
layers);
}
}
public static Builder builder() {
return new Builder();
}
private final String baseImage;
private final Set<Platform> platforms;
private final Instant creationTime;
private final ImageFormat format;
// image execution parameters
private final Map<String, String> environment;
private final Map<String, String> labels;
private final Set<AbsoluteUnixPath> volumes;
private final Set<Port> exposedPorts;
@Nullable private final String user;
@Nullable private final AbsoluteUnixPath workingDirectory;
@Nullable private final List<String> entrypoint;
@Nullable private final List<String> cmd;
private final List<LayerObject> layers;
private ContainerBuildPlan(
String baseImage,
Set<Platform> platforms,
Instant creationTime,
ImageFormat format,
Map<String, String> environment,
Map<String, String> labels,
Set<AbsoluteUnixPath> volumes,
Set<Port> exposedPorts,
@Nullable String user,
@Nullable AbsoluteUnixPath workingDirectory,
@Nullable List<String> entrypoint,
@Nullable List<String> cmd,
List<LayerObject> layers) {
this.baseImage = baseImage;
this.platforms = platforms;
this.creationTime = creationTime;
this.format = format;
this.environment = environment;
this.labels = labels;
this.volumes = volumes;
this.exposedPorts = exposedPorts;
this.user = user;
this.workingDirectory = workingDirectory;
this.entrypoint = entrypoint;
this.cmd = cmd;
this.layers = layers;
}
public String getBaseImage() {
return baseImage;
}
/**
* Creates and returns a default platform if the user hasn't added or set any platforms ,else
* returns a list of user specified platforms .
*
* @return platforms a list of user specified platforms.
*/
public Set<Platform> getPlatforms() {
return new LinkedHashSet<>(platforms);
}
public ImageFormat getFormat() {
return format;
}
public Instant getCreationTime() {
return creationTime;
}
public Map<String, String> getEnvironment() {
return new HashMap<>(environment);
}
public Set<AbsoluteUnixPath> getVolumes() {
return new HashSet<>(volumes);
}
public Map<String, String> getLabels() {
return new HashMap<>(labels);
}
public Set<Port> getExposedPorts() {
return new HashSet<>(exposedPorts);
}
@Nullable
public String getUser() {
return user;
}
@Nullable
public AbsoluteUnixPath getWorkingDirectory() {
return workingDirectory;
}
@Nullable
public List<String> getEntrypoint() {
return entrypoint == null ? null : new ArrayList<>(entrypoint);
}
@Nullable
public List<String> getCmd() {
return cmd == null ? null : new ArrayList<>(cmd);
}
public List<? extends LayerObject> getLayers() {
return new ArrayList<>(layers);
}
/**
* Creates a builder configured with the current values.
*
* @return {@link Builder} configured with the current values.
*/
public Builder toBuilder() {
return builder()
.setBaseImage(baseImage)
.setPlatforms(platforms)
.setCreationTime(creationTime)
.setFormat(format)
.setEnvironment(environment)
.setLabels(labels)
.setVolumes(volumes)
.setExposedPorts(exposedPorts)
.setUser(user)
.setWorkingDirectory(workingDirectory)
.setEntrypoint(entrypoint)
.setCmd(cmd)
.setLayers(layers);
}
}