Add Magnum cluster support

Add CRUD support for Magnum COE clusters. Please refer Magnum's
API ref doc for the API design[1].

[1] https://developer.openstack.org/api-ref/container-infrastructure-management

Change-Id: I7adff704083a4aeb4c509eb97671eb26f3b9a12c
This commit is contained in:
Feilong Wang 2018-06-22 16:49:24 +12:00 committed by Monty Taylor
parent 76b2adfe9c
commit dcb6c53f94
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
5 changed files with 375 additions and 0 deletions

View File

@ -919,6 +919,40 @@ class Normalizer(object):
ret.append(self._normalize_server_usage(server_usage))
return ret
def _normalize_coe_clusters(self, coe_clusters):
ret = []
for coe_cluster in coe_clusters:
ret.append(self._normalize_coe_cluster(coe_cluster))
return ret
def _normalize_coe_cluster(self, coe_cluster):
"""Normalize Magnum COE cluster."""
coe_cluster = coe_cluster.copy()
# Discard noise
coe_cluster.pop('links', None)
c_id = coe_cluster.pop('uuid')
ret = munch.Munch(
id=c_id,
location=self._get_current_location(),
)
for key in (
'status',
'cluster_template_id',
'stack_id',
'keypair',
'master_count',
'create_timeout',
'node_count',
'name'):
ret[key] = coe_cluster.pop(key)
ret['properties'] = coe_cluster
return ret
def _normalize_cluster_templates(self, cluster_templates):
ret = []
for cluster_template in cluster_templates:

View File

@ -8648,6 +8648,152 @@ class OpenStackCloud(_normalize.Normalizer):
return True
@_utils.cache_on_arguments()
def list_coe_clusters(self):
"""List COE(Ccontainer Orchestration Engine) cluster.
:returns: a list of dicts containing the cluster.
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
with _utils.shade_exceptions("Error fetching cluster list"):
data = self._container_infra_client.get('/clusters')
return self._normalize_coe_clusters(
self._get_and_munchify('clusters', data))
def search_coe_clusters(
self, name_or_id=None, filters=None):
"""Search COE cluster.
:param name_or_id: cluster name or ID.
:param filters: a dict containing additional filters to use.
:param detail: a boolean to control if we need summarized or
detailed output.
:returns: a list of dict containing the cluster
:raises: ``OpenStackCloudException``: if something goes wrong during
the OpenStack API call.
"""
coe_clusters = self.list_coe_clusters()
return _utils._filter_list(
coe_clusters, name_or_id, filters)
def get_coe_cluster(self, name_or_id, filters=None):
"""Get a COE cluster by name or ID.
:param name_or_id: Name or ID of the cluster.
:param filters:
A dictionary of meta data to use for further filtering. Elements
of this dictionary may, themselves, be dictionaries. Example::
{
'last_name': 'Smith',
'other': {
'gender': 'Female'
}
}
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A cluster dict or None if no matching cluster is found.
"""
return _utils._get_entity(self, 'coe_cluster', name_or_id,
filters=filters)
def create_coe_cluster(
self, name, cluster_template_id, **kwargs):
"""Create a COE cluster based on given cluster template.
:param string name: Name of the cluster.
:param string image_id: ID of the cluster template to use.
Other arguments will be passed in kwargs.
:returns: a dict containing the cluster description
:raises: ``OpenStackCloudException`` if something goes wrong during
the OpenStack API call
"""
error_message = ("Error creating cluster of name"
" {cluster_name}".format(cluster_name=name))
with _utils.shade_exceptions(error_message):
body = kwargs.copy()
body['name'] = name
body['cluster_template_id'] = cluster_template_id
cluster = self._container_infra_client.post(
'/clusters', json=body)
self.list_coe_clusters.invalidate(self)
return cluster
def delete_coe_cluster(self, name_or_id):
"""Delete a COE cluster.
:param name_or_id: Name or unique ID of the cluster.
:returns: True if the delete succeeded, False if the
cluster was not found.
:raises: OpenStackCloudException on operation error.
"""
cluster = self.get_coe_cluster(name_or_id)
if not cluster:
self.log.debug(
"COE Cluster %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True)
return False
with _utils.shade_exceptions("Error in deleting COE cluster"):
self._container_infra_client.delete(
'/clusters/{id}'.format(id=cluster['id']))
self.list_coe_clusters.invalidate(self)
return True
@_utils.valid_kwargs('name')
def update_coe_cluster(self, name_or_id, operation, **kwargs):
"""Update a COE cluster.
:param name_or_id: Name or ID of the COE cluster being updated.
:param operation: Operation to perform - add, remove, replace.
Other arguments will be passed with kwargs.
:returns: a dict representing the updated cluster.
:raises: OpenStackCloudException on operation error.
"""
self.list_coe_clusters.invalidate(self)
cluster = self.get_coe_cluster(name_or_id)
if not cluster:
raise exc.OpenStackCloudException(
"COE cluster %s not found." % name_or_id)
if operation not in ['add', 'replace', 'remove']:
raise TypeError(
"%s operation not in 'add', 'replace', 'remove'" % operation)
patches = _utils.generate_patches_from_kwargs(operation, **kwargs)
# No need to fire an API call if there is an empty patch
if not patches:
return cluster
with _utils.shade_exceptions(
"Error updating COE cluster {0}".format(name_or_id)):
self._container_infra_client.patch(
'/clusters/{id}'.format(id=cluster['id']),
json=patches)
new_cluster = self.get_coe_cluster(name_or_id)
return new_cluster
@_utils.cache_on_arguments()
def list_cluster_templates(self, detail=False):
"""List cluster templates.

