From 6ad1fb26a49bef9d15f004a32557aa0351a72242 Mon Sep 17 00:00:00 2001 From: "OTSUKA, Yuanying" Date: Thu, 18 Dec 2014 00:00:31 +0900 Subject: [PATCH] Implement creating Bay using heat Co-authored-by: Steven Dake Implements-blueprint: backend-bay-heat-kube Change-Id: Ia544d8bd3bc03afa900247b8da09675c0c6aec71 --- magnum/cmd/conductor.py | 4 +- magnum/conductor/handlers/bay_k8s_heat.py | 56 ++++++++----------- .../2581ebaf0cb2_initial_migration.py | 6 +- magnum/db/sqlalchemy/models.py | 3 + magnum/objects/bay.py | 3 + 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/magnum/cmd/conductor.py b/magnum/cmd/conductor.py index c3fe62699a..07819f446e 100644 --- a/magnum/cmd/conductor.py +++ b/magnum/cmd/conductor.py @@ -21,7 +21,7 @@ import sys from oslo.config import cfg from magnum.common import rpc_service as service -from magnum.conductor.handlers import bay_ironic as bay_ironic +from magnum.conductor.handlers import bay_k8s_heat from magnum.conductor.handlers import docker_conductor from magnum.conductor.handlers import kube as k8s_conductor from magnum.openstack.common._i18n import _ @@ -43,7 +43,7 @@ def main(): endpoints = [ docker_conductor.Handler(), k8s_conductor.Handler(), - bay_ironic.Handler() + bay_k8s_heat.Handler() ] server = service.Service(cfg.CONF.conductor.topic, cfg.CONF.conductor.host, endpoints) diff --git a/magnum/conductor/handlers/bay_k8s_heat.py b/magnum/conductor/handlers/bay_k8s_heat.py index b893f6dfed..2a342224a2 100644 --- a/magnum/conductor/handlers/bay_k8s_heat.py +++ b/magnum/conductor/handlers/bay_k8s_heat.py @@ -12,9 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. +from heatclient.common import template_utils from oslo.config import cfg -from magnum.common import heat +from magnum.common import clients from magnum import objects from magnum.openstack.common._i18n import _ from magnum.openstack.common import log as logging @@ -22,7 +23,8 @@ from magnum.openstack.common import log as logging k8s_heat_opts = [ cfg.StrOpt('template_path', - default='/etc/magnum/templates/kubecluster.yaml', + default= + '/etc/magnum/templates/heat-kubernetes/kubecluster.yaml', help=_( 'Location of template to build a k8s cluster. '))] @@ -43,25 +45,30 @@ class Handler(object): # Bay Operations def bay_create(self, ctxt, bay): - if bay.type is not 'k8s_heat': - return - LOG.debug('k8s_heat bay_create') - # 'definition' and 'properties' field are needed. + + osc = clients.OpenStackClients(ctxt) # stack_name is unique on each tenant. stack_name = bay.name - # bay_definition is used for heat parameter - # larsks/heat-kubernetes requires - # 'ssh_key_name' and 'external_network_id' - bay_definition = bay.definition + bay_model = objects.BayModel.get_by_uuid(ctxt, bay.baymodel_id) + bay_definition = { + 'ssh_key_name': bay_model.keypair_id, + 'external_network_id': bay_model.external_network_id, + 'server_image': bay_model.image_id, + 'server_flavor': bay_model.flavor_id + } + tpl_files, template = template_utils.get_template_contents( + cfg.CONF.k8s_heat.template_path) - heat_client = heat.get_client(ctxt) - created_stack = heat_client.stacks.create(stack_name=stack_name, - template=cfg.CONF.k8s_heat.template_path, - parameters=bay_definition) - stack_id = created_stack['stack']['id'] - bay.properties['stack_id'] = stack_id + fields = { + 'stack_name': stack_name, + 'parameters': bay_definition, + 'template': template, + 'files': dict(list(tpl_files.items())) + } + created_stack = osc.heat().stacks.create(**fields) + bay.stack_id = created_stack['stack']['id'] bay.create() @@ -71,31 +78,14 @@ class Handler(object): return bay def bay_delete(self, ctxt, uuid): - # if bay.type is not 'k8s_heat': - # return - LOG.debug('k8s_heat bay_delete') bay = objects.Bay.get_by_uuid(ctxt, uuid) - stack_id = bay.properties['stack_id'] - heat.stacks.delete(stack_id) bay.destroy() return None def bay_show(self, ctxt, uuid): - # if bay.type is not 'k8s_heat': - # return - LOG.debug('k8s_heat bay_show') bay = objects.Bay.get_by_uuid(ctxt, uuid) - stack_id = bay.properties['stack_id'] - stack = heat.stacks.get(stack_id) - - if stack.status == 'COMPLETE': - master_address = stack['outputs'][0]['output_value'] - minion_addresses = stack['outputs'][2]['output_value'] - bay.properties['master_address'] = master_address - bay.properties['minion_addresses'] = minion_addresses - bay.save() return bay diff --git a/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py b/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py index e0526ebdae..adcf9c81ec 100644 --- a/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py +++ b/magnum/db/sqlalchemy/alembic/versions/2581ebaf0cb2_initial_migration.py @@ -38,6 +38,9 @@ def upgrade(): sa.Column('name', sa.String(length=255), nullable=True), sa.Column('baymodel_id', sa.String(length=255), nullable=True), sa.Column('node_count', sa.Integer(), nullable=True), + sa.Column('master_address', sa.String(length=255), nullable=True), + sa.Column('minions_address', sa.Text(), nullable=True), + sa.Column('stack_id', sa.String(length=255), nullable=True), sa.PrimaryKeyConstraint('id'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' @@ -49,10 +52,9 @@ def upgrade(): sa.Column('id', sa.Integer(), nullable=False), sa.Column('uuid', sa.String(length=36), nullable=True), sa.Column('name', sa.String(length=255), nullable=True), - sa.Column('type', sa.String(length=20), nullable=True), - sa.Column('image_id', sa.String(length=255), nullable=True), sa.Column('flavor_id', sa.String(length=255), nullable=True), sa.Column('keypair_id', sa.String(length=255), nullable=True), + sa.Column('image_id', sa.String(length=255), nullable=True), sa.Column('external_network_id', sa.String(length=255), nullable=True), sa.Column('dns_nameserver', sa.String(length=255), nullable=True), sa.PrimaryKeyConstraint('id'), diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index 3df65332bb..d42d279591 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -123,6 +123,9 @@ class Bay(Base): uuid = Column(String(36)) name = Column(String(255)) baymodel_id = Column(String(255)) + stack_id = Column(String(255)) + master_address = Column(String(255)) + minions_address = Column(JSONEncodedList) node_count = Column(Integer()) diff --git a/magnum/objects/bay.py b/magnum/objects/bay.py index 4da2e73ffb..084f2a98a0 100644 --- a/magnum/objects/bay.py +++ b/magnum/objects/bay.py @@ -31,6 +31,9 @@ class Bay(base.MagnumObject): 'uuid': obj_utils.str_or_none, 'name': obj_utils.str_or_none, 'baymodel_id': obj_utils.str_or_none, + 'stack_id': obj_utils.str_or_none, + 'master_address': obj_utils.str_or_none, + 'minions_address': obj_utils.list_or_none, 'node_count': obj_utils.int_or_none }