Skip to content

Commit

Permalink
Merge pull request pmsipilot#31 from pmsipilot/ip-port-mapping
Browse files Browse the repository at this point in the history
fix: Support IP address in port mappings
  • Loading branch information
jubianchi authored Mar 6, 2018
2 parents aa9cff3 + a72bc1e commit 78c8303
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr

if (false === ((bool) ($flags & WITHOUT_PORTS))) {
foreach ($definition['ports'] ?? [] as $port) {
list($host, $container, $proto) = explodeMapping($port);
list($target, $host, $container, $proto) = explodePortMapping($port);

addRelation(
addPort($graph, (int) $host, $proto),
addPort($graph, (int) $host, $proto, $target),
$graph->getVertex($service),
'ports',
$host !== $container ? $container : null
Expand Down Expand Up @@ -390,13 +390,15 @@ function addService(Graph $graph, string $service, string $type = null)
*
* @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)) {
return $graph->getVertex($port);
$target = $target ? $target.':' : null;

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.proto', $proto ?: 'tcp');

Expand Down Expand Up @@ -494,7 +496,7 @@ function addRelation(Vertex $from, Vertex $to, string $type, string $alias = nul
*
* @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
*/
function explodeMapping($mapping): array
Expand All @@ -510,3 +512,30 @@ function explodeMapping($mapping): array

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];
}

0 comments on commit 78c8303

Please sign in to comment.