View File

@ -0,0 +1,28 @@
# 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.
"""
test_coe_clusters
----------------------------------
Functional tests for `shade` COE clusters methods.
"""
from openstack.tests.functional.cloud import base
class TestCompute(base.BaseFunctionalTestCase):
# NOTE(flwang): Currently, running Magnum on a cloud which doesn't support
# nested virtualization will lead to timeout. So this test file is mostly
# like a note to document why we can't have function testing for Magnum
# clusters CRUD.
pass

View File

@ -0,0 +1,163 @@
# 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.
import munch
from openstack.tests.unit import base
coe_cluster_obj = munch.Munch(
status="CREATE_IN_PROGRESS",
cluster_template_id="0562d357-8641-4759-8fed-8173f02c9633",
uuid="731387cf-a92b-4c36-981e-3271d63e5597",
links=[{}, {}],
stack_id="31c1ee6c-081e-4f39-9f0f-f1d87a7defa1",
keypair="my_keypair",
master_count=3,
create_timeout=60,
node_count=10,
name="k8s",
created_at="2016-08-29T06:51:31+00:00",
api_address="https://172.24.4.6:6443",
discovery_url="https://discovery.etcd.io/cbeb580da58915809d59ee69348a84f3",
updated_at="2016-08-29T06:53:24+00:00",
coe_version="v1.2.0",
master_addresses=["172.24.4.6"],
node_addresses=["172.24.4.13"],
status_reason="Stack CREATE completed successfully",
)
class TestCOEClusters(base.TestCase):
def test_list_coe_clusters(self):
self.register_uris([dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()]))])
cluster_list = self.cloud.list_coe_clusters()
self.assertEqual(
cluster_list[0],
self.cloud._normalize_coe_cluster(coe_cluster_obj))
self.assert_calls()
def test_create_coe_cluster(self):
self.register_uris([dict(
method='POST',
uri='https://container-infra.example.com/v1/clusters',
json=dict(baymodels=[coe_cluster_obj.toDict()]),
validate=dict(json={
'name': 'k8s',
'cluster_template_id': '0562d357-8641-4759-8fed-8173f02c9633',
'master_count': 3,
'node_count': 10}),
)])
self.cloud.create_coe_cluster(
name=coe_cluster_obj.name,
cluster_template_id=coe_cluster_obj.cluster_template_id,
master_count=coe_cluster_obj.master_count,
node_count=coe_cluster_obj.node_count)
self.assert_calls()
def test_search_coe_cluster_by_name(self):
self.register_uris([dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()]))])
coe_clusters = self.cloud.search_coe_clusters(
name_or_id='k8s')
self.assertEqual(1, len(coe_clusters))
self.assertEqual(coe_cluster_obj.uuid, coe_clusters[0]['id'])
self.assert_calls()
def test_search_coe_cluster_not_found(self):
self.register_uris([dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()]))])
coe_clusters = self.cloud.search_coe_clusters(
name_or_id='non-existent')
self.assertEqual(0, len(coe_clusters))
self.assert_calls()
def test_get_coe_cluster(self):
self.register_uris([dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()]))])
r = self.cloud.get_coe_cluster(coe_cluster_obj.name)
self.assertIsNotNone(r)
self.assertDictEqual(
r, self.cloud._normalize_coe_cluster(coe_cluster_obj))
self.assert_calls()
def test_get_coe_cluster_not_found(self):
self.register_uris([dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[]))])
r = self.cloud.get_coe_cluster('doesNotExist')
self.assertIsNone(r)
self.assert_calls()
def test_delete_coe_cluster(self):
uri = ('https://container-infra.example.com/v1/clusters/%s' %
coe_cluster_obj.uuid)
self.register_uris([
dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()])),
dict(
method='DELETE',
uri=uri),
])
self.cloud.delete_coe_cluster(coe_cluster_obj.uuid)
self.assert_calls()
def test_update_coe_cluster(self):
uri = ('https://container-infra.example.com/v1/clusters/%s' %
coe_cluster_obj.uuid)
self.register_uris([
dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
json=dict(clusters=[coe_cluster_obj.toDict()])),
dict(
method='PATCH',
uri=uri,
status_code=200,
validate=dict(
json=[{
u'op': u'replace',
u'path': u'/name',
u'value': u'new-coe-cluster'
}]
)),
dict(
method='GET',
uri='https://container-infra.example.com/v1/clusters',
# This json value is not meaningful to the test - it just has
# to be valid.
json=dict(clusters=[coe_cluster_obj.toDict()])),
])
new_name = 'new-coe-cluster'
self.cloud.update_coe_cluster(
coe_cluster_obj.uuid, 'replace', name=new_name)
self.assert_calls()

View File

@ -0,0 +1,4 @@
---
features:
- |
Added magnum cluster CRUD support to cloud abstraction layer.