-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the function to get mesos slaves
- Loading branch information
Showing
3 changed files
with
82 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
import types | ||
|
||
import pytest | ||
|
||
from marathon.models.constraint import MarathonConstraint | ||
|
||
from ucrspawner import mesosslave | ||
from ucrspawner.exceptions import UCRSpawnerException | ||
|
||
def test_match(monkeypatch): | ||
mesos_master_response = json.loads('{"id":"40237088-f509-46a4-a5f1-c8f88ca8e03f-S0","hostname":"mesos-slave","port":5051,"attributes":{"foo":123.0,"bar":"wow"},"pid":"slave(1)@172.24.0.5:5051","registered_time":1518339392.10703,"resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"used_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":0.0},"offered_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":0.0},"reserved_resources":{},"unreserved_resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"active":true,"version":"1.4.0","capabilities":["MULTI_ROLE","HIERARCHICAL_ROLE","RESERVATION_REFINEMENT"],"reserved_resources_full":{},"unreserved_resources_full":[{"name":"cpus","type":"SCALAR","scalar":{"value":2.0},"role":"*"},{"name":"mem","type":"SCALAR","scalar":{"value":999.0},"role":"*"},{"name":"disk","type":"SCALAR","scalar":{"value":69716.0},"role":"*"},{"name":"ports","type":"RANGES","ranges":{"range":[{"begin":31000,"end":32000}]},"role":"*"}],"used_resources_full":[],"offered_resources_full":[]}') | ||
|
||
slave = mesosslave.MesosSlave(mesos_master_response) | ||
|
||
assert slave.match(MarathonConstraint('foo', 'LIKE', '123')) | ||
assert not slave.match(MarathonConstraint('foo', 'UNLIKE', '123')) | ||
assert slave.match(MarathonConstraint('bar', 'LIKE', 'wo[w]')) | ||
assert not slave.match(MarathonConstraint('bar', 'LIKE', 'wof')) | ||
with pytest.raises(UCRSpawnerException) as excinfo: | ||
slave.match(MarathonConstraint('foo', 'UNKNOWN', '')) | ||
assert str(excinfo.value) == 'Unsupported constraint operator: UNKNOWN' |
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,38 @@ | ||
import re | ||
|
||
from .exceptions import UCRSpawnerException | ||
|
||
|
||
class MesosSlave(): | ||
def __init__(self, params): | ||
self.hostname = params['hostname'] | ||
self.attributes = params['attributes'] | ||
|
||
self.disk = params['resources']['disk'] | ||
self.cpus = params['resources']['cpus'] | ||
self.mem = params['resources']['mem'] | ||
self.disk = params['resources']['disk'] | ||
self.gpus = int(params['resources']['gpus']) | ||
|
||
self.available_cpus = params['unreserved_resources']['cpus'] | ||
self.available_mem = params['unreserved_resources']['mem'] | ||
self.available_disk = params['unreserved_resources']['disk'] | ||
self.available_gpus = int(params['unreserved_resources']['gpus']) | ||
|
||
def is_available(self): | ||
return self.available_cpus > 0 and \ | ||
self.available_mem > 0 and \ | ||
self.available_disk > 0 and \ | ||
(self.gpus == 0 or self.available_gpus > 0) | ||
|
||
def match(self, constraint): | ||
if constraint.field == 'hostname': | ||
value = self.hostname | ||
else: | ||
value = str(self.attributes[constraint.field]) | ||
if constraint.operator == 'LIKE': | ||
return re.compile(constraint.value).match(value) is not None | ||
elif constraint.operator == 'UNLIKE': | ||
return re.compile(constraint.value).match(value) is None | ||
else: | ||
raise UCRSpawnerException('Unsupported constraint operator: %s' % constraint.operator) |
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