Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Fix for IP per task in Marathon 1.3+.
Browse files Browse the repository at this point in the history
This should resolve #313.
  • Loading branch information
brndnmtthws committed Sep 30, 2016
1 parent 0e1bf02 commit 7155707
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ def get_service_ports(self, app):
:return: The list of ports. Note that if auto-assigning and ports
become exhausted, a port may be returned as None.
"""
# Are we using 'USER' network?
if is_user_network(app):
# Here we must use portMappings
portMappings = app.get('container', {})\
.get('docker', {})\
.get('portMappings', [])
ports = filter(lambda p: p is not None,
map(lambda p: p.get('servicePort', None),
portMappings))
return list(ports)

ports = app.get('ports', [])
if 'portDefinitions' in app:
ports = filter(lambda p: p is not None,
Expand All @@ -119,8 +130,6 @@ def get_service_ports(self, app):
ports = list(ports) # wtf python?
if not ports and is_ip_per_task(app) and self.can_assign \
and len(app['tasks']) > 0:
logger.warning("Auto assigning service port for "
"IP-per-container task")
task = app['tasks'][0]
_, task_ports = get_task_ip_and_ports(app, task)
if task_ports is not None:
Expand Down Expand Up @@ -158,6 +167,17 @@ def is_ip_per_task(app):
return app.get('ipAddress') is not None


def is_user_network(app):
"""
Returns True if container network mode is set to USER
:param app: The application to check.
:return: True if using USER network, False otherwise.
"""
return app.get('container', {})\
.get('docker', {})\
.get('network', '') == 'USER'


def get_task_ip_and_ports(app, task):
"""
Return the IP address and list of ports used to access a task. For a
Expand All @@ -174,17 +194,29 @@ def get_task_ip_and_ports(app, task):
# case there are no discovery ports. At the moment, Mesos only supports a
# single IP address, so just take the first IP in the list.
if is_ip_per_task(app):
logger.debug("Using IP per container")
task_ip_addresses = task.get('ipAddresses')
if not task_ip_addresses:
logger.warning("Task %s does not yet have an ip address allocated",
task['id'])
return None, None

task_ip = task_ip_addresses[0]['ipAddress']

discovery = app['ipAddress'].get('discovery', {})
task_ports = [int(port['number'])
for port in discovery.get('ports', [])]
# Are we using 'USER' network?
if is_user_network(app):
logger.debug("Using IP per container")
# in this case, we pull the port from portMappings
portMappings = app.get('container', {})\
.get('docker', {})\
.get('portMappings', [])
ports = filter(lambda p: p is not None,
map(lambda p: p.get('servicePort', None),
portMappings))
task_ports = list(ports)
else:
discovery = app['ipAddress'].get('discovery', {})
task_ports = [int(port['number'])
for port in discovery.get('ports', [])]
else:
logger.debug("Using host port mapping")
task_ports = task.get('ports', [])
Expand Down

0 comments on commit 7155707

Please sign in to comment.