Limit the amount of disk of container

The patch supports the storage_opt size=xxx when run/create
a container. If user set this option, it can limit the size
of the hard disk per container.

Change-Id: Iafcf9d1a4a2b7ccb70998d8797eada29ed3315f5
Implements: blueprint limit-container-diskspace
This commit is contained in:
Feng Shengqin 2018-01-26 17:11:10 +08:00
parent 9a51618f48
commit a6f5a4602b
4 changed files with 30 additions and 5 deletions

View File

@ -158,8 +158,13 @@ class CreateContainer(command.ShowOne):
parser.add_argument(
'--hostname',
metavar='<hostname>',
help='Container host name'
)
help='Container host name')
parser.add_argument(
'--disk',
metavar='<disk>',
type=int,
default=None,
help='The disk size in GiB for per container.')
return parser
def take_action(self, parsed_args):
@ -189,6 +194,7 @@ class CreateContainer(command.ShowOne):
opts['mounts'] = zun_utils.parse_mounts(parsed_args.mount)
opts['runtime'] = parsed_args.runtime
opts['hostname'] = parsed_args.hostname
opts['disk'] = parsed_args.disk
opts = zun_utils.remove_null_parms(**opts)
container = client.containers.create(**opts)
@ -713,8 +719,13 @@ class RunContainer(command.ShowOne):
parser.add_argument(
'--hostname',
metavar='<hostname>',
help='Container host name'
)
help='Container host name')
parser.add_argument(
'--disk',
metavar='<disk>',
type=int,
default=None,
help='The disk size in GiB for per container.')
return parser
def take_action(self, parsed_args):
@ -744,6 +755,7 @@ class RunContainer(command.ShowOne):
opts['mounts'] = zun_utils.parse_mounts(parsed_args.mount)
opts['runtime'] = parsed_args.runtime
opts['hostname'] = parsed_args.hostname
opts['disk'] = parsed_args.disk
opts = zun_utils.remove_null_parms(**opts)
container = client.containers.run(**opts)

View File

@ -35,6 +35,7 @@ CONTAINER1 = {'id': '1234',
'auto_remove': True,
'runtime': 'runc',
'hostname': 'testhost',
'disk': '20',
}
CONTAINER2 = {'id': '1235',

View File

@ -23,7 +23,7 @@ CREATION_ATTRIBUTES = ['name', 'image', 'command', 'cpu', 'memory',
'environment', 'workdir', 'labels', 'image_pull_policy',
'restart_policy', 'interactive', 'image_driver',
'security_groups', 'hints', 'nets', 'auto_remove',
'runtime', 'hostname', 'mounts']
'runtime', 'hostname', 'mounts', 'disk']
class Container(base.Resource):

View File

@ -126,6 +126,11 @@ def _show_container(container):
metavar='<hostname>',
default=None,
help='Container host name')
@utils.arg('--disk',
metavar='<disk>',
type=int,
default=None,
help='The disk size in GiB for per container.')
def do_create(cs, args):
"""Create a container."""
opts = {}
@ -144,6 +149,7 @@ def do_create(cs, args):
opts['mounts'] = zun_utils.parse_mounts(args.mount)
opts['runtime'] = args.runtime
opts['hostname'] = args.hostname
opts['disk'] = args.disk
if args.security_group:
opts['security_groups'] = args.security_group
@ -527,6 +533,11 @@ def do_kill(cs, args):
metavar='<hostname>',
default=None,
help='Container hostname')
@utils.arg('--disk',
metavar='<disk>',
type=int,
default=None,
help='The disk size in GiB for per container.')
def do_run(cs, args):
"""Run a command in a new container."""
opts = {}
@ -545,6 +556,7 @@ def do_run(cs, args):
opts['mounts'] = zun_utils.parse_mounts(args.mount)
opts['runtime'] = args.runtime
opts['hostname'] = args.hostname
opts['disk'] = args.disk
if args.security_group:
opts['security_groups'] = args.security_group