From 32989b4f7b1fdee40768188289bce4a8baf624b7 Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Wed, 17 Jul 2019 15:42:48 +1200 Subject: [PATCH] Allow setting network, subnet and FIP when creating cluster When using a public cluster template, user still need the capability to reuse their existing network/subnet, and they also need to be able to turn of/off the floatingip to overwrite the setting in the public template. This patch supports that by adding those three items as parameters when creating cluster. Story: 2006208 Task: 35797 Change-Id: I11579ff6b83d133c71c2cbf49ee4b20996dfb918 --- .../drivers/heat/dcos_centos_template_def.py | 2 +- magnum/api/controllers/v1/bay.py | 9 +++++ magnum/api/controllers/v1/cluster.py | 35 ++++++++++------ ...4133d_add_network_subnet_fip_to_cluster.py | 40 +++++++++++++++++++ magnum/db/sqlalchemy/models.py | 3 ++ .../drivers/heat/k8s_coreos_template_def.py | 3 +- .../drivers/heat/k8s_fedora_template_def.py | 3 +- magnum/drivers/heat/k8s_template_def.py | 4 +- .../drivers/heat/swarm_fedora_template_def.py | 3 +- .../drivers/heat/swarm_mode_template_def.py | 3 +- magnum/drivers/heat/template_def.py | 6 +-- .../drivers/mesos_ubuntu_v1/template_def.py | 3 +- magnum/objects/cluster.py | 8 +++- .../handlers/test_k8s_cluster_conductor.py | 9 ++++- .../handlers/test_mesos_cluster_conductor.py | 3 ++ .../handlers/test_swarm_cluster_conductor.py | 5 ++- magnum/tests/unit/db/utils.py | 3 ++ .../unit/drivers/test_template_definition.py | 10 +++-- magnum/tests/unit/objects/test_objects.py | 2 +- ...hen-creating-cluster-ae0cda35ade28a9f.yaml | 8 ++++ 20 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 magnum/db/sqlalchemy/alembic/versions/47380964133d_add_network_subnet_fip_to_cluster.py create mode 100644 releasenotes/notes/allow-setting-network-subnet-FIP-when-creating-cluster-ae0cda35ade28a9f.yaml diff --git a/contrib/drivers/heat/dcos_centos_template_def.py b/contrib/drivers/heat/dcos_centos_template_def.py index 2d47274907..0c203d0525 100644 --- a/contrib/drivers/heat/dcos_centos_template_def.py +++ b/contrib/drivers/heat/dcos_centos_template_def.py @@ -156,7 +156,7 @@ class DcosCentosTemplateDefinition(template_def.BaseTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster) template_def.add_lb_env_file(env_files, cluster_template) template_def.add_fip_env_file(env_files, cluster_template, cluster) diff --git a/magnum/api/controllers/v1/bay.py b/magnum/api/controllers/v1/bay.py index dd995772a6..dad79d20b2 100755 --- a/magnum/api/controllers/v1/bay.py +++ b/magnum/api/controllers/v1/bay.py @@ -142,6 +142,15 @@ class Bay(base.APIBase): bay_faults = wsme.wsattr(wtypes.DictType(wtypes.text, wtypes.text)) """Fault info collected from the heat resources of this bay""" + fixed_network = wtypes.StringType(min_length=1, max_length=255) + """The fixed network name to attach to the Cluster""" + + fixed_subnet = wtypes.StringType(min_length=1, max_length=255) + """The fixed subnet name to attach to the Cluster""" + + floating_ip_enabled = wsme.wsattr(types.boolean, default=True) + """Indicates whether created clusters should have a floating ip or not.""" + def __init__(self, **kwargs): super(Bay, self).__init__() diff --git a/magnum/api/controllers/v1/cluster.py b/magnum/api/controllers/v1/cluster.py index c48d8f21e0..dfd932addc 100755 --- a/magnum/api/controllers/v1/cluster.py +++ b/magnum/api/controllers/v1/cluster.py @@ -174,6 +174,15 @@ class Cluster(base.APIBase): faults = wsme.wsattr(wtypes.DictType(wtypes.text, wtypes.text)) """Fault info collected from the heat resources of this cluster""" + fixed_network = wtypes.StringType(min_length=1, max_length=255) + """The fixed network name to attach to the Cluster""" + + fixed_subnet = wtypes.StringType(min_length=1, max_length=255) + """The fixed subnet name to attach to the Cluster""" + + floating_ip_enabled = wsme.wsattr(types.boolean, default=True) + """Indicates whether created clusters should have a floating ip or not.""" + def __init__(self, **kwargs): super(Cluster, self).__init__() self.fields = [] @@ -236,7 +245,10 @@ class Cluster(base.APIBase): created_at=timeutils.utcnow(), updated_at=timeutils.utcnow(), coe_version=None, - container_version=None) + container_version=None, + fixed_network=None, + fixed_subnet=None, + floating_ip_enabled=True) return cls._convert_with_links(sample, 'http://localhost:9511', expand) @@ -471,23 +483,20 @@ class ClustersController(base.Controller): if cluster.keypair is None: cluster.keypair = cluster_template.keypair_id - # If docker_volume_size is not present, use cluster_template value - if (cluster.docker_volume_size == wtypes.Unset or - not cluster.docker_volume_size): - cluster.docker_volume_size = cluster_template.docker_volume_size - # If labels is not present, use cluster_template value if cluster.labels == wtypes.Unset: cluster.labels = cluster_template.labels - # If master_flavor_id is not present, use cluster_template value - if (cluster.master_flavor_id == wtypes.Unset or - not cluster.master_flavor_id): - cluster.master_flavor_id = cluster_template.master_flavor_id + # If floating_ip_enabled is not present, use cluster_template value + if cluster.floating_ip_enabled == wtypes.Unset: + cluster.floating_ip_enabled = cluster_template.floating_ip_enabled - # If flavor_id is not present, use cluster_template value - if cluster.flavor_id == wtypes.Unset or not cluster.flavor_id: - cluster.flavor_id = cluster_template.flavor_id + attributes = ["docker_volume_size", "master_flavor_id", "flavor_id", + "fixed_network", "fixed_subnet"] + for attr in attributes: + if (getattr(cluster, attr) == wtypes.Unset or + not getattr(cluster, attr)): + setattr(cluster, attr, getattr(cluster_template, attr)) cluster_dict = cluster.as_dict() diff --git a/magnum/db/sqlalchemy/alembic/versions/47380964133d_add_network_subnet_fip_to_cluster.py b/magnum/db/sqlalchemy/alembic/versions/47380964133d_add_network_subnet_fip_to_cluster.py new file mode 100644 index 0000000000..5e4a3c4067 --- /dev/null +++ b/magnum/db/sqlalchemy/alembic/versions/47380964133d_add_network_subnet_fip_to_cluster.py @@ -0,0 +1,40 @@ +# 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. + +"""add-network-subnet-fip-to-cluster + +Revision ID: 47380964133d +Revises: 461d798132c7 +Create Date: 2019-07-17 13:17:58.760452 + +""" + +# revision identifiers, used by Alembic. +revision = '47380964133d' +down_revision = '461d798132c7' + +from alembic import op +from oslo_db.sqlalchemy.types import String +import sqlalchemy as sa +from sqlalchemy.dialects.mysql import TINYTEXT + + +def upgrade(): + op.add_column('cluster', sa.Column('fixed_network', + String(255, mysql_ndb_type=TINYTEXT), + nullable=True)) + op.add_column('cluster', sa.Column('fixed_subnet', + String(255, mysql_ndb_type=TINYTEXT), + nullable=True)) + op.add_column('cluster', sa.Column('floating_ip_enabled', + sa.Boolean(), + default=False)) diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index b8f3ccf9de..ec3e7ae291 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -144,6 +144,9 @@ class Cluster(Base): # so, we use 512 chars to get some buffer. ca_cert_ref = Column(String(512, mysql_ndb_type=mysql_TEXT)) magnum_cert_ref = Column(String(512, mysql_ndb_type=mysql_TEXT)) + fixed_network = Column(String(255, mysql_ndb_type=TINYTEXT)) + fixed_subnet = Column(String(255, mysql_ndb_type=TINYTEXT)) + floating_ip_enabled = Column(Boolean, default=True) class ClusterTemplate(Base): diff --git a/magnum/drivers/heat/k8s_coreos_template_def.py b/magnum/drivers/heat/k8s_coreos_template_def.py index c965b76205..8ca5862e05 100644 --- a/magnum/drivers/heat/k8s_coreos_template_def.py +++ b/magnum/drivers/heat/k8s_coreos_template_def.py @@ -157,7 +157,8 @@ class CoreOSK8sTemplateDefinition(k8s_template_def.K8sTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster_template, + cluster) template_def.add_etcd_volume_env_file(env_files, cluster_template) template_def.add_volume_env_file(env_files, cluster) template_def.add_lb_env_file(env_files, cluster_template) diff --git a/magnum/drivers/heat/k8s_fedora_template_def.py b/magnum/drivers/heat/k8s_fedora_template_def.py index 4bd399a61d..746b769fc0 100644 --- a/magnum/drivers/heat/k8s_fedora_template_def.py +++ b/magnum/drivers/heat/k8s_fedora_template_def.py @@ -216,7 +216,8 @@ class K8sFedoraTemplateDefinition(k8s_template_def.K8sTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster_template, + cluster) template_def.add_etcd_volume_env_file(env_files, cluster_template) template_def.add_volume_env_file(env_files, cluster) template_def.add_lb_env_file(env_files, cluster_template) diff --git a/magnum/drivers/heat/k8s_template_def.py b/magnum/drivers/heat/k8s_template_def.py index 1337a30792..2edf6a23ff 100644 --- a/magnum/drivers/heat/k8s_template_def.py +++ b/magnum/drivers/heat/k8s_template_def.py @@ -59,9 +59,9 @@ class K8sTemplateDefinition(template_def.BaseTemplateDefinition): cluster_template_attr='external_network_id', required=True) self.add_parameter('fixed_network', - cluster_template_attr='fixed_network') + cluster_attr='fixed_network') self.add_parameter('fixed_subnet', - cluster_template_attr='fixed_subnet') + cluster_attr='fixed_subnet') self.add_parameter('network_driver', cluster_template_attr='network_driver') self.add_parameter('volume_driver', diff --git a/magnum/drivers/heat/swarm_fedora_template_def.py b/magnum/drivers/heat/swarm_fedora_template_def.py index 74feb03c3b..d35d365cb1 100644 --- a/magnum/drivers/heat/swarm_fedora_template_def.py +++ b/magnum/drivers/heat/swarm_fedora_template_def.py @@ -155,7 +155,8 @@ class SwarmFedoraTemplateDefinition(template_def.BaseTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster_template, + cluster) template_def.add_volume_env_file(env_files, cluster) template_def.add_lb_env_file(env_files, cluster_template) diff --git a/magnum/drivers/heat/swarm_mode_template_def.py b/magnum/drivers/heat/swarm_mode_template_def.py index 23c9c55613..2b1c024752 100644 --- a/magnum/drivers/heat/swarm_mode_template_def.py +++ b/magnum/drivers/heat/swarm_mode_template_def.py @@ -182,7 +182,8 @@ class SwarmModeTemplateDefinition(template_def.BaseTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster_template, + cluster) template_def.add_volume_env_file(env_files, cluster) template_def.add_lb_env_file(env_files, cluster_template) template_def.add_fip_env_file(env_files, cluster_template, cluster) diff --git a/magnum/drivers/heat/template_def.py b/magnum/drivers/heat/template_def.py index 1ae3aa04d6..1d85724d4d 100755 --- a/magnum/drivers/heat/template_def.py +++ b/magnum/drivers/heat/template_def.py @@ -514,7 +514,7 @@ def add_fip_env_file(env_files, cluster_template, cluster): ) master_lb_fip_enabled = strutils.bool_from_string(lb_fip_enabled) - if cluster_template.floating_ip_enabled: + if cluster.floating_ip_enabled: env_files.append(COMMON_ENV_PATH + 'enable_floating_ip.yaml') else: env_files.append(COMMON_ENV_PATH + 'disable_floating_ip.yaml') @@ -525,8 +525,8 @@ def add_fip_env_file(env_files, cluster_template, cluster): env_files.append(COMMON_ENV_PATH + 'disable_lb_floating_ip.yaml') -def add_priv_net_env_file(env_files, cluster_template): - if cluster_template.fixed_network: +def add_priv_net_env_file(env_files, cluster_template, cluster): + if (cluster.fixed_network or cluster_template.fixed_network): env_files.append(COMMON_ENV_PATH + 'no_private_network.yaml') else: env_files.append(COMMON_ENV_PATH + 'with_private_network.yaml') diff --git a/magnum/drivers/mesos_ubuntu_v1/template_def.py b/magnum/drivers/mesos_ubuntu_v1/template_def.py index 2becab1fae..4679d68a50 100644 --- a/magnum/drivers/mesos_ubuntu_v1/template_def.py +++ b/magnum/drivers/mesos_ubuntu_v1/template_def.py @@ -121,7 +121,8 @@ class UbuntuMesosTemplateDefinition(template_def.BaseTemplateDefinition): def get_env_files(self, cluster_template, cluster): env_files = [] - template_def.add_priv_net_env_file(env_files, cluster_template) + template_def.add_priv_net_env_file(env_files, cluster_template, + cluster) template_def.add_lb_env_file(env_files, cluster_template) return env_files diff --git a/magnum/objects/cluster.py b/magnum/objects/cluster.py index e8acb57b53..9c3fcc11ec 100644 --- a/magnum/objects/cluster.py +++ b/magnum/objects/cluster.py @@ -51,8 +51,9 @@ class Cluster(base.MagnumPersistentObject, base.MagnumObject, # Version 1.19: Added nodegroups, default_ng_worker, default_ng_master # Version 1.20: Fields node_count, master_count, node_addresses, # master_addresses are now properties. + # Version 1.21 Added fixed_network, fixed_subnet, floating_ip_enabled - VERSION = '1.20' + VERSION = '1.21' dbapi = dbapi.get_instance() @@ -84,7 +85,10 @@ class Cluster(base.MagnumPersistentObject, base.MagnumObject, 'trustee_password': fields.StringField(nullable=True), 'trustee_user_id': fields.StringField(nullable=True), 'coe_version': fields.StringField(nullable=True), - 'container_version': fields.StringField(nullable=True) + 'container_version': fields.StringField(nullable=True), + 'fixed_network': fields.StringField(nullable=True), + 'fixed_subnet': fields.StringField(nullable=True), + 'floating_ip_enabled': fields.BooleanField(default=True), } @staticmethod diff --git a/magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py index c0b4c7a9c0..4c2764945d 100644 --- a/magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py @@ -118,7 +118,10 @@ class TestClusterConductorWithK8s(base.TestCase): 'master_flavor_id': 'master_flavor_id', 'flavor_id': 'flavor_id', 'project_id': 'project_id', - 'keystone_auth_default_policy': self.keystone_auth_default_policy + 'keystone_auth_default_policy': self.keystone_auth_default_policy, + 'fixed_network': 'fixed_network', + 'fixed_subnet': 'fixed_subnet', + 'floating_ip_enabled': False, } self.worker_ng_dict = { 'uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a53', @@ -554,6 +557,8 @@ class TestClusterConductorWithK8s(base.TestCase): 'docker_volume_size': 20, 'master_flavor': 'master_flavor_id', 'minion_flavor': 'flavor_id', + 'fixed_network': 'fixed_network', + 'fixed_subnet': 'fixed_subnet', 'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e', 'flannel_backend': 'vxlan', 'flannel_network_cidr': '10.101.0.0/16', @@ -607,7 +612,7 @@ class TestClusterConductorWithK8s(base.TestCase): } self.assertEqual(expected, definition) self.assertEqual( - ['../../common/templates/environments/with_private_network.yaml', + ['../../common/templates/environments/no_private_network.yaml', '../../common/templates/environments/no_etcd_volume.yaml', '../../common/templates/environments/with_volume.yaml', '../../common/templates/environments/no_master_lb.yaml', diff --git a/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py index 58f83da396..998ea02d84 100644 --- a/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_mesos_cluster_conductor.py @@ -72,6 +72,9 @@ class TestClusterConductorWithMesos(base.TestCase): 'mesos_slave_executor_env_variables': '{}', 'mesos_slave_work_dir': '/tmp/mesos/slave' }, + 'fixed_network': '', + 'fixed_subnet': '', + 'floating_ip_enabled': False, } self.worker_ng_dict = { 'uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a53', diff --git a/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py b/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py index 09a3059b95..255e4e3faa 100644 --- a/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py +++ b/magnum/tests/unit/conductor/handlers/test_swarm_cluster_conductor.py @@ -81,7 +81,10 @@ class TestClusterConductorWithSwarm(base.TestCase): 'rexray_preempt': 'False', 'swarm_strategy': 'spread', 'availability_zone': 'az_1'}, - 'coe_version': 'fake-version' + 'coe_version': 'fake-version', + 'fixed_network': '', + 'fixed_subnet': '', + 'floating_ip_enabled': False, } self.worker_ng_dict = { 'uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a53', diff --git a/magnum/tests/unit/db/utils.py b/magnum/tests/unit/db/utils.py index 8a5055b701..9056ed07fd 100644 --- a/magnum/tests/unit/db/utils.py +++ b/magnum/tests/unit/db/utils.py @@ -98,6 +98,9 @@ def get_test_cluster(**kw): 'labels': kw.get('labels'), 'master_flavor_id': kw.get('master_flavor_id', None), 'flavor_id': kw.get('flavor_id', None), + 'fixed_network': kw.get('fixed_network', None), + 'fixed_subnet': kw.get('fixed_subnet', None), + 'floating_ip_enabled': kw.get('floating_ip_enabled', True), } if kw.pop('for_api_use', False): diff --git a/magnum/tests/unit/drivers/test_template_definition.py b/magnum/tests/unit/drivers/test_template_definition.py index 7a9dcdd9f6..7ee24f2759 100644 --- a/magnum/tests/unit/drivers/test_template_definition.py +++ b/magnum/tests/unit/drivers/test_template_definition.py @@ -219,7 +219,7 @@ class TemplateDefinitionTestCase(base.TestCase): mock_cluster_template = mock.MagicMock(floating_ip_enabled=False, master_lb_enabled=False, labels={}) - mock_cluster = mock.MagicMock(labels={}) + mock_cluster = mock.MagicMock(labels={}, floating_ip_enabled=False) env_files = [] cmn_tdef.add_fip_env_file(env_files, mock_cluster_template, mock_cluster) @@ -235,7 +235,7 @@ class TemplateDefinitionTestCase(base.TestCase): mock_cluster_template = mock.MagicMock(floating_ip_enabled=False, master_lb_enabled=True, labels={}) - mock_cluster = mock.MagicMock(labels={}) + mock_cluster = mock.MagicMock(labels={}, floating_ip_enabled=False,) env_files = [] cmn_tdef.add_fip_env_file(env_files, mock_cluster_template, mock_cluster) @@ -254,7 +254,8 @@ class TemplateDefinitionTestCase(base.TestCase): labels={"master_lb_floating_ip_enabled": "true"} ) mock_cluster = mock.MagicMock( - labels={"master_lb_floating_ip_enabled": "true"}) + labels={"master_lb_floating_ip_enabled": "true"}, + floating_ip_enabled=False,) env_files = [] cmn_tdef.add_fip_env_file(env_files, mock_cluster_template, mock_cluster) @@ -273,7 +274,8 @@ class TemplateDefinitionTestCase(base.TestCase): labels={"master_lb_floating_ip_enabled": "false"} ) mock_cluster = mock.MagicMock( - labels={"master_lb_floating_ip_enabled": "false"}) + labels={"master_lb_floating_ip_enabled": "false"}, + floating_ip_enabled=False,) env_files = [] cmn_tdef.add_fip_env_file(env_files, mock_cluster_template, diff --git a/magnum/tests/unit/objects/test_objects.py b/magnum/tests/unit/objects/test_objects.py index 7adc81fee2..33fb251865 100644 --- a/magnum/tests/unit/objects/test_objects.py +++ b/magnum/tests/unit/objects/test_objects.py @@ -355,7 +355,7 @@ class TestObject(test_base.TestCase, _TestObject): # For more information on object version testing, read # https://docs.openstack.org/magnum/latest/contributor/objects.html object_data = { - 'Cluster': '1.20-fcdb29a886bf9552cdac03470570024c', + 'Cluster': '1.21-11ab13dfd5cb53578d398008d758dab8', 'ClusterTemplate': '1.19-3b0b2b3933d0955abf3ab40111744960', 'Certificate': '1.1-1924dc077daa844f0f9076332ef96815', 'MyObj': '1.0-34c4b1aadefd177b13f9a2f894cc23cd', diff --git a/releasenotes/notes/allow-setting-network-subnet-FIP-when-creating-cluster-ae0cda35ade28a9f.yaml b/releasenotes/notes/allow-setting-network-subnet-FIP-when-creating-cluster-ae0cda35ade28a9f.yaml new file mode 100644 index 0000000000..dc8ee45b71 --- /dev/null +++ b/releasenotes/notes/allow-setting-network-subnet-FIP-when-creating-cluster-ae0cda35ade28a9f.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + When using a public cluster template, user still need the capability + to reuse their existing network/subnet, and they also need to be + able to turn of/off the floating IP to overwrite the setting in the + public template. Now this is supported by adding those three + items as parameters when creating cluster.