in container_agent/run_containers.py [0:0]
def LoadPorts(ports_spec, ctr_name):
"""Process a "ports" block of config and return a list of ports."""
# TODO(thockin): could be a dict of name -> Port
all_ports = []
all_port_names = []
all_host_port_mappings = set()
for port_index, port_spec in enumerate(ports_spec):
if 'name' in port_spec:
port_name = port_spec['name']
if not IsRfc1035Name(port_name):
raise ConfigException(
'containers[%s].ports[%d].name is invalid: %s'
% (ctr_name, port_index, port_name))
if port_name in all_port_names:
raise ConfigException(
'containers[%s].ports[%d].name is not unique: %s'
% (ctr_name, port_index, port_name))
all_port_names.append(port_name)
else:
port_name = str(port_index)
if 'containerPort' not in port_spec:
raise ConfigException(
'containers[%s].ports[%s] has no containerPort'
% (ctr_name, port_name))
ctr_port = port_spec['containerPort']
if not IsValidPort(ctr_port):
raise ConfigException(
'containers[%s].ports[%s].containerPort is invalid: %d'
% (ctr_name, port_name, ctr_port))
host_ip = port_spec.get('hostIp', '')
if host_ip and not IsValidIpv4Address(host_ip):
raise ConfigException(
'containers[%s].ports[%s].hostIp is invalid: %s'
% (ctr_name, port_name, host_ip))
host_port = port_spec.get('hostPort', ctr_port)
if not IsValidPort(host_port):
raise ConfigException(
'containers[%s].ports[%s].hostPort is invalid: %d'
% (ctr_name, port_name, host_port))
proto = port_spec.get('protocol', 'TCP')
if not IsValidProtocol(proto):
raise ConfigException(
'containers[%s].ports[%s].protocol is invalid: %s'
% (ctr_name, port_name, proto))
host_port_mapping = (host_ip, host_port, proto)
if host_port_mapping in all_host_port_mappings:
raise ConfigException(
'containers[%s].ports[%s].(hostIp,hostPort,protocol) \
is not unique: %s %d %s'
% (ctr_name, port_name, host_ip, host_port, proto))
all_host_port_mappings.add(host_port_mapping)
all_ports.append((host_ip, host_port, ctr_port, ProtocolString(proto)))
return all_ports