Skip to content

Commit

Permalink
container: T2216: use defaultValue XML definition to define port prot…
Browse files Browse the repository at this point in the history
…ocol

Instead of hardcoding the default protocol as TCP in the Python script we shall
use the XML based defaultValue approach instead. This also automatically exports
the default to the CLI completion helper.
  • Loading branch information
c-po committed Dec 25, 2022
1 parent 1ef7ba7 commit a0d3c57
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 10 additions & 1 deletion interface-definitions/container.xml.in
Expand Up @@ -207,14 +207,23 @@
</leafNode>
<leafNode name="protocol">
<properties>
<help>Protocol tcp/udp</help>
<help>Transport protocol used for port mapping</help>
<completionHelp>
<list>tcp udp</list>
</completionHelp>
<valueHelp>
<format>tcp</format>
<description>Use Transmission Control Protocol for given port</description>
</valueHelp>
<valueHelp>
<format>udp</format>
<description>Use User Datagram Protocol for given port</description>
</valueHelp>
<constraint>
<regex>(tcp|udp)</regex>
</constraint>
</properties>
<defaultValue>tcp</defaultValue>
</leafNode>
</children>
</tagNode>
Expand Down
18 changes: 12 additions & 6 deletions src/conf_mode/container.py
Expand Up @@ -73,9 +73,19 @@ def get_config(config=None):
# Merge per-container default values
if 'name' in container:
default_values = defaults(base + ['name'])
if 'port' in default_values:
del default_values['port']
for name in container['name']:
container['name'][name] = dict_merge(default_values, container['name'][name])

# XXX: T2665: we can not safely rely on the defaults() when there are
# tagNodes in place, it is better to blend in the defaults manually.
if 'port' in container['name'][name]:
for port in container['name'][name]['port']:
default_values = defaults(base + ['name', 'port'])
container['name'][name]['port'][port] = dict_merge(
default_values, container['name'][name]['port'][port])

# Delete container network, delete containers
tmp = node_changed(conf, base + ['network'])
if tmp: container.update({'network_remove' : tmp})
Expand Down Expand Up @@ -242,14 +252,10 @@ def generate_run_arguments(name, container_config):
if 'port' in container_config:
protocol = ''
for portmap in container_config['port']:
if 'protocol' in container_config['port'][portmap]:
protocol = container_config['port'][portmap]['protocol']
protocol = f'/{protocol}'
else:
protocol = '/tcp'
protocol = container_config['port'][portmap]['protocol']
sport = container_config['port'][portmap]['source']
dport = container_config['port'][portmap]['destination']
port += f' -p {sport}:{dport}{protocol}'
port += f' -p {sport}:{dport}/{protocol}'

# Bind volume
volume = ''
Expand Down

0 comments on commit a0d3c57

Please sign in to comment.