Search in sources :

Example 6 with Port

use of com.google.cloud.tools.jib.api.buildplan.Port in project jib by GoogleContainerTools.

the class Ports method parse.

/**
 * Converts/validates a list of strings representing port ranges to an expanded list of {@link
 * Port}s.
 *
 * <p>For example: ["1000", "2000-2002"] will expand to a list of {@link Port}s with the port
 * numbers [1000, 2000, 2001, 2002]
 *
 * @param ports the list of port numbers/ranges, with an optional protocol separated by a '/'
 *     (defaults to TCP if missing).
 * @return the ports as a list of {@link Port}
 * @throws NumberFormatException if any of the ports are in an invalid format or out of range
 */
public static Set<Port> parse(List<String> ports) throws NumberFormatException {
    Set<Port> result = new HashSet<>();
    for (String port : ports) {
        Matcher matcher = portPattern.matcher(port);
        if (!matcher.matches()) {
            throw new NumberFormatException("Invalid port configuration: '" + port + "'. Make sure the port is a single number or a range of two numbers separated " + "with a '-', with or without protocol specified (e.g. '<portNum>/tcp' or " + "'<portNum>/udp').");
        }
        // Parse protocol
        int min = Integer.parseInt(matcher.group(1));
        int max = min;
        if (!Strings.isNullOrEmpty(matcher.group(2))) {
            max = Integer.parseInt(matcher.group(2));
        }
        String protocol = matcher.group(3);
        // Error if configured as 'max-min' instead of 'min-max'
        if (min > max) {
            throw new NumberFormatException("Invalid port range '" + port + "'; smaller number must come first.");
        }
        // Warn for possibly invalid port numbers
        if (min < 1 || max > 65535) {
            throw new NumberFormatException("Port number '" + port + "' is out of usual range (1-65535).");
        }
        for (int portNumber = min; portNumber <= max; portNumber++) {
            result.add(Port.parseProtocol(portNumber, protocol));
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) Port(com.google.cloud.tools.jib.api.buildplan.Port) HashSet(java.util.HashSet)

Example 7 with Port

use of com.google.cloud.tools.jib.api.buildplan.Port in project jib by GoogleContainerTools.

the class JsonToImageTranslator method portMapToSet.

/**
 * Converts a map of exposed ports as strings to a set of {@link Port}s (e.g. {@code
 * {"1000/tcp":{}}} -> {@code Port(1000, Protocol.TCP)}).
 *
 * @param portMap the map to convert
 * @return a set of {@link Port}s
 */
@VisibleForTesting
static ImmutableSet<Port> portMapToSet(@Nullable Map<String, Map<String, String>> portMap) throws BadContainerConfigurationFormatException {
    if (portMap == null) {
        return ImmutableSet.of();
    }
    ImmutableSet.Builder<Port> ports = new ImmutableSet.Builder<>();
    for (Map.Entry<String, Map<String, String>> entry : portMap.entrySet()) {
        String port = entry.getKey();
        Matcher matcher = PORT_PATTERN.matcher(port);
        if (!matcher.matches()) {
            throw new BadContainerConfigurationFormatException("Invalid port configuration: '" + port + "'.");
        }
        int portNumber = Integer.parseInt(matcher.group("portNum"));
        String protocol = matcher.group("protocol");
        ports.add(Port.parseProtocol(portNumber, protocol));
    }
    return ports.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Matcher(java.util.regex.Matcher) Port(com.google.cloud.tools.jib.api.buildplan.Port) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Port (com.google.cloud.tools.jib.api.buildplan.Port)7 Matcher (java.util.regex.Matcher)4 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)3 Path (java.nio.file.Path)3 Instant (java.time.Instant)3 Map (java.util.Map)3 CredentialRetriever (com.google.cloud.tools.jib.api.CredentialRetriever)2 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)2 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 LayerConfiguration (com.google.cloud.tools.jib.api.LayerConfiguration)1 File (java.io.File)1 IOException (java.io.IOException)1 MAX_VALUE (java.lang.Long.MAX_VALUE)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1