diff --git a/template/capsule/capsule-volume.yaml b/template/capsule/capsule-volume.yaml index ae9d5ceea..94bc93fe1 100644 --- a/template/capsule/capsule-volume.yaml +++ b/template/capsule/capsule-volume.yaml @@ -5,9 +5,9 @@ metadata: name: capsule-volume labels: foo: bar -restartPolicy: always availabilityZone: nova spec: + restartPolicy: Always containers: - image: test command: diff --git a/template/capsule/capsule.yaml b/template/capsule/capsule.yaml index 251448940..10824ebb9 100644 --- a/template/capsule/capsule.yaml +++ b/template/capsule/capsule.yaml @@ -6,9 +6,9 @@ metadata: labels: app: web app1: web1 -restartPolicy: always availabilityZone: nova spec: + restartPolicy: Always containers: - image: ubuntu command: diff --git a/zun/api/controllers/v1/capsules.py b/zun/api/controllers/v1/capsules.py index a92a798c3..6450364cb 100644 --- a/zun/api/controllers/v1/capsules.py +++ b/zun/api/controllers/v1/capsules.py @@ -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 diff --git a/zun/api/controllers/v1/schemas/parameter_types.py b/zun/api/controllers/v1/schemas/parameter_types.py index 7e7ccc60a..a35a85d90 100644 --- a/zun/api/controllers/v1/schemas/parameter_types.py +++ b/zun/api/controllers/v1/schemas/parameter_types.py @@ -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, }, diff --git a/zun/api/controllers/versions.py b/zun/api/controllers/versions.py index fa3665896..979a1ef03 100644 --- a/zun/api/controllers/versions.py +++ b/zun/api/controllers/versions.py @@ -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): diff --git a/zun/api/rest_api_version_history.rst b/zun/api/rest_api_version_history.rst index 9211a591e..644076968 100644 --- a/zun/api/rest_api_version_history.rst +++ b/zun/api/rest_api_version_history.rst @@ -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. diff --git a/zun/common/utils.py b/zun/common/utils.py index 6a3e17d14..aeaa509b0 100644 --- a/zun/common/utils.py +++ b/zun/common/utils.py @@ -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 diff --git a/zun/tests/unit/api/controllers/test_root.py b/zun/tests/unit/api/controllers/test_root.py index 47c3b3ae7..d81aa2126 100644 --- a/zun/tests/unit/api/controllers/test_root.py +++ b/zun/tests/unit/api/controllers/test_root.py @@ -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'}]} diff --git a/zun/tests/unit/api/test_validations.py b/zun/tests/unit/api/test_validations.py index 959680a3c..987668d42 100644 --- a/zun/tests/unit/api/test_validations.py +++ b/zun/tests/unit/api/test_validations.py @@ -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 " diff --git a/zun/tests/unit/common/test_utils.py b/zun/tests/unit/common/test_utils.py index 095598a41..279903c96 100644 --- a/zun/tests/unit/common/test_utils.py +++ b/zun/tests/unit/common/test_utils.py @@ -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(