1717package com .google .cloud .tools .jib .image .json ;
1818
1919import com .google .cloud .tools .jib .blob .BlobDescriptor ;
20+ import com .google .cloud .tools .jib .configuration .Port ;
21+ import com .google .cloud .tools .jib .configuration .Port .Protocol ;
2022import com .google .cloud .tools .jib .image .DescriptorDigest ;
2123import com .google .cloud .tools .jib .image .DigestOnlyLayer ;
2224import com .google .cloud .tools .jib .image .Image ;
2527import com .google .cloud .tools .jib .image .LayerPropertyNotFoundException ;
2628import com .google .cloud .tools .jib .image .ReferenceLayer ;
2729import com .google .cloud .tools .jib .image .ReferenceNoDiffIdLayer ;
30+ import com .google .common .annotations .VisibleForTesting ;
31+ import com .google .common .collect .ImmutableList ;
2832import java .util .ArrayList ;
2933import java .util .List ;
34+ import java .util .Map ;
35+ import java .util .regex .Matcher ;
36+ import java .util .regex .Pattern ;
37+ import javax .annotation .Nullable ;
3038
3139/** Translates {@link V21ManifestTemplate} and {@link V22ManifestTemplate} into {@link Image}. */
3240public class JsonToImageTranslator {
3341
42+ /**
43+ * Pattern used for parsing information out of exposed port configurations. Only accepts single
44+ * ports with protocol.
45+ *
46+ * <p>Example matches: 100, 1000/tcp, 2000/udp
47+ */
48+ private static final Pattern portPattern = Pattern .compile ("(\\ d+)(?:/(tcp|udp))?" );
49+
3450 /**
3551 * Translates {@link V21ManifestTemplate} to {@link Image}.
3652 *
@@ -60,11 +76,14 @@ public static Image<Layer> toImage(V21ManifestTemplate manifestTemplate)
6076 * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
6177 * information.
6278 * @throws LayerPropertyNotFoundException if adding image layers fails.
79+ * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
80+ * format
6381 */
6482 public static Image <Layer > toImage (
6583 BuildableManifestTemplate manifestTemplate ,
6684 ContainerConfigurationTemplate containerConfigurationTemplate )
67- throws LayerCountMismatchException , LayerPropertyNotFoundException {
85+ throws LayerCountMismatchException , LayerPropertyNotFoundException ,
86+ BadContainerConfigurationFormatException {
6887 List <ReferenceNoDiffIdLayer > layers = new ArrayList <>();
6988 for (BuildableManifestTemplate .ContentDescriptorTemplate layerObjectTemplate :
7089 manifestTemplate .getLayers ()) {
@@ -103,7 +122,8 @@ public static Image<Layer> toImage(
103122 }
104123
105124 if (containerConfigurationTemplate .getContainerExposedPorts () != null ) {
106- imageBuilder .setExposedPorts (containerConfigurationTemplate .getContainerExposedPorts ());
125+ imageBuilder .setExposedPorts (
126+ portMapToList (containerConfigurationTemplate .getContainerExposedPorts ()));
107127 }
108128
109129 if (containerConfigurationTemplate .getContainerEnvironment () != null ) {
@@ -115,5 +135,34 @@ public static Image<Layer> toImage(
115135 return imageBuilder .build ();
116136 }
117137
138+ /**
139+ * Converts a map of exposed ports as strings to a list of {@link Port}s (e.g. {@code
140+ * {"1000/tcp":{}}} -> {@code Port(1000, Protocol.TCP)}).
141+ *
142+ * @param portMap the map to convert
143+ * @return a list of {@link Port}s
144+ */
145+ @ VisibleForTesting
146+ static ImmutableList <Port > portMapToList (@ Nullable Map <String , Map <?, ?>> portMap )
147+ throws BadContainerConfigurationFormatException {
148+ if (portMap == null ) {
149+ return ImmutableList .of ();
150+ }
151+ ImmutableList .Builder <Port > ports = new ImmutableList .Builder <>();
152+ for (Map .Entry <String , Map <?, ?>> entry : portMap .entrySet ()) {
153+ String port = entry .getKey ();
154+ Matcher matcher = portPattern .matcher (port );
155+ if (!matcher .matches ()) {
156+ throw new BadContainerConfigurationFormatException (
157+ "Invalid port configuration: '" + port + "'." );
158+ }
159+
160+ int portNumber = Integer .parseInt (matcher .group (1 ));
161+ String protocol = matcher .group (2 );
162+ ports .add (new Port (portNumber , Protocol .getFromString (protocol )));
163+ }
164+ return ports .build ();
165+ }
166+
118167 private JsonToImageTranslator () {}
119168}
0 commit comments