Modify restart_policy to spec content

Align with Kubernetes

Change-Id: I24cec2b92ebef0c984cbf74bac074633624c498a
Signed-off-by: Kevin Zhao <kevin.zhao@arm.com>
This commit is contained in:
Kevin Zhao 2018-05-31 15:44:41 +08:00 committed by Hongbin Lu
parent 15e29a81f2
commit 1e8503e870
10 changed files with 37 additions and 26 deletions

View File

@ -5,9 +5,9 @@ metadata:
name: capsule-volume
labels:
foo: bar
restartPolicy: always
availabilityZone: nova
spec:
restartPolicy: Always
containers:
- image: test
command:

View File

@ -6,9 +6,9 @@ metadata:
labels:
app: web
app1: web1
restartPolicy: always
availabilityZone: nova
spec:
restartPolicy: Always
containers:
- image: ubuntu
command:

View File

@ -138,6 +138,7 @@ class CapsuleController(base.Controller):
spec_content, template_json = \
utils.check_capsule_template(capsules_template)
containers_spec = utils.capsule_get_container_spec(spec_content)
volumes_spec = utils.capsule_get_volume_spec(spec_content)
@ -152,8 +153,8 @@ class CapsuleController(base.Controller):
capsule_need_memory = 0
container_volume_requests = []
capsule_restart_policy = template_json.get('restart_policy',
'always')
capsule_restart_policy = spec_content.get('restart_policy',
'always')
container_restart_policy = {"MaximumRetryCount": "0",
"Name": capsule_restart_policy}
new_capsule.restart_policy = capsule_restart_policy

View File

@ -471,6 +471,7 @@ capsule_spec = {
"properties": {
"containers": capsule_containers_list,
"volumes": capsule_volumes_list,
"restartPolicy": capsule_restart_policy,
},
"additionalProperties": True,
"required": ['containers']
@ -482,7 +483,6 @@ capsule_template = {
"kind": capsule_kind,
"capsuleVersion": capsule_version,
"metadata": capsule_metadata,
"restartPolicy": capsule_restart_policy,
"spec": capsule_spec,
"availabilityZone": availability_zone,
},

View File

@ -48,10 +48,11 @@ REST_API_VERSION_HISTORY = """REST API Version History:
* 1.13 - Add support for listing networks of a container
* 1.14 - Add support to rename the container from update api
* 1.15 - Remove add_security_group and remove_security_group
* 1.16 - Modify restart_policy to capsule spec content
"""
BASE_VER = '1.1'
CURRENT_MAX_VER = '1.15'
CURRENT_MAX_VER = '1.16'
class Version(object):

View File

@ -129,3 +129,8 @@ user documentation.
Remove the APIs for adding/removing security group to/from a container.
These APIs are removed because they are proxy APIs to Neutron.
1.16
----
Modify restart_policy to capsule spec content to align with Kubernetes.

View File

@ -365,16 +365,18 @@ def check_capsule_template(tpl):
if kind_field not in ['capsule', 'Capsule']:
raise exception.InvalidCapsuleTemplate("kind fields need to be "
"set as capsule or Capsule")
# Align the Capsule restartPolicy with container restart_policy
if 'restartPolicy' in tpl_json.keys():
tpl_json['restartPolicy'] = \
VALID_CAPSULE_RESTART_POLICY[tpl_json['restartPolicy']]
tpl_json[VALID_CAPSULE_FIELD['restartPolicy']] = \
tpl_json.pop('restartPolicy')
spec_field = tpl_json.get('spec')
if spec_field is None:
raise exception.InvalidCapsuleTemplate("No Spec found")
# Align the Capsule restartPolicy with container restart_policy
# Also change the template filed name from Kubernetes type to OpenStack
# type.
if 'restartPolicy' in spec_field.keys():
spec_field['restartPolicy'] = \
VALID_CAPSULE_RESTART_POLICY[spec_field['restartPolicy']]
spec_field[VALID_CAPSULE_FIELD['restartPolicy']] = \
spec_field.pop('restartPolicy')
if spec_field.get('containers') is None:
raise exception.InvalidCapsuleTemplate("No valid containers field")
return spec_field, tpl_json

View File

@ -28,7 +28,7 @@ class TestRootController(api_base.FunctionalTest):
'default_version':
{'id': 'v1',
'links': [{'href': 'http://localhost/v1/', 'rel': 'self'}],
'max_version': '1.15',
'max_version': '1.16',
'min_version': '1.1',
'status': 'CURRENT'},
'description': 'Zun is an OpenStack project which '
@ -37,7 +37,7 @@ class TestRootController(api_base.FunctionalTest):
'versions': [{'id': 'v1',
'links': [{'href': 'http://localhost/v1/',
'rel': 'self'}],
'max_version': '1.15',
'max_version': '1.16',
'min_version': '1.1',
'status': 'CURRENT'}]}

View File

@ -221,8 +221,9 @@ class TestCapsuleSchemaValidations(base.BaseTestCase):
"metadata": {
"labels": {"app": "web"},
"name": "template"},
"restartPolicy": "Always",
"spec": {
"restartPolicy": "Always",
"containers": [
{"workDir": "/root", "image": "ubuntu",
"volumeMounts": [{"readOnly": True,
@ -239,7 +240,7 @@ class TestCapsuleSchemaValidations(base.BaseTestCase):
"memory": 1024}}}],
"volumes": [
{"cinder": {"autoRemove": True, "size": 5},
"name": "volume1"}
"name": "volume1"},
]}}}
self.schema_validator.validate(request_to_validate)
@ -305,8 +306,8 @@ class TestCapsuleSchemaValidations(base.BaseTestCase):
"metadata": {
"labels": {"app": "web"},
"name": "template"},
"restartPolicy": restart_policy,
"spec": {"containers": [{"image": "test"}]}
"spec": {"containers": [{"image": "test"}],
"restartPolicy": restart_policy}
}}
self.schema_validator.validate(request_to_validate)
for restart_policy in invalid_restart_policy:
@ -317,8 +318,8 @@ class TestCapsuleSchemaValidations(base.BaseTestCase):
"metadata": {
"labels": {"app": "web"},
"name": "template"},
"restartPolicy": restart_policy,
"spec": {"containers": [{"image": "test"}]}
"spec": {"restartPolicy": restart_policy,
"containers": [{"image": "test"}]}
}}
with self.assertRaisesRegex(exception.SchemaValidationError,
"Invalid input for field "

View File

@ -143,11 +143,11 @@ class TestUtils(base.TestCase):
params = ({"kind": "capsule", "spec": {}})
utils.check_capsule_template(params)
params = ({"kind": "capsule", "restartPolicy": "Always", "spec": {
"containers": [{"image": "test1"}]
params = ({"kind": "capsule", "spec": {
"containers": [{"image": "test1"}], "restartPolicy": "Always",
}})
spec_content, tpl_json = utils.check_capsule_template(params)
self.assertEqual(tpl_json["restart_policy"], "always")
self.assertEqual(spec_content["restart_policy"], "always")
def test_check_capsule_template_unicode(self):
with self.assertRaisesRegex(
@ -167,10 +167,11 @@ class TestUtils(base.TestCase):
params = (u'{"kind": "capsule", "spec": {}}')
utils.check_capsule_template(params)
params = (u'{"kind": "capsule", "restartPolicy": "Always", "spec": {'
u'"containers": [{"image": "test1"}]}}')
params = (u'{"kind": "capsule", "spec": {'
u'"containers": [{"image": "test1"}],'
u'"restartPolicy": "Always"}}')
spec_content, tpl_json = utils.check_capsule_template(params)
self.assertEqual(tpl_json["restart_policy"], "always")
self.assertEqual(spec_content["restart_policy"], "always")
def test_capsule_get_container_spec(self):
with self.assertRaisesRegex(