2020import com .google .cloud .tools .jib .image .json .BuildableManifestTemplate ;
2121import com .google .cloud .tools .jib .image .json .V22ManifestTemplate ;
2222import com .google .cloud .tools .jib .registry .credentials .RegistryCredentials ;
23+ import com .google .common .annotations .VisibleForTesting ;
2324import com .google .common .base .Splitter ;
25+ import com .google .common .base .Strings ;
2426import com .google .common .collect .ImmutableList ;
2527import com .google .common .collect .ImmutableMap ;
2628import java .util .ArrayList ;
2729import java .util .HashMap ;
2830import java .util .List ;
2931import java .util .Map ;
32+ import java .util .regex .Matcher ;
33+ import java .util .regex .Pattern ;
3034import javax .annotation .Nullable ;
3135import javax .lang .model .SourceVersion ;
3236
@@ -35,6 +39,13 @@ public class BuildConfiguration {
3539
3640 public static class Builder {
3741
42+ /**
43+ * Pattern used for parsing information out of exposed port configurations.
44+ *
45+ * <p>Examples: 100, 200-210, 1000/tcp, 2000/udp, 500-600/tcp
46+ */
47+ private static final Pattern portPattern = Pattern .compile ("(\\ d+)(?:-(\\ d+))?(/tcp|/udp)?" );
48+
3849 // All the parameters below are set to their default values.
3950 @ Nullable private ImageReference baseImageReference ;
4051 @ Nullable private String baseImageCredentialHelperName ;
@@ -46,6 +57,7 @@ public static class Builder {
4657 private List <String > javaArguments = new ArrayList <>();
4758 private List <String > jvmFlags = new ArrayList <>();
4859 private Map <String , String > environmentMap = new HashMap <>();
60+ private List <String > exposedPorts = new ArrayList <>();
4961 private Class <? extends BuildableManifestTemplate > targetFormat = V22ManifestTemplate .class ;
5062
5163 private BuildLogger buildLogger ;
@@ -112,6 +124,13 @@ public Builder setEnvironment(@Nullable Map<String, String> environmentMap) {
112124 return this ;
113125 }
114126
127+ public Builder setExposedPorts (@ Nullable List <String > exposedPorts ) {
128+ if (exposedPorts != null ) {
129+ this .exposedPorts = exposedPorts ;
130+ }
131+ return this ;
132+ }
133+
115134 public Builder setTargetFormat (Class <? extends BuildableManifestTemplate > targetFormat ) {
116135 this .targetFormat = targetFormat ;
117136 return this ;
@@ -154,6 +173,7 @@ public BuildConfiguration build() {
154173 ImmutableList .copyOf (javaArguments ),
155174 ImmutableList .copyOf (jvmFlags ),
156175 ImmutableMap .copyOf (environmentMap ),
176+ expandPortRanges (exposedPorts ),
157177 targetFormat );
158178
159179 case 1 :
@@ -178,6 +198,63 @@ public BuildConfiguration build() {
178198 throw new IllegalStateException (errorMessage .toString ());
179199 }
180200 }
201+
202+ /**
203+ * TODO: Move this to a class in frontend
204+ *
205+ * <p>Converts/validates a list of ports with ranges to an expanded form without ranges.
206+ *
207+ * <p>Example: ["1000/tcp", "2000-2002/tcp"] -> ["1000/tcp", "2000/tcp", "2001/tcp", "2002/tcp"]
208+ *
209+ * @param ports the list of port numbers/ranges
210+ * @return the ports as a list of integers
211+ * @throws NumberFormatException if any of the ports are in an invalid format or out of range
212+ */
213+ @ VisibleForTesting
214+ ImmutableList <String > expandPortRanges (List <String > ports ) throws NumberFormatException {
215+ ImmutableList .Builder <String > result = new ImmutableList .Builder <>();
216+
217+ for (String port : ports ) {
218+ Matcher matcher = portPattern .matcher (port );
219+
220+ if (!matcher .matches ()) {
221+ throw new NumberFormatException (
222+ "Invalid port configuration: '"
223+ + port
224+ + "'. Make sure the port is a single number or a range of two numbers separated "
225+ + "with a '-', with or without protocol specified (e.g. '<portNum>/tcp' or "
226+ + "'<portNum>/udp')." );
227+ }
228+
229+ // Parse protocol
230+ int min = Integer .parseInt (matcher .group (1 ));
231+ int max = min ;
232+ if (!Strings .isNullOrEmpty (matcher .group (2 ))) {
233+ max = Integer .parseInt (matcher .group (2 ));
234+ }
235+ String protocol = matcher .group (3 );
236+
237+ // Error if configured as 'max-min' instead of 'min-max'
238+ if (min > max ) {
239+ throw new NumberFormatException (
240+ "Invalid port range '" + port + "'; smaller number must come first." );
241+ }
242+
243+ // Warn for possibly invalid port numbers
244+ if (min < 1 || max > 65535 ) {
245+ // TODO: Add details/use HelpfulSuggestions for these warnings
246+ buildLogger .warn ("Port number '" + port + "' is out of usual range (1-65535)." );
247+ }
248+
249+ // Add all numbers in range to list
250+ for (int portNum = min ; portNum <= max ; portNum ++) {
251+ // TODO: Use a class w/ port number and protocol instead of a string
252+ result .add (portNum + (protocol == null ? "" : protocol ));
253+ }
254+ }
255+
256+ return result .build ();
257+ }
181258 }
182259
183260 /**
@@ -193,6 +270,10 @@ public static boolean isValidJavaClass(String className) {
193270 return true ;
194271 }
195272
273+ public static Builder builder (BuildLogger buildLogger ) {
274+ return new Builder (buildLogger );
275+ }
276+
196277 private final BuildLogger buildLogger ;
197278 private final ImageReference baseImageReference ;
198279 @ Nullable private final String baseImageCredentialHelperName ;
@@ -204,12 +285,9 @@ public static boolean isValidJavaClass(String className) {
204285 private final ImmutableList <String > javaArguments ;
205286 private final ImmutableList <String > jvmFlags ;
206287 private final ImmutableMap <String , String > environmentMap ;
288+ private final ImmutableList <String > exposedPorts ;
207289 private final Class <? extends BuildableManifestTemplate > targetFormat ;
208290
209- public static Builder builder (BuildLogger buildLogger ) {
210- return new Builder (buildLogger );
211- }
212-
213291 /** Instantiate with {@link Builder#build}. */
214292 private BuildConfiguration (
215293 BuildLogger buildLogger ,
@@ -223,6 +301,7 @@ private BuildConfiguration(
223301 ImmutableList <String > javaArguments ,
224302 ImmutableList <String > jvmFlags ,
225303 ImmutableMap <String , String > environmentMap ,
304+ ImmutableList <String > exposedPorts ,
226305 Class <? extends BuildableManifestTemplate > targetFormat ) {
227306 this .buildLogger = buildLogger ;
228307 this .baseImageReference = baseImageReference ;
@@ -235,6 +314,7 @@ private BuildConfiguration(
235314 this .javaArguments = javaArguments ;
236315 this .jvmFlags = jvmFlags ;
237316 this .environmentMap = environmentMap ;
317+ this .exposedPorts = exposedPorts ;
238318 this .targetFormat = targetFormat ;
239319 }
240320
@@ -310,6 +390,10 @@ public ImmutableMap<String, String> getEnvironment() {
310390 return environmentMap ;
311391 }
312392
393+ public ImmutableList <String > getExposedPorts () {
394+ return exposedPorts ;
395+ }
396+
313397 public Class <? extends BuildableManifestTemplate > getTargetFormat () {
314398 return targetFormat ;
315399 }
0 commit comments