From 23ca0d3c668f600d2b6ad9b9deb28ada047a484c Mon Sep 17 00:00:00 2001 From: Theodoros Tsioutsias Date: Mon, 14 Oct 2019 08:58:00 +0000 Subject: [PATCH] No new NGs for clusters without an api_address With this change, the nodegroup api controller raises an exception if the user tries to create a nodegroup in a cluster that does not have an api_address yet. If the nodegroup is created without the cluster's API address as an input then the new nodes will not be able to join the cluster. Change-Id: If3b168d7f756a055b80d38a4f80cedc97f1b47e8 story: 2006716 task: 37087 --- magnum/api/controllers/v1/nodegroup.py | 4 ++++ magnum/common/exception.py | 4 ++++ magnum/tests/unit/api/controllers/v1/test_nodegroup.py | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/magnum/api/controllers/v1/nodegroup.py b/magnum/api/controllers/v1/nodegroup.py index c6e9ed55a7..449a7db4d3 100644 --- a/magnum/api/controllers/v1/nodegroup.py +++ b/magnum/api/controllers/v1/nodegroup.py @@ -292,6 +292,10 @@ class NodeGroupController(base.Controller): policy.enforce(context, 'nodegroup:create', action='nodegroup:create') cluster = api_utils.get_resource('Cluster', cluster_id) + # Before we start, we need to check that the cluster has an + # api_address. If not, just fail. + if 'api_address' not in cluster or not cluster.api_address: + raise exception.ClusterAPIAddressUnavailable() cluster_ngs = [ng.name for ng in cluster.nodegroups] if nodegroup.name in cluster_ngs: raise exception.NodeGroupAlreadyExists(name=nodegroup.name, diff --git a/magnum/common/exception.py b/magnum/common/exception.py index f4a1a22024..d9467fc855 100755 --- a/magnum/common/exception.py +++ b/magnum/common/exception.py @@ -466,3 +466,7 @@ class NgOperationInProgress(Invalid): class InvalidClusterTemplateForUpgrade(Conflict): message = _("Cluster Template is not valid for upgrade: %(reason)s") + + +class ClusterAPIAddressUnavailable(Conflict): + message = _("Cluster API address is not available yet") diff --git a/magnum/tests/unit/api/controllers/v1/test_nodegroup.py b/magnum/tests/unit/api/controllers/v1/test_nodegroup.py index 9e958ce8f9..cd460d6940 100644 --- a/magnum/tests/unit/api/controllers/v1/test_nodegroup.py +++ b/magnum/tests/unit/api/controllers/v1/test_nodegroup.py @@ -353,6 +353,16 @@ class TestPost(NodeGroupControllerTest): self.assertEqual('application/json', response.content_type) self.assertEqual(406, response.status_int) + def test_create_ng_cluster_no_api_address(self): + # Remove the api address from the cluster and make sure + # that the request is not accepted. + self.cluster.api_address = None + self.cluster.save() + ng_dict = apiutils.nodegroup_post_data() + response = self.post_json(self.url, ng_dict, expect_errors=True) + self.assertEqual('application/json', response.content_type) + self.assertEqual(409, response.status_int) + class TestDelete(NodeGroupControllerTest):