From 945faab54d27924fe78461a29ddb3477135c9165 Mon Sep 17 00:00:00 2001 From: aiordache Date: Fri, 10 Jul 2020 11:39:39 +0200 Subject: [PATCH] parse deploy.resources Signed-off-by: aiordache --- compose/config/config.py | 29 -------------- compose/project.py | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index a3110682c..c5791e153 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -932,35 +932,6 @@ def finalize_service(service_config, service_names, version, environment, return normalize_v1_service_format(service_dict) -def translate_resource_keys_to_container_config(resources_dict, service_dict): - if 'limits' in resources_dict: - service_dict['mem_limit'] = resources_dict['limits'].get('memory') - if 'cpus' in resources_dict['limits']: - service_dict['cpus'] = float(resources_dict['limits']['cpus']) - if 'reservations' in resources_dict: - service_dict['mem_reservation'] = resources_dict['reservations'].get('memory') - if 'cpus' in resources_dict['reservations']: - return ['resources.reservations.cpus'] - return [] - - -def convert_restart_policy(name): - try: - return { - 'any': 'always', - 'none': 'no', - 'on-failure': 'on-failure' - }[name] - except KeyError: - raise ConfigurationError('Invalid restart policy "{}"'.format(name)) - - -def convert_credential_spec_to_security_opt(credential_spec): - if 'file' in credential_spec: - return 'file://{file}'.format(file=credential_spec['file']) - return 'registry://{registry}'.format(registry=credential_spec['registry']) - - def normalize_v1_service_format(service_dict): if 'log_driver' in service_dict or 'log_opt' in service_dict: if 'logging' not in service_dict: diff --git a/compose/project.py b/compose/project.py index 216615aee..c02a1d8dd 100644 --- a/compose/project.py +++ b/compose/project.py @@ -125,6 +125,16 @@ class Project(object): service_dict['scale'] = project.get_service_scale(service_dict) + service_dict = translate_credential_spec_to_security_opt(service_dict) + service_dict, ignored_keys = translate_deploy_keys_to_container_config( + service_dict + ) + if ignored_keys: + log.warning( + 'The following deploy sub-keys are not supported and have' + ' been ignored: {}'.format(', '.join(ignored_keys)) + ) + project.services.append( Service( service_dict.pop('name'), @@ -796,6 +806,81 @@ class Project(object): return container_operation_with_timeout +def translate_credential_spec_to_security_opt(service_dict): + result = [] + + if 'credential_spec' in service_dict: + spec = convert_credential_spec_to_security_opt(service_dict['credential_spec']) + result.append('credentialspec={spec}'.format(spec=spec)) + + if result: + service_dict['security_opt'] = result + + return service_dict + + +def translate_resource_keys_to_container_config(resources_dict, service_dict): + if 'limits' in resources_dict: + service_dict['mem_limit'] = resources_dict['limits'].get('memory') + if 'cpus' in resources_dict['limits']: + service_dict['cpus'] = float(resources_dict['limits']['cpus']) + if 'reservations' in resources_dict: + service_dict['mem_reservation'] = resources_dict['reservations'].get('memory') + if 'cpus' in resources_dict['reservations']: + return ['resources.reservations.cpus'] + return [] + + +def convert_restart_policy(name): + try: + return { + 'any': 'always', + 'none': 'no', + 'on-failure': 'on-failure' + }[name] + except KeyError: + raise ConfigurationError('Invalid restart policy "{}"'.format(name)) + + +def convert_credential_spec_to_security_opt(credential_spec): + if 'file' in credential_spec: + return 'file://{file}'.format(file=credential_spec['file']) + return 'registry://{registry}'.format(registry=credential_spec['registry']) + + +def translate_deploy_keys_to_container_config(service_dict): + if 'credential_spec' in service_dict: + del service_dict['credential_spec'] + if 'configs' in service_dict: + del service_dict['configs'] + + if 'deploy' not in service_dict: + return service_dict, [] + + deploy_dict = service_dict['deploy'] + ignored_keys = [ + k for k in ['endpoint_mode', 'labels', 'update_config', 'rollback_config'] + if k in deploy_dict + ] + + if 'restart_policy' in deploy_dict: + service_dict['restart'] = { + 'Name': convert_restart_policy(deploy_dict['restart_policy'].get('condition', 'any')), + 'MaximumRetryCount': deploy_dict['restart_policy'].get('max_attempts', 0) + } + for k in deploy_dict['restart_policy'].keys(): + if k != 'condition' and k != 'max_attempts': + ignored_keys.append('restart_policy.{}'.format(k)) + + ignored_keys.extend( + translate_resource_keys_to_container_config( + deploy_dict.get('resources', {}), service_dict + ) + ) + del service_dict['deploy'] + return service_dict, ignored_keys + + def get_volumes_from(project, service_dict): volumes_from = service_dict.pop('volumes_from', None) if not volumes_from: