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

Commit

Permalink
Update for Marathon 1.5 API Changes (#476)
Browse files Browse the repository at this point in the history
update for marathon 1.5 api changes
  • Loading branch information
drewkerrigan authored Jul 21, 2017
1 parent 4e196f2 commit 1303b53
Show file tree
Hide file tree
Showing 9 changed files with 853 additions and 188 deletions.
2 changes: 2 additions & 0 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,8 @@ def compareMapFile(map_file, map_string):


def get_health_check(app, portIndex):
if 'healthChecks' not in app:
return None
for check in app['healthChecks']:
if check.get('port'):
return check
Expand Down
34 changes: 34 additions & 0 deletions tests/1-nginx-marathon1.5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"id": "nginx",
"container": {
"type": "DOCKER",
"docker": {
"image": "brndnmtthws/nginx-echo-sleep",
"forcePullImage":true
},
"portMappings": [
{ "hostPort": 0, "containerPort": 8080, "servicePort": 10000 }
]
},
"networks": [
{ "mode": "container/bridge" }
],
"instances": 5,
"cpus": 0.1,
"mem": 65,
"healthChecks": [{
"protocol": "MESOS_HTTP",
"path": "/",
"portIndex": 0,
"timeoutSeconds": 15,
"gracePeriodSeconds": 15,
"intervalSeconds": 3,
"maxConsecutiveFailures": 10
}],
"labels":{
"HAPROXY_DEPLOYMENT_GROUP":"nginx",
"HAPROXY_DEPLOYMENT_ALT_PORT":"10001",
"HAPROXY_GROUP":"external"
},
"acceptedResourceRoles":["*", "slave_public"]
}
87 changes: 87 additions & 0 deletions tests/marathon15_apps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"apps": [
{
"id": "/pywebserver",
"backoffFactor": 1.15,
"backoffSeconds": 1,
"cmd": "echo \"host $HOST and port $PORT\" > index.html && python -m http.server 80",
"container": {
"type": "DOCKER",
"docker": {
"forcePullImage": false,
"image": "python:3",
"parameters": [],
"privileged": false
},
"volumes": [],
"portMappings": [
{
"containerPort": 80,
"hostPort": 0,
"labels": {},
"name": "test",
"protocol": "tcp",
"servicePort": 10101
}
]
},
"cpus": 0.1,
"disk": 0,
"executor": "",
"instances": 1,
"labels": {
"HAPROXY_GROUP": "external",
"HAPROXY_0_VHOST": "myvhost.com"
},
"maxLaunchDelaySeconds": 3600,
"mem": 128,
"gpus": 0,
"networks": [
{
"mode": "container/bridge"
}
],
"requirePorts": false,
"upgradeStrategy": {
"maximumOverCapacity": 1,
"minimumHealthCapacity": 1
},
"version": "2017-07-19T17:34:41.967Z",
"versionInfo": {
"lastScalingAt": "2017-07-19T17:34:41.967Z",
"lastConfigChangeAt": "2017-07-19T17:34:41.967Z"
},
"killSelection": "YOUNGEST_FIRST",
"unreachableStrategy": {
"inactiveAfterSeconds": 300,
"expungeAfterSeconds": 600
},
"tasksStaged": 0,
"tasksRunning": 1,
"tasksHealthy": 0,
"tasksUnhealthy": 0,
"deployments": [],
"tasks": [
{
"ipAddresses": [
{
"ipAddress": "172.17.0.2",
"protocol": "IPv4"
}
],
"stagedAt": "2017-07-19T17:34:43.039Z",
"state": "TASK_RUNNING",
"ports": [
1565
],
"startedAt": "2017-07-19T17:35:15.654Z",
"version": "2017-07-19T17:34:41.967Z",
"id": "pywebserver.8cad6a69-6ca8-11e7-beb2-0e2beceebfcc",
"appId": "/pywebserver",
"slaveId": "db7b40e2-791c-445f-b373-183e2a648a86-S1",
"host": "10.0.2.148"
}
]
}
]
}
57 changes: 57 additions & 0 deletions tests/test_marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,63 @@ def test_config_simple_app_balance(self):
'''
self.assertMultiLineEqual(config, expected)

def test_bridge_app_marathon15(self):
with open('tests/marathon15_apps.json') as data_file:
apps = json.load(data_file)

class Marathon:
def __init__(self, data):
self.data = data

def list(self):
return self.data

def health_check(self):
return True

def strict_mode(self):
return False

groups = ['external']
bind_http_https = True
ssl_certs = ""
templater = marathon_lb.ConfigTemplater()
apps = marathon_lb.get_apps(Marathon(apps['apps']))
config = marathon_lb.config(apps, groups, bind_http_https,
ssl_certs, templater)
expected = self.base_config + '''
frontend marathon_http_in
bind *:80
mode http
acl host_myvhost_com_pywebserver hdr(host) -i myvhost.com
use_backend pywebserver_10101 if host_myvhost_com_pywebserver
frontend marathon_http_appid_in
bind *:9091
mode http
acl app__pywebserver hdr(x-marathon-app-id) -i /pywebserver
use_backend pywebserver_10101 if app__pywebserver
frontend marathon_https_in
bind *:443 ssl crt /etc/ssl/cert.pem
mode http
use_backend pywebserver_10101 if { ssl_fc_sni myvhost.com }
frontend pywebserver_10101
bind *:10101
mode http
use_backend pywebserver_10101
backend pywebserver_10101
balance roundrobin
mode http
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server 10_0_2_148_1565 10.0.2.148:1565
'''
self.assertMultiLineEqual(config, expected)

def test_zdd_app(self):
with open('tests/zdd_apps.json') as data_file:
zdd_apps = json.load(data_file)
Expand Down
74 changes: 74 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,48 @@ def test_get_task_ip_and_ports_ip_per_task_no_ip_marathon13(self):

self.assertEquals(result, expected)

def test_get_task_ip_and_ports_ip_per_task_marathon15(self):
app = {
'container': {
'type': 'DOCKER',
'docker': {
'image': 'nginx'
},
'portMappings': [
{
'containerPort': 80,
'servicePort': 10000,
},
{
'containerPort': 81,
'servicePort': 10001,
},
]
},
'networks': [
{
'mode': 'container',
'name': 'dcos'
}
]
}
task = {
"id": "testtaskid",
"ipAddresses": [{"ipAddress": "1.2.3.4"}]
}

result = utils.get_task_ip_and_ports(app, task)
expected = ("1.2.3.4", [80, 81])
self.assertEquals(result, expected)

task_no_ip = {
"id": "testtaskid",
}

result = utils.get_task_ip_and_ports(app, task_no_ip)
expected = (None, None)
self.assertEquals(result, expected)

def test_get_task_ip_and_ports_portmapping_null(self):
app = {
'ipAddress': {},
Expand Down Expand Up @@ -289,6 +331,38 @@ def test_ip_per_task_marathon13(self):
self.assertEquals(self.assigner.get_service_ports(app),
[10000, 10001])

def test_ip_per_task_marathon15(self):
app = {
'container': {
'type': 'DOCKER',
'docker': {
'image': 'nginx'
},
'portMappings': [
{
'containerPort': 80,
'servicePort': 10000,
},
{
'containerPort': 81,
'servicePort': 10001,
},
],
},
'networks': [
{
'mode': 'container',
'name': 'dcos'
}
],
'tasks': [{
"id": "testtaskid",
"ipAddresses": [{"ipAddress": "1.2.3.4"}]
}],
}
self.assertEquals(self.assigner.get_service_ports(app),
[10000, 10001])

def test_ip_per_task_portMappings_null(self):
app = {
'ipAddress': {},
Expand Down
Loading

0 comments on commit 1303b53

Please sign in to comment.