-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsonnet): add port conflict solver as jsonnet postprocessor
docker-compose configuration generated by jsonnet is now postprocessed by a python function to dedupe port mappings of the same `published` value. When a `published` is conflicting a previously defined one, it is incremented until the conflict is solved, making all published ports from all services distinct.
- Loading branch information
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from collections import defaultdict | ||
|
||
from ddb.feature.docker.lib.compose.config.types import ServicePort | ||
|
||
|
||
def apply_resolve_ports_conflicts(compose): | ||
""" | ||
Resolve ports conflicts in docker compose configuration. | ||
""" | ||
published = defaultdict(set) | ||
|
||
if 'services' in compose: # pylint:disable=too-many-nested-blocks | ||
for service in compose['services'].values(): | ||
if 'ports' in service: | ||
service_ports = service['ports'] | ||
for index, port in enumerate(tuple(service_ports)): | ||
parsed_ports = ServicePort.parse(port) | ||
if len(parsed_ports) == 1: | ||
parsed_port = parsed_ports[0] | ||
new_published = parsed_port.published | ||
while new_published in published[parsed_port.protocol]: | ||
new_published = new_published + 1 | ||
published[parsed_port.protocol].add(new_published) | ||
if parsed_port.published != new_published: | ||
if isinstance(port, str): | ||
service_port_data = dict(parsed_port._asdict()) | ||
service_port_data['published'] = new_published | ||
new_port = ServicePort(**service_port_data) | ||
port = new_port.legacy_repr() | ||
if not new_port.protocol and port.endswith('/tcp'): | ||
port = port[:-4] | ||
else: | ||
port['published'] = str(new_published) | ||
service_ports[index] = port | ||
|
||
return compose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
66 changes: 66 additions & 0 deletions
66
tests/it/test_jsonnet_it.data/jsonnet-resolve-ports-conflicts/docker-compose.expected.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
networks: {} | ||
services: | ||
db_a: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2933:5433 | ||
restart: 'no' | ||
db_b: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2932:5432 | ||
restart: 'no' | ||
db_c: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2934:5432 | ||
restart: 'no' | ||
db_d: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2931:5431 | ||
restart: 'no' | ||
db_e: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2938:5438 | ||
restart: 'no' | ||
db_f: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2935:5433 | ||
restart: 'no' | ||
db_g: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2932:5432/udp | ||
restart: 'no' | ||
db_h: | ||
image: postgres | ||
init: true | ||
ports: | ||
- 2933:5432/udp | ||
restart: 'no' | ||
db_i: | ||
image: postgres | ||
init: true | ||
ports: | ||
- published: '2939' | ||
target: '5438' | ||
restart: 'no' | ||
db_j: | ||
image: postgres | ||
init: true | ||
ports: | ||
- published: '2940' | ||
target: '5438' | ||
restart: 'no' | ||
version: '3.7' | ||
volumes: {} |
20 changes: 20 additions & 0 deletions
20
tests/it/test_jsonnet_it.data/jsonnet-resolve-ports-conflicts/docker-compose.yml.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local ddb = import 'ddb.docker.libjsonnet'; | ||
|
||
ddb.Compose({ | ||
services: { | ||
db_a: ddb.Image("postgres") + ddb.Expose("5433"), | ||
db_b: ddb.Image("postgres") + ddb.Expose("5432"), | ||
db_c: ddb.Image("postgres") + ddb.Expose("5432"), | ||
db_d: ddb.Image("postgres") + ddb.Expose("5431"), | ||
db_e: ddb.Image("postgres") + ddb.Expose("5438"), | ||
db_f: ddb.Image("postgres") + ddb.Expose("5433"), | ||
db_g: ddb.Image("postgres") + ddb.Expose("5432", protocol="udp"), | ||
db_h: ddb.Image("postgres") + ddb.Expose("5432", protocol="udp"), | ||
db_i: ddb.Image("postgres") + { | ||
ports: [{published: '2938', target: '5438'}] | ||
}, | ||
db_j: ddb.Image("postgres") + { | ||
ports: [{published: '2938', target: '5438'}] | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters