From cbe05aa97d90d3ac38d55fcee472ec2130aa1327 Mon Sep 17 00:00:00 2001 From: Theodoros Tsioutsias Date: Mon, 24 Jun 2019 13:36:53 +0200 Subject: [PATCH] ng-6: Add new fields to nodegroup objects Since each nodegroup will be one independent stack, we have to add more fields to the table and object in order to track each stack contained in the cluster. This adds the stack_id, version, status, status_reason and version fields to the nodegroup object. Change-Id: I6d36b2d3bc6476efbef6a9f702ffc73cfa0fab8c --- .../conductor/handlers/cluster_conductor.py | 3 ++ magnum/db/api.py | 2 +- .../versions/c04e925e65c2_nodegroups_v2.py | 50 +++++++++++++++++++ magnum/db/sqlalchemy/api.py | 6 ++- magnum/db/sqlalchemy/models.py | 4 ++ magnum/objects/nodegroup.py | 18 ++++--- magnum/tests/unit/db/utils.py | 8 ++- magnum/tests/unit/objects/test_objects.py | 2 +- 8 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 magnum/db/sqlalchemy/alembic/versions/c04e925e65c2_nodegroups_v2.py diff --git a/magnum/conductor/handlers/cluster_conductor.py b/magnum/conductor/handlers/cluster_conductor.py index 77f2a26b6b..638e38dee0 100755 --- a/magnum/conductor/handlers/cluster_conductor.py +++ b/magnum/conductor/handlers/cluster_conductor.py @@ -77,6 +77,9 @@ class Handler(object): # Create cluster cluster_driver.create_cluster(context, cluster, create_timeout) cluster.save() + for ng in cluster.nodegroups: + ng.stack_id = cluster.stack_id + ng.save() except Exception as e: cluster.status = fields.ClusterStatus.CREATE_FAILED diff --git a/magnum/db/api.py b/magnum/db/api.py index 07e1c61f16..2727dc0ab4 100644 --- a/magnum/db/api.py +++ b/magnum/db/api.py @@ -634,7 +634,7 @@ class Connection(object): :param context: The security context :param cluster_id: The uuid of the cluster where the nodegroup - belongs to. + belongs to. :param filters: Filters to apply. Defaults to None. :param limit: Maximum number of nodegroups to return. diff --git a/magnum/db/sqlalchemy/alembic/versions/c04e925e65c2_nodegroups_v2.py b/magnum/db/sqlalchemy/alembic/versions/c04e925e65c2_nodegroups_v2.py new file mode 100644 index 0000000000..88b099cf6d --- /dev/null +++ b/magnum/db/sqlalchemy/alembic/versions/c04e925e65c2_nodegroups_v2.py @@ -0,0 +1,50 @@ +# Copyright (c) 2018 European Organization for Nuclear Research. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""nodegroups_v2 + +Revision ID: c04e925e65c2 +Revises: 47380964133d +Create Date: 2019-06-14 09:29:58.288671 + +""" + +# revision identifiers, used by Alembic. +revision = 'c04e925e65c2' +down_revision = '47380964133d' + +from alembic import op + +import sqlalchemy as sa + +from oslo_db.sqlalchemy.types import String + + +def upgrade(): + op.add_column('nodegroup', sa.Column('stack_id', String(255))) + op.add_column('nodegroup', sa.Column('status', String(20))) + op.add_column('nodegroup', sa.Column('status_reason', sa.Text())) + op.add_column('nodegroup', sa.Column('version', String(20))) + + # Populate existing nodegroups with the cluster stack_id + connection = op.get_bind() + + connection.execute( + "UPDATE nodegroup " + "INNER JOIN cluster ON nodegroup.cluster_id=cluster.uuid " + "SET nodegroup.stack_id=cluster.stack_id, " + "nodegroup.status=cluster.status, nodegroup.version=0 " + "WHERE nodegroup.cluster_id=cluster.uuid" + ) diff --git a/magnum/db/sqlalchemy/api.py b/magnum/db/sqlalchemy/api.py index 2c74037c92..cc1b41a5bd 100644 --- a/magnum/db/sqlalchemy/api.py +++ b/magnum/db/sqlalchemy/api.py @@ -795,6 +795,10 @@ class Connection(api.Connection): query = query.filter_by(**filter_dict) + if 'status' in filters: + query = query.filter( + models.NodeGroup.status.in_(filters['status'])) + return query def create_nodegroup(self, values): @@ -882,8 +886,8 @@ class Connection(api.Connection): query = model_query(models.NodeGroup) if not context.is_admin: query = query.filter_by(project_id=context.project_id) - query = self._add_nodegoup_filters(query, filters) query = query.filter_by(cluster_id=cluster_id) + query = self._add_nodegoup_filters(query, filters) return _paginate_query(models.NodeGroup, limit, marker, sort_key, sort_dir, query) diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index ec3e7ae291..5ba7751893 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -289,3 +289,7 @@ class NodeGroup(Base): max_node_count = Column(Integer()) min_node_count = Column(Integer()) is_default = Column(Boolean, default=False) + stack_id = Column(String(255)) + status = Column(String(20)) + status_reason = Column(Text) + version = Column(String(20)) diff --git a/magnum/objects/nodegroup.py b/magnum/objects/nodegroup.py index 0c3e1ddc72..b6b2e0287e 100644 --- a/magnum/objects/nodegroup.py +++ b/magnum/objects/nodegroup.py @@ -19,6 +19,7 @@ from oslo_versionedobjects import fields from magnum.db import api as dbapi from magnum.objects import base +from magnum.objects import fields as m_fields @base.MagnumObjectRegistry.register @@ -45,7 +46,11 @@ class NodeGroup(base.MagnumPersistentObject, base.MagnumObject, 'role': fields.StringField(), 'max_node_count': fields.IntegerField(nullable=True), 'min_node_count': fields.IntegerField(nullable=False, default=1), - 'is_default': fields.BooleanField(default=False) + 'is_default': fields.BooleanField(default=False), + 'stack_id': fields.StringField(nullable=True), + 'status': m_fields.ClusterStatusField(nullable=True), + 'status_reason': fields.StringField(nullable=True), + 'version': fields.StringField(nullable=True), } @staticmethod @@ -129,7 +134,7 @@ class NodeGroup(base.MagnumPersistentObject, base.MagnumObject, return cls.dbapi.get_cluster_nodegroup_count(context, cluster_id) @base.remotable_classmethod - def list(cls, context, cluster, limit=None, marker=None, + def list(cls, context, cluster_id, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of NodeGroup objects. @@ -145,12 +150,9 @@ class NodeGroup(base.MagnumPersistentObject, base.MagnumObject, :returns: a list of :class:`NodeGroup` objects. """ - db_nodegroups = cls.dbapi.list_cluster_nodegroups(context, cluster, - limit=limit, - marker=marker, - sort_key=sort_key, - sort_dir=sort_dir, - filters=filters) + db_nodegroups = cls.dbapi.list_cluster_nodegroups( + context, cluster_id, limit=limit, marker=marker, sort_key=sort_key, + sort_dir=sort_dir, filters=filters) return NodeGroup._from_db_object_list(db_nodegroups, cls, context) @base.remotable diff --git a/magnum/tests/unit/db/utils.py b/magnum/tests/unit/db/utils.py index b25a91adec..e3f836cb69 100644 --- a/magnum/tests/unit/db/utils.py +++ b/magnum/tests/unit/db/utils.py @@ -299,7 +299,11 @@ def get_test_nodegroup(**kw): 'min_node_count': kw.get('min_node_count', 1), 'is_default': kw.get('is_default', True), 'created_at': kw.get('created_at'), - 'updated_at': kw.get('updated_at') + 'updated_at': kw.get('updated_at'), + 'status': kw.get('status', 'CREATE_COMPLETE'), + 'status_reason': kw.get('status_reason', 'Completed successfully'), + 'version': kw.get('version', '1'), + 'stack_id': kw.get('stack_id', '047c6319-7abd-fake-a033-8c6af0173cd0'), } @@ -327,7 +331,7 @@ def get_nodegroups_for_cluster(**kw): '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'), project_id=kw.get('project_id', 'fake_project'), node_addresses=kw.get('node_addresses', ['172.17.2.4']), - node_count=kw.get('node_count', 3) + node_count=kw.get('node_count', 3), ) # get masters nodegroup diff --git a/magnum/tests/unit/objects/test_objects.py b/magnum/tests/unit/objects/test_objects.py index 33fb251865..ec565133a8 100644 --- a/magnum/tests/unit/objects/test_objects.py +++ b/magnum/tests/unit/objects/test_objects.py @@ -364,7 +364,7 @@ object_data = { 'Stats': '1.0-73a1cd6e3c0294c932a66547faba216c', 'Quota': '1.0-94e100aebfa88f7d8428e007f2049c18', 'Federation': '1.0-166da281432b083f0e4b851336e12e20', - 'NodeGroup': '1.0-75e1378a800040312c59f89546a51d74' + 'NodeGroup': '1.0-8cb4544a28a49860d816158a7c3060b1' }