From e7a2366af6e47429ed3233e3c733cc3c3e333249 Mon Sep 17 00:00:00 2001 From: "Jay Lau (Guangya Liu)" Date: Thu, 15 Jan 2015 05:11:45 -0500 Subject: [PATCH] Set replication controller name from rc manifest Only replication controller manifest knows about rc name, so this patch retrieve rc name from rc manifest. Change-Id: I8193c2066ea1f0a40761f304f784fbb3b2d16796 --- .../controllers/v1/replicationcontroller.py | 31 ++++++++++++++----- .../2581ebaf0cb2_initial_migration.py | 2 +- magnum/db/sqlalchemy/models.py | 2 +- magnum/objects/replicationcontroller.py | 10 +++--- .../v1/test_replicationcontroller.py | 23 +++++++++----- magnum/tests/db/utils.py | 2 +- 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/magnum/api/controllers/v1/replicationcontroller.py b/magnum/api/controllers/v1/replicationcontroller.py index a7ea980915..82e16090e2 100644 --- a/magnum/api/controllers/v1/replicationcontroller.py +++ b/magnum/api/controllers/v1/replicationcontroller.py @@ -26,6 +26,7 @@ from magnum.api.controllers.v1 import collection from magnum.api.controllers.v1 import types from magnum.api.controllers.v1 import utils as api_utils from magnum.common import exception +from magnum.common import k8s_manifest from magnum import objects @@ -70,7 +71,7 @@ class ReplicationController(base.APIBase): uuid = types.uuid """Unique UUID for this ReplicationController""" - name = wtypes.text + name = wsme.wsattr(wtypes.text, readonly=True) """Name of this ReplicationController""" images = [wtypes.text] @@ -79,16 +80,16 @@ class ReplicationController(base.APIBase): bay_uuid = types.uuid """Unique UUID of the bay the ReplicationController runs on""" - selector = {wtypes.text: wtypes.text} + labels = wsme.wsattr({wtypes.text: wtypes.text}, readonly=True) """Selector of this ReplicationController""" - replicas = wtypes.IntegerType() + replicas = wsme.wsattr(wtypes.IntegerType(), readonly=True) """Replicas of this ReplicationController""" rc_definition_url = wtypes.text """URL for ReplicationController file to create the RC""" - rc_data = wtypes.text + replicationcontroller_data = wtypes.text """Data for service to create the ReplicationController""" links = wsme.wsattr([link.Link], readonly=True) @@ -121,7 +122,7 @@ class ReplicationController(base.APIBase): def _convert_with_links(rc, url, expand=True): if not expand: rc.unset_fields_except(['uuid', 'name', 'images', 'bay_uuid', - 'selector', 'replicas']) + 'labels', 'replicas']) # never expose the rc_id attribute rc.rc_id = wtypes.Unset @@ -145,7 +146,7 @@ class ReplicationController(base.APIBase): name='MyReplicationController', images=['MyImage'], bay_uuid='f978db47-9a37-4e9f-8572-804a10abc0ab', - selector={'name': 'foo'}, + labels={'name': 'foo'}, replicas=2, created_at=datetime.datetime.utcnow(), updated_at=datetime.datetime.utcnow()) @@ -154,6 +155,19 @@ class ReplicationController(base.APIBase): sample._rc_uuid = '87504bd9-ca50-40fd-b14e-bcb23ed42b27' return cls._convert_with_links(sample, 'http://localhost:9511', expand) + def parse_manifest(self): + # Set replication controller name and labels from its manifest + # TODO(jay-lau-513): retrieve replication controller name from + # rc_definition_url + if (hasattr(self, "replicationcontroller_data") + and self.replicationcontroller_data is not None): + manifest = k8s_manifest.parse(self.replicationcontroller_data) + self.name = manifest["id"] + if "labels" in manifest: + self.labels = manifest["labels"] + if "replicas" in manifest: + self.replicas = manifest["replicas"] + class ReplicationControllerCollection(collection.Collection): """API representation of a collection of ReplicationControllers.""" @@ -277,6 +291,7 @@ class ReplicationControllersController(rest.RestController): if self.from_rcs: raise exception.OperationNotPermitted + rc.parse_manifest() rc_obj = objects.ReplicationController(pecan.request.context, **rc.as_dict()) new_rc = pecan.request.rpcapi.rc_create(rc_obj) @@ -315,8 +330,8 @@ class ReplicationControllersController(rest.RestController): # ignore rc_definition_url as it was used for create rc if field == 'rc_definition_url': continue - # ignore rc_data as it was used for create rc - if field == 'rc_data': + # ignore replicationcontroller_data as it was used for create rc + if field == 'replicationcontroller_data': continue try: patch_val = getattr(rc, field) diff --git a/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py b/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py index 468fd1d6ac..bb2b387072 100644 --- a/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py +++ b/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py @@ -128,7 +128,7 @@ def upgrade(): sa.Column('name', sa.String(length=255), nullable=True), sa.Column('bay_uuid', sa.String(length=36), nullable=True), sa.Column('images', sa.Text(), nullable=False), - sa.Column('selector', sa.Text(), nullable=True), + sa.Column('labels', sa.Text(), nullable=True), sa.Column('replicas', sa.Integer(), nullable=True), sa.PrimaryKeyConstraint('id'), mysql_ENGINE='InnoDB', diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index 26916f3a4a..980c4a9a2f 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -229,5 +229,5 @@ class ReplicationController(Base): name = Column(String(255)) bay_uuid = Column(String(36)) images = Column(JSONEncodedList) - selector = Column(JSONEncodedDict) + labels = Column(JSONEncodedDict) replicas = Column(Integer()) diff --git a/magnum/objects/replicationcontroller.py b/magnum/objects/replicationcontroller.py index 01f4096211..7b59e43f0a 100644 --- a/magnum/objects/replicationcontroller.py +++ b/magnum/objects/replicationcontroller.py @@ -31,10 +31,10 @@ class ReplicationController(base.MagnumObject): 'name': obj_utils.str_or_none, 'images': obj_utils.list_or_none, 'bay_uuid': obj_utils.str_or_none, - 'selector': obj_utils.dict_or_none, - 'replicas': int, + 'labels': obj_utils.dict_or_none, + 'replicas': obj_utils.int_or_none, 'rc_definition_url': obj_utils.str_or_none, - 'rc_data': obj_utils.str_or_none, + 'repplicationcontroller_data': obj_utils.str_or_none, } @staticmethod @@ -44,8 +44,8 @@ class ReplicationController(base.MagnumObject): # ignore rc_definition_url as it was used for create rc if field == 'rc_definition_url': continue - # ignore rc_data as it was used for create rc - if field == 'rc_data': + # ignore repplicationcontroller_data as it was used for create rc + if field == 'repplicationcontroller_data': continue rc[field] = db_rc[field] diff --git a/magnum/tests/api/controllers/v1/test_replicationcontroller.py b/magnum/tests/api/controllers/v1/test_replicationcontroller.py index c84c9181b3..8380c75d51 100644 --- a/magnum/tests/api/controllers/v1/test_replicationcontroller.py +++ b/magnum/tests/api/controllers/v1/test_replicationcontroller.py @@ -27,10 +27,20 @@ class TestRCController(db_base.DbTestCase): with patch.object(api.API, 'rc_create') as mock_method: mock_method.side_effect = self.mock_rc_create # Create a replication controller - params = '{"name": "rc_example_A", "images": ["ubuntu"],' \ - '"selector": {"foo": "foo1"}, "replicas": 2,' \ - '"bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae",' \ - '"rc_definition_url": "http://172.17.1.2/rc.json"}' + params = ''' + { + "bay_uuid": "7ae81bb3-dec3-4289-8d6c-da80bd8001ae", + "replicationcontroller_data": "\ + {\ + \\"id\\": \\"name_of_rc\\", \ + \\"replicas\\": 3, \ + \\"labels\\": {\ + \\"foo\\": \\"foo1\\"\ + }\ + }\ + \" + } + ''' response = self.app.post('/v1/rcs', params=params, content_type='application/json') @@ -42,11 +52,10 @@ class TestRCController(db_base.DbTestCase): self.assertEqual(1, len(response.json)) c = response.json['rcs'][0] self.assertIsNotNone(c.get('uuid')) - self.assertEqual('rc_example_A', c.get('name')) - self.assertEqual(['ubuntu'], c.get('images')) + self.assertEqual('name_of_rc', c.get('name')) self.assertEqual('7ae81bb3-dec3-4289-8d6c-da80bd8001ae', c.get('bay_uuid')) - self.assertEqual('foo1', c.get('selector')['foo']) + self.assertEqual('foo1', c.get('labels')['foo']) # Get just the one we created response = self.app.get('/v1/rcs/%s' % c.get('uuid')) diff --git a/magnum/tests/db/utils.py b/magnum/tests/db/utils.py index 0bae0ca526..6ac57c0070 100644 --- a/magnum/tests/db/utils.py +++ b/magnum/tests/db/utils.py @@ -181,7 +181,7 @@ def get_test_rc(**kw): 'name': kw.get('name', 'service1'), 'images': kw.get('images', ['steak/for-dinner']), 'bay_uuid': kw.get('bay_uuid', '10a47dd1-4874-4298-91cf-eff046dbdb8e'), - 'selector': kw.get('selector', {'name': 'foo'}), + 'labels': kw.get('labels', {'name': 'foo'}), 'replicas': kw.get('replicas', 3), 'rc_definition_url': kw.get('file:///tmp/rc.yaml'), 'created_at': kw.get('created_at'),