Commit b5429cc8 authored by Daniele Venzano's avatar Daniele Venzano

Implement container resource updates

parent 51d20402
...@@ -57,3 +57,7 @@ class BaseBackend: ...@@ -57,3 +57,7 @@ class BaseBackend:
def preload_image(self, image_name: str) -> None: def preload_image(self, image_name: str) -> None:
"""Make a service image available.""" """Make a service image available."""
raise NotImplementedError raise NotImplementedError
def update_service(self, service, cores=None, memory=None):
"""Update a service reservation."""
raise NotImplementedError
...@@ -318,3 +318,23 @@ class DockerClient: ...@@ -318,3 +318,23 @@ class DockerClient:
except docker.errors.APIError as e: except docker.errors.APIError as e:
log.error('Cannot download image {}: {}'.format(image_name, e)) log.error('Cannot download image {}: {}'.format(image_name, e))
raise ZoeException('Cannot download image {}: {}'.format(image_name, e)) raise ZoeException('Cannot download image {}: {}'.format(image_name, e))
def update(self, docker_id, cpu_quota=None, mem_reservation=None, mem_limit=None):
"""Update the resource reservation for a container."""
kwargs = {}
if cpu_quota is not None:
kwargs['cpu_quota'] = cpu_quota
if mem_reservation is not None:
kwargs['mem_reservation'] = mem_reservation
if mem_limit is not None:
kwargs['mem_limit'] = mem_limit
try:
cont = self.cli.containers.get(docker_id)
except (docker.errors.NotFound, docker.errors.APIError):
return
try:
cont.update(**kwargs)
except docker.errors.APIError:
pass
...@@ -187,3 +187,18 @@ class DockerEngineBackend(zoe_master.backends.base.BaseBackend): ...@@ -187,3 +187,18 @@ class DockerEngineBackend(zoe_master.backends.base.BaseBackend):
log.debug('Image {} pre-loaded on host {} in {:.2f}s'.format(image_name, host_conf.name, time.time() - time_start)) log.debug('Image {} pre-loaded on host {} in {:.2f}s'.format(image_name, host_conf.name, time.time() - time_start))
if not one_success: if not one_success:
raise ZoeException('Cannot pull image {}'.format(image_name)) raise ZoeException('Cannot pull image {}'.format(image_name))
def update_service(self, service, cores=None, memory=None):
"""Update a service reservation."""
conf = self._get_config(service.backend_host)
engine = DockerClient(conf)
if service.backend_id is not None:
info = engine.info()
if cores is not None and cores > info['NCPU']:
cores = info['NCPU']
if memory is not None and memory > info['MemTotal']:
memory = info['MemTotal']
cpu_quota = cores * 1000000
engine.update(service.backend_id, cpu_quota=cpu_quota, mem_reservation=memory)
else:
log.error('Cannot terminate service {}, since it has not backend ID'.format(service.name))
...@@ -78,3 +78,7 @@ class KubernetesBackend(zoe_master.backends.base.BaseBackend): ...@@ -78,3 +78,7 @@ class KubernetesBackend(zoe_master.backends.base.BaseBackend):
def preload_image(self, image_name: str) -> None: def preload_image(self, image_name: str) -> None:
"""Make a service image available.""" """Make a service image available."""
raise NotImplementedError raise NotImplementedError
def update_service(self, service, cores=None, memory=None):
"""Update a service reservation."""
log.error('Reservation update not implemented in the Swarm back-end')
...@@ -85,3 +85,7 @@ class SwarmBackend(zoe_master.backends.base.BaseBackend): ...@@ -85,3 +85,7 @@ class SwarmBackend(zoe_master.backends.base.BaseBackend):
def preload_image(self, image_name: str) -> None: def preload_image(self, image_name: str) -> None:
"""Make a service image available.""" """Make a service image available."""
raise NotImplementedError raise NotImplementedError
def update_service(self, service, cores=None, memory=None):
"""Update a service reservation."""
log.error('Reservation update not implemented in the Swarm back-end')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment