Merge pull request #31 from pmsipilot/ip-port-mapping

fix: Support IP address in port mappings
This commit is contained in:
Julien BIANCHI 2018-03-06 09:21:39 +01:00 committed by GitHub
commit 78c83037ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -330,10 +330,10 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
if (false === ((bool) ($flags & WITHOUT_PORTS))) { if (false === ((bool) ($flags & WITHOUT_PORTS))) {
foreach ($definition['ports'] ?? [] as $port) { foreach ($definition['ports'] ?? [] as $port) {
list($host, $container, $proto) = explodeMapping($port); list($target, $host, $container, $proto) = explodePortMapping($port);
addRelation( addRelation(
addPort($graph, (int) $host, $proto), addPort($graph, (int) $host, $proto, $target),
$graph->getVertex($service), $graph->getVertex($service),
'ports', 'ports',
$host !== $container ? $container : null $host !== $container ? $container : null
@ -390,13 +390,15 @@ function addService(Graph $graph, string $service, string $type = null)
* *
* @return Vertex * @return Vertex
*/ */
function addPort(Graph $graph, int $port, string $proto = null) function addPort(Graph $graph, int $port, string $proto = null, string $target = null)
{ {
if (true === $graph->hasVertex($port)) { $target = $target ? $target.':' : null;
return $graph->getVertex($port);
if (true === $graph->hasVertex($target.$port)) {
return $graph->getVertex($target.$port);
} }
$vertex = $graph->createVertex($port); $vertex = $graph->createVertex($target.$port);
$vertex->setAttribute('docker_compose.type', 'port'); $vertex->setAttribute('docker_compose.type', 'port');
$vertex->setAttribute('docker_compose.proto', $proto ?: 'tcp'); $vertex->setAttribute('docker_compose.proto', $proto ?: 'tcp');
@ -494,7 +496,7 @@ function addRelation(Vertex $from, Vertex $to, string $type, string $alias = nul
* *
* @param string $mapping A docker mapping (<from>[:<to>]) * @param string $mapping A docker mapping (<from>[:<to>])
* *
* @return array An 2 items array containing the parts of the mapping. * @return array An 2 or 3 items array containing the parts of the mapping.
* If the mapping does not specify a second part, the first one will be repeated * If the mapping does not specify a second part, the first one will be repeated
*/ */
function explodeMapping($mapping): array function explodeMapping($mapping): array
@ -510,3 +512,30 @@ function explodeMapping($mapping): array
return [$parts[0], $subparts[0], $subparts[1] ?? null]; return [$parts[0], $subparts[0], $subparts[1] ?? null];
} }
/**
* @internal
*
* @param string $mapping A docker mapping (<from>[:<to>])
*
* @return array An 2 or 3 items array containing the parts of the mapping.
* If the mapping does not specify a second part, the first one will be repeated
*/
function explodePortMapping($mapping): array
{
$parts = explode(':', $mapping);
if (count($parts) < 3) {
$target = null;
$host = $parts[0];
$container = $parts[1] ?? $parts[0];
} else {
$target = $parts[0];
$host = $parts[1];
$container = $parts[2];
}
$subparts = array_values(array_filter(explode('/', $container)));
return [$target, $host, $subparts[0], $subparts[1] ?? null];
}