Exposes CPU limit

pull/579/head
Gladys Nalvarte 2019-02-15 14:57:58 +01:00
rodzic 9766c95455
commit 5bf2eadcf8
3 zmienionych plików z 80 dodań i 15 usunięć

Wyświetl plik

@ -97,6 +97,28 @@ class Repo2Docker(Application):
"""
)
extra_build_kwargs = Dict(
{},
help="""
extra kwargs to limit CPU quota when building a docker image.
Dictionary that allows the user to set the desired runtime flag
to configure the amount of access to CPU resources your container has.
Reference https://docs.docker.com/config/containers/resource_constraints/#cpu
""",
config=True
)
extra_run_kwargs = Dict(
{},
help="""
extra kwargs to limit CPU quota when running a docker image.
Dictionary that allows the user to set the desired runtime flag
to configure the amount of access to CPU resources your container has.
Reference https://docs.docker.com/config/containers/resource_constraints/#cpu
""",
config=True
)
default_buildpack = Any(
PythonBuildPack,
config=True,
@ -499,8 +521,7 @@ class Repo2Docker(Application):
'mode': 'rw'
}
container = client.containers.run(
self.output_image_spec,
run_kwargs = dict(
publish_all_ports=self.all_ports,
ports=ports,
detach=True,
@ -508,6 +529,12 @@ class Repo2Docker(Application):
volumes=container_volumes,
environment=self.environment
)
run_kwargs.update(self.extra_run_kwargs)
container = client.containers.run(
self.output_image_spec, **run_kwargs)
while container.status == 'created':
time.sleep(0.5)
container.reload()
@ -636,7 +663,8 @@ class Repo2Docker(Application):
self.output_image_spec,
self.build_memory_limit,
build_args,
self.cache_from):
self.cache_from,
self.extra_build_kwargs):
if 'stream' in l:
self.log.info(l['stream'],
extra=dict(phase='building'))

Wyświetl plik

@ -9,6 +9,8 @@ import docker
import sys
import xml.etree.ElementTree as ET
from traitlets import Dict
TEMPLATE = r"""
FROM buildpack-deps:bionic
@ -463,7 +465,7 @@ class BuildPack:
appendix=self.appendix,
)
def build(self, client, image_spec, memory_limit, build_args, cache_from):
def build(self, client, image_spec, memory_limit, build_args, cache_from, extra_build_kwargs):
tarf = io.BytesIO()
tar = tarfile.open(fileobj=tarf, mode='w')
dockerfile_tarinfo = tarfile.TarInfo("Dockerfile")
@ -503,17 +505,22 @@ class BuildPack:
}
if memory_limit:
limits['memory'] = memory_limit
for line in client.build(
fileobj=tarf,
tag=image_spec,
custom_context=True,
buildargs=build_args,
decode=True,
forcerm=True,
rm=True,
container_limits=limits,
cache_from=cache_from
):
build_kwargs = dict(
fileobj=tarf,
tag=image_spec,
custom_context=True,
buildargs=build_args,
decode=True,
forcerm=True,
rm=True,
container_limits=limits,
cache_from=cache_from,
)
build_kwargs.update(extra_build_kwargs)
for line in client.build(**build_kwargs):
yield line

Wyświetl plik

@ -1,6 +1,7 @@
from tempfile import TemporaryDirectory
from unittest.mock import patch
import docker
import escapism
from repo2docker.app import Repo2Docker
@ -72,3 +73,32 @@ def test_local_dir_image_name(repo_with_content):
assert app.output_image_spec.startswith(
'r2d' + escapism.escape(upstream, escape_char='-').lower()
)
def test_build_kwargs(repo_with_content):
upstream, sha1 = repo_with_content
argv = [upstream]
app = make_r2d(argv)
app.extra_build_kwargs = {'somekey': "somevalue"}
with patch.object(docker.APIClient, 'build') as builds:
builds.return_value = []
app.build()
builds.assert_called_once()
args, kwargs = builds.call_args
assert 'somekey' in kwargs
assert kwargs['somekey'] == "somevalue"
def test_run_kwargs(repo_with_content):
upstream, sha1 = repo_with_content
argv = [upstream]
app = make_r2d(argv)
app.extra_run_kwargs = {'somekey': "somevalue"}
with patch.object(docker.DockerClient, 'containers') as containers:
app.start_container()
containers.run.assert_called_once()
args, kwargs = containers.run.call_args
assert 'somekey' in kwargs
assert kwargs['somekey'] == "somevalue"