Use bay to init K8sAPI instead of bay_uuid
This patch aimed to reduce db call count when we invode k8sAPI. For those user using bay name to do pod/rc/service operations, we can saving a db call. Change-Id: I1488d0526e1d444cb681b408f8a13ce25b4aee6f
This commit is contained in:
parent
b52cc471f2
commit
b0ce382cf1
|
@ -40,7 +40,8 @@ class Handler(object):
|
|||
|
||||
def service_create(self, context, service):
|
||||
LOG.debug("service_create")
|
||||
self.k8s_api = k8s.create_k8s_api(context, service.bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, service.bay_uuid)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
manifest = k8s_manifest.parse(service.manifest)
|
||||
try:
|
||||
resp = self.k8s_api.create_namespaced_service(body=manifest,
|
||||
|
@ -69,20 +70,17 @@ class Handler(object):
|
|||
|
||||
def service_update(self, context, service_ident, bay_ident, manifest):
|
||||
LOG.debug("service_update %s", service_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(service_ident):
|
||||
service = objects.Service.get_by_uuid(context,
|
||||
service_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
else:
|
||||
service = objects.Service.get_by_name(context,
|
||||
service_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
service_ident = service.name
|
||||
try:
|
||||
|
@ -100,7 +98,7 @@ class Handler(object):
|
|||
service['name'] = resp.metadata.name
|
||||
service['project_id'] = context.project_id
|
||||
service['user_id'] = context.user_id
|
||||
service['bay_uuid'] = bay_uuid
|
||||
service['bay_uuid'] = bay.uuid
|
||||
service['labels'] = ast.literal_eval(resp.metadata.labels)
|
||||
if not resp.spec.selector:
|
||||
service['selector'] = {}
|
||||
|
@ -120,18 +118,15 @@ class Handler(object):
|
|||
|
||||
def service_delete(self, context, service_ident, bay_ident):
|
||||
LOG.debug("service_delete %s", service_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(service_ident):
|
||||
service = objects.Service.get_by_uuid(context, service_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
service_name = service.name
|
||||
else:
|
||||
service_name = service_ident
|
||||
if conductor_utils.object_has_stack(context, bay_uuid):
|
||||
if conductor_utils.object_has_stack(context, bay.uuid):
|
||||
try:
|
||||
|
||||
self.k8s_api.delete_namespaced_service(name=str(service_name),
|
||||
|
@ -144,33 +139,27 @@ class Handler(object):
|
|||
|
||||
def service_show(self, context, service_ident, bay_ident):
|
||||
LOG.debug("service_show %s", service_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(service_ident):
|
||||
service = objects.Service.get_by_uuid(context, service_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
else:
|
||||
service = objects.Service.get_by_name(context, service_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
|
||||
return service
|
||||
|
||||
def service_list(self, context, bay_ident):
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
try:
|
||||
resp = self.k8s_api.list_namespaced_service(namespace='default')
|
||||
except rest.ApiException as err:
|
||||
raise exception.KubernetesAPIFailed(err=err)
|
||||
|
||||
if resp is None:
|
||||
raise exception.ServiceListNotFound(bay_uuid=bay_uuid)
|
||||
raise exception.ServiceListNotFound(bay_uuid=bay.uuid)
|
||||
|
||||
services = []
|
||||
for service_entry in resp.items:
|
||||
|
@ -179,7 +168,7 @@ class Handler(object):
|
|||
service['name'] = service_entry.metadata.name
|
||||
service['project_id'] = context.project_id
|
||||
service['user_id'] = context.user_id
|
||||
service['bay_uuid'] = bay_uuid
|
||||
service['bay_uuid'] = bay.uuid
|
||||
service['labels'] = ast.literal_eval(
|
||||
service_entry.metadata.labels)
|
||||
if not service_entry.spec.selector:
|
||||
|
@ -205,7 +194,8 @@ class Handler(object):
|
|||
# Pod Operations
|
||||
def pod_create(self, context, pod):
|
||||
LOG.debug("pod_create")
|
||||
self.k8s_api = k8s.create_k8s_api(context, pod.bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, pod.bay_uuid)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
manifest = k8s_manifest.parse(pod.manifest)
|
||||
try:
|
||||
resp = self.k8s_api.create_namespaced_pod(body=manifest,
|
||||
|
@ -228,17 +218,14 @@ class Handler(object):
|
|||
|
||||
def pod_update(self, context, pod_ident, bay_ident, manifest):
|
||||
LOG.debug("pod_update %s", pod_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(pod_ident):
|
||||
pod = objects.Pod.get_by_uuid(context, pod_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
else:
|
||||
pod = objects.Pod.get_by_name(context, pod_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
pod_ident = pod.name
|
||||
try:
|
||||
resp = self.k8s_api.replace_namespaced_pod(name=str(pod_ident),
|
||||
|
@ -254,7 +241,7 @@ class Handler(object):
|
|||
pod['name'] = resp.metadata.name
|
||||
pod['project_id'] = context.project_id
|
||||
pod['user_id'] = context.user_id
|
||||
pod['bay_uuid'] = bay_uuid
|
||||
pod['bay_uuid'] = bay.uuid
|
||||
pod['images'] = [c.image for c in resp.spec.containers]
|
||||
if not resp.metadata.labels:
|
||||
pod['labels'] = {}
|
||||
|
@ -267,18 +254,15 @@ class Handler(object):
|
|||
|
||||
def pod_delete(self, context, pod_ident, bay_ident):
|
||||
LOG.debug("pod_delete %s", pod_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(pod_ident):
|
||||
pod = objects.Pod.get_by_uuid(context, pod_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
pod_name = pod.name
|
||||
else:
|
||||
pod_name = pod_ident
|
||||
if conductor_utils.object_has_stack(context, bay_uuid):
|
||||
if conductor_utils.object_has_stack(context, bay.uuid):
|
||||
try:
|
||||
self.k8s_api.delete_namespaced_pod(name=str(pod_name), body={},
|
||||
namespace='default')
|
||||
|
@ -290,33 +274,27 @@ class Handler(object):
|
|||
|
||||
def pod_show(self, context, pod_ident, bay_ident):
|
||||
LOG.debug("pod_show %s", pod_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(pod_ident):
|
||||
pod = objects.Pod.get_by_uuid(context, pod_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
else:
|
||||
pod = objects.Pod.get_by_name(context, pod_ident,
|
||||
bay_uuid, self.k8s_api)
|
||||
bay.uuid, self.k8s_api)
|
||||
|
||||
return pod
|
||||
|
||||
def pod_list(self, context, bay_ident):
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
try:
|
||||
resp = self.k8s_api.list_namespaced_pod(namespace='default')
|
||||
except rest.ApiException as err:
|
||||
raise exception.KubernetesAPIFailed(err=err)
|
||||
|
||||
if resp is None:
|
||||
raise exception.PodListNotFound(bay_uuid=bay_uuid)
|
||||
raise exception.PodListNotFound(bay_uuid=bay.uuid)
|
||||
|
||||
pods = []
|
||||
for pod_entry in resp.items:
|
||||
|
@ -325,7 +303,7 @@ class Handler(object):
|
|||
pod['name'] = pod_entry.metadata.name
|
||||
pod['project_id'] = context.project_id
|
||||
pod['user_id'] = context.user_id
|
||||
pod['bay_uuid'] = bay_uuid
|
||||
pod['bay_uuid'] = bay.uuid
|
||||
pod['images'] = [c.image for c in pod_entry.spec.containers]
|
||||
if not pod_entry.metadata.labels:
|
||||
pod['labels'] = {}
|
||||
|
@ -342,7 +320,8 @@ class Handler(object):
|
|||
# Replication Controller Operations
|
||||
def rc_create(self, context, rc):
|
||||
LOG.debug("rc_create")
|
||||
self.k8s_api = k8s.create_k8s_api(context, rc.bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, rc.bay_uuid)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
manifest = k8s_manifest.parse(rc.manifest)
|
||||
try:
|
||||
resp = self.k8s_api.create_namespaced_replication_controller(
|
||||
|
@ -364,18 +343,15 @@ class Handler(object):
|
|||
|
||||
def rc_update(self, context, rc_ident, bay_ident, manifest):
|
||||
LOG.debug("rc_update %s", rc_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(rc_ident):
|
||||
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
else:
|
||||
rc = objects.ReplicationController.get_by_name(context, rc_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
try:
|
||||
resp = self.k8s_api.replace_namespaced_replication_controller(
|
||||
|
@ -393,7 +369,7 @@ class Handler(object):
|
|||
rc['project_id'] = context.project_id
|
||||
rc['user_id'] = context.user_id
|
||||
rc['images'] = [c.image for c in resp.spec.template.spec.containers]
|
||||
rc['bay_uuid'] = bay_uuid
|
||||
rc['bay_uuid'] = bay.uuid
|
||||
rc['labels'] = ast.literal_eval(resp.metadata.labels)
|
||||
rc['replicas'] = resp.status.replicas
|
||||
|
||||
|
@ -401,19 +377,16 @@ class Handler(object):
|
|||
|
||||
def rc_delete(self, context, rc_ident, bay_ident):
|
||||
LOG.debug("rc_delete %s", rc_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(rc_ident):
|
||||
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
rc_name = rc.name
|
||||
else:
|
||||
rc_name = rc_ident
|
||||
if conductor_utils.object_has_stack(context, bay_uuid):
|
||||
if conductor_utils.object_has_stack(context, bay.uuid):
|
||||
try:
|
||||
self.k8s_api.delete_namespaced_replication_controller(
|
||||
name=str(rc_name),
|
||||
|
@ -427,28 +400,22 @@ class Handler(object):
|
|||
|
||||
def rc_show(self, context, rc_ident, bay_ident):
|
||||
LOG.debug("rc_show %s", rc_ident)
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
if utils.is_uuid_like(rc_ident):
|
||||
rc = objects.ReplicationController.get_by_uuid(context, rc_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
else:
|
||||
rc = objects.ReplicationController.get_by_name(context, rc_ident,
|
||||
bay_uuid,
|
||||
bay.uuid,
|
||||
self.k8s_api)
|
||||
|
||||
return rc
|
||||
|
||||
def rc_list(self, context, bay_ident):
|
||||
# Since bay identifier is specified verify whether its a UUID
|
||||
# or Name. If name is specified as bay identifier need to extract
|
||||
# the bay uuid since its needed to get the k8s_api object.
|
||||
bay_uuid = conductor_utils.retrieve_bay_uuid(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay_uuid)
|
||||
bay = conductor_utils.retrieve_bay(context, bay_ident)
|
||||
self.k8s_api = k8s.create_k8s_api(context, bay)
|
||||
try:
|
||||
resp = self.k8s_api.list_namespaced_replication_controller(
|
||||
namespace='default')
|
||||
|
@ -457,7 +424,7 @@ class Handler(object):
|
|||
|
||||
if resp is None:
|
||||
raise exception.ReplicationControllerListNotFound(
|
||||
bay_uuid=bay_uuid)
|
||||
bay_uuid=bay.uuid)
|
||||
|
||||
rcs = []
|
||||
for entry in resp._items:
|
||||
|
@ -468,7 +435,7 @@ class Handler(object):
|
|||
rc['user_id'] = context.user_id
|
||||
rc['images'] = [
|
||||
c.image for c in entry.spec.template.spec.containers]
|
||||
rc['bay_uuid'] = bay_uuid
|
||||
rc['bay_uuid'] = bay.uuid
|
||||
# Convert string to dictionary
|
||||
rc['labels'] = ast.literal_eval(entry.metadata.labels)
|
||||
rc['replicas'] = entry.status.replicas
|
||||
|
|
|
@ -19,7 +19,6 @@ from oslo_log import log as logging
|
|||
from magnum.common.pythonk8sclient.swagger_client import api_client
|
||||
from magnum.common.pythonk8sclient.swagger_client.apis import apiv_api
|
||||
from magnum.conductor.handlers.common import cert_manager
|
||||
from magnum.conductor import utils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
@ -42,12 +41,11 @@ class K8sAPI(apiv_api.ApivApi):
|
|||
raise err
|
||||
return tmp
|
||||
|
||||
def __init__(self, context, bay_uuid):
|
||||
def __init__(self, context, bay):
|
||||
self.ca_file = None
|
||||
self.cert_file = None
|
||||
self.key_file = None
|
||||
|
||||
bay = utils.retrieve_bay(context, bay_uuid)
|
||||
if bay.magnum_cert_ref:
|
||||
self._create_certificate_files(bay)
|
||||
|
||||
|
@ -83,13 +81,13 @@ class K8sAPI(apiv_api.ApivApi):
|
|||
self.key_file.close()
|
||||
|
||||
|
||||
def create_k8s_api(context, bay_uuid):
|
||||
def create_k8s_api(context, bay):
|
||||
"""Create a kubernetes API client
|
||||
|
||||
Creates connection with Kubernetes master and creates ApivApi instance
|
||||
to call Kubernetes APIs.
|
||||
|
||||
:param context: The security context
|
||||
:param bay_uuid: Unique identifier for the Bay
|
||||
:param bay: Bay object
|
||||
"""
|
||||
return K8sAPI(context, bay_uuid)
|
||||
return K8sAPI(context, bay)
|
||||
|
|
|
@ -35,7 +35,7 @@ class K8sMonitor(MonitorBase):
|
|||
}
|
||||
|
||||
def pull_data(self):
|
||||
k8s_api = k8s.create_k8s_api(self.context, self.bay.uuid)
|
||||
k8s_api = k8s.create_k8s_api(self.context, self.bay)
|
||||
nodes = k8s_api.list_namespaced_node()
|
||||
self.data['nodes'] = self._parse_node_info(nodes)
|
||||
pods = k8s_api.list_namespaced_pod('default')
|
||||
|
|
|
@ -47,7 +47,7 @@ class ScaleManager(object):
|
|||
'stack_id': stack.id})
|
||||
|
||||
hosts_no_container = list(hosts)
|
||||
k8s_api = k8s.create_k8s_api(self.context, bay.uuid)
|
||||
k8s_api = k8s.create_k8s_api(self.context, bay)
|
||||
for pod in k8s_api.list_namespaced_pod(namespace='default').items:
|
||||
host = pod.spec.node_name
|
||||
if host in hosts_no_container:
|
||||
|
|
|
@ -18,8 +18,11 @@ from magnum.objects import bay
|
|||
from magnum.objects import baymodel
|
||||
|
||||
|
||||
def retrieve_bay(context, bay_uuid):
|
||||
return bay.Bay.get_by_uuid(context, bay_uuid)
|
||||
def retrieve_bay(context, bay_ident):
|
||||
if not utils.is_uuid_like(bay_ident):
|
||||
return bay.Bay.get_by_name(context, bay_ident)
|
||||
else:
|
||||
return bay.Bay.get_by_uuid(context, bay_ident)
|
||||
|
||||
|
||||
def retrieve_baymodel(context, bay):
|
||||
|
|
|
@ -32,15 +32,15 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
@mock.patch('magnum.common.docker_utils.DockerHTTPClient')
|
||||
@mock.patch.object(docker_utils, 'cert_manager')
|
||||
@mock.patch.object(docker_utils.objects.BayModel, 'get_by_uuid')
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_uuid')
|
||||
def test_docker_for_container(self, mock_get_bay_by_uuid,
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_name')
|
||||
def test_docker_for_container(self, mock_get_bay_by_name,
|
||||
mock_get_baymodel_by_uuid,
|
||||
mock_cert_manager,
|
||||
mock_docker_client):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_bay = mock.MagicMock()
|
||||
mock_bay.api_address = 'https://1.2.3.4:2376'
|
||||
mock_get_bay_by_uuid.return_value = mock_bay
|
||||
mock_get_bay_by_name.return_value = mock_bay
|
||||
mock_baymodel = mock.MagicMock()
|
||||
mock_baymodel.tls_disabled = False
|
||||
mock_get_baymodel_by_uuid.return_value = mock_baymodel
|
||||
|
@ -58,7 +58,7 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
mock_container) as docker:
|
||||
self.assertEqual(mock_docker, docker)
|
||||
|
||||
mock_get_bay_by_uuid.assert_called_once_with(mock.sentinel.context,
|
||||
mock_get_bay_by_name.assert_called_once_with(mock.sentinel.context,
|
||||
mock_container.bay_uuid)
|
||||
mock_get_baymodel_by_uuid.assert_called_once_with(
|
||||
mock.sentinel.context, mock_bay.baymodel_id)
|
||||
|
@ -73,10 +73,10 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
@mock.patch('magnum.common.docker_utils.DockerHTTPClient')
|
||||
@mock.patch.object(docker_utils, 'cert_manager')
|
||||
@mock.patch.object(docker_utils.objects.BayModel, 'get_by_uuid')
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_uuid')
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_name')
|
||||
@mock.patch.object(docker_utils.objects.Container, 'get_by_uuid')
|
||||
def test_docker_for_container_uuid(self, mock_get_container_by_uuid,
|
||||
mock_get_bay_by_uuid,
|
||||
mock_get_bay_by_name,
|
||||
mock_get_baymodel_by_uuid,
|
||||
mock_cert_manager,
|
||||
mock_docker_client):
|
||||
|
@ -85,7 +85,7 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
mock_get_container_by_uuid.return_value = mock_container
|
||||
mock_bay = mock.MagicMock()
|
||||
mock_bay.api_address = 'https://1.2.3.4:2376'
|
||||
mock_get_bay_by_uuid.return_value = mock_bay
|
||||
mock_get_bay_by_name.return_value = mock_bay
|
||||
mock_baymodel = mock.MagicMock()
|
||||
mock_baymodel.tls_disabled = False
|
||||
mock_get_baymodel_by_uuid.return_value = mock_baymodel
|
||||
|
@ -106,7 +106,7 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
mock_get_container_by_uuid.assert_called_once_with(
|
||||
mock.sentinel.context, mock_container.uuid
|
||||
)
|
||||
mock_get_bay_by_uuid.assert_called_once_with(mock.sentinel.context,
|
||||
mock_get_bay_by_name.assert_called_once_with(mock.sentinel.context,
|
||||
mock_container.bay_uuid)
|
||||
mock_get_baymodel_by_uuid.assert_called_once_with(
|
||||
mock.sentinel.context, mock_bay.baymodel_id)
|
||||
|
@ -120,14 +120,14 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
|
||||
@mock.patch('magnum.common.docker_utils.DockerHTTPClient')
|
||||
@mock.patch.object(docker_utils.objects.BayModel, 'get_by_uuid')
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_uuid')
|
||||
def test_docker_for_container_tls_disabled(self, mock_get_bay_by_uuid,
|
||||
@mock.patch.object(docker_utils.objects.Bay, 'get_by_name')
|
||||
def test_docker_for_container_tls_disabled(self, mock_get_bay_by_name,
|
||||
mock_get_baymodel_by_uuid,
|
||||
mock_docker_client):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_bay = mock.MagicMock()
|
||||
mock_bay.api_address = 'tcp://1.2.3.4:2376'
|
||||
mock_get_bay_by_uuid.return_value = mock_bay
|
||||
mock_get_bay_by_name.return_value = mock_bay
|
||||
mock_baymodel = mock.MagicMock()
|
||||
mock_baymodel.tls_disabled = True
|
||||
mock_get_baymodel_by_uuid.return_value = mock_baymodel
|
||||
|
@ -138,7 +138,7 @@ class TestDockerUtils(base.BaseTestCase):
|
|||
mock_container) as docker:
|
||||
self.assertEqual(mock_docker, docker)
|
||||
|
||||
mock_get_bay_by_uuid.assert_called_once_with(mock.sentinel.context,
|
||||
mock_get_bay_by_name.assert_called_once_with(mock.sentinel.context,
|
||||
mock_container.bay_uuid)
|
||||
mock_get_baymodel_by_uuid.assert_called_once_with(
|
||||
mock.sentinel.context, mock_bay.baymodel_id)
|
||||
|
|
|
@ -42,8 +42,9 @@ class TestK8sConductor(base.TestCase):
|
|||
def mock_baymodel(self):
|
||||
return objects.BayModel({})
|
||||
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
@patch('ast.literal_eval')
|
||||
def test_pod_create_with_success(self, mock_ast):
|
||||
def test_pod_create_with_success(self, mock_ast, mock_retrieve):
|
||||
expected_pod = mock.MagicMock()
|
||||
expected_pod.uuid = 'test-uuid'
|
||||
expected_pod.name = 'test-name'
|
||||
|
@ -58,7 +59,8 @@ class TestK8sConductor(base.TestCase):
|
|||
(mock_kube_api.return_value.create_namespaced_pod
|
||||
.assert_called_once_with(body=manifest, namespace='default'))
|
||||
|
||||
def test_pod_create_with_fail(self):
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
def test_pod_create_with_fail(self, mock_retrieve):
|
||||
expected_pod = mock.MagicMock()
|
||||
manifest = {"key": "value"}
|
||||
expected_pod.manifest = '{"key": "value"}'
|
||||
|
@ -76,7 +78,8 @@ class TestK8sConductor(base.TestCase):
|
|||
.assert_called_once_with(body=manifest,
|
||||
namespace='default'))
|
||||
|
||||
def test_pod_create_fail_on_existing_pod(self):
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
def test_pod_create_fail_on_existing_pod(self, retrieve_bay):
|
||||
expected_pod = mock.MagicMock()
|
||||
expected_pod.manifest = '{"key": "value"}'
|
||||
|
||||
|
@ -173,9 +176,13 @@ class TestK8sConductor(base.TestCase):
|
|||
.assert_called_once_with(
|
||||
name=mock_pod.name, body={}, namespace='default'))
|
||||
|
||||
@patch('magnum.objects.Bay.get_by_name')
|
||||
@patch('magnum.conductor.k8s_api.create_k8s_api')
|
||||
@patch('ast.literal_eval')
|
||||
def test_service_create_with_success(self, mock_ast, mock_kube_api):
|
||||
def test_service_create_with_success(self,
|
||||
mock_ast,
|
||||
mock_kube_api,
|
||||
mock_get_bay):
|
||||
fake_service = mock.MagicMock()
|
||||
fake_service.name = 'test-name'
|
||||
|
||||
|
@ -193,7 +200,8 @@ class TestK8sConductor(base.TestCase):
|
|||
(mock_kube_api.return_value.create_namespaced_service
|
||||
.assert_called_once_with(body=manifest, namespace='default'))
|
||||
|
||||
def test_service_create_with_failure(self):
|
||||
@patch('magnum.objects.Bay.get_by_name')
|
||||
def test_service_create_with_failure(self, mock_get_bay):
|
||||
expected_service = mock.MagicMock()
|
||||
expected_service.create = mock.MagicMock()
|
||||
manifest = {"key": "value"}
|
||||
|
@ -299,8 +307,9 @@ class TestK8sConductor(base.TestCase):
|
|||
.assert_called_once_with(
|
||||
name=mock_service.name, namespace='default'))
|
||||
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
@patch('ast.literal_eval')
|
||||
def test_rc_create_with_success(self, mock_ast):
|
||||
def test_rc_create_with_success(self, mock_ast, mock_retrieve):
|
||||
expected_rc = mock.MagicMock()
|
||||
manifest = {"key": "value"}
|
||||
expected_rc.name = 'test-name'
|
||||
|
@ -316,7 +325,8 @@ class TestK8sConductor(base.TestCase):
|
|||
.create_namespaced_replication_controller
|
||||
.assert_called_once_with(body=manifest, namespace='default'))
|
||||
|
||||
def test_rc_create_with_failure(self):
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
def test_rc_create_with_failure(self, mock_retrieve):
|
||||
expected_rc = mock.MagicMock()
|
||||
manifest = {"key": "value"}
|
||||
expected_rc.manifest = '{"key": "value"}'
|
||||
|
|
|
@ -60,11 +60,9 @@ class TestK8sAPI(base.TestCase):
|
|||
@patch(
|
||||
'magnum.conductor.handlers.common.cert_manager.get_bay_ca_certificate')
|
||||
@patch('magnum.conductor.handlers.common.cert_manager.get_bay_magnum_cert')
|
||||
@patch('magnum.conductor.utils.retrieve_bay')
|
||||
@patch('magnum.common.pythonk8sclient.swagger_client.api_client.ApiClient')
|
||||
def _test_create_k8s_api(self, bay_uuid,
|
||||
def test_create_k8s_api(self,
|
||||
mock_api_client,
|
||||
mock_bay_retrieval,
|
||||
mock_get_bay_magnum_cert,
|
||||
mock_get_bay_ca_cert):
|
||||
bay_obj = mock.MagicMock()
|
||||
|
@ -72,7 +70,6 @@ class TestK8sAPI(base.TestCase):
|
|||
bay_obj.api_address = 'fake-k8s-api-endpoint'
|
||||
bay_obj.magnum_cert_ref = 'fake-magnum-cert-ref'
|
||||
bay_obj.ca_cert_ref = 'fake-ca-cert-ref'
|
||||
mock_bay_retrieval.return_value = bay_obj
|
||||
|
||||
mock_get_bay_magnum_cert.return_value = self._mock_cert_mgr_get_cert(
|
||||
'fake-magnum-cert-ref')
|
||||
|
@ -88,24 +85,10 @@ class TestK8sAPI(base.TestCase):
|
|||
with patch(
|
||||
'magnum.conductor.k8s_api.K8sAPI._create_temp_file_with_content',
|
||||
side_effect=self._mock_named_file_creation):
|
||||
k8s_api.create_k8s_api(context, bay_uuid)
|
||||
|
||||
mock_bay_retrieval.assert_called_once_with(context, bay_uuid)
|
||||
k8s_api.create_k8s_api(context, bay_obj)
|
||||
|
||||
mock_api_client.assert_called_once_with(
|
||||
bay_obj.api_address,
|
||||
key_file='priv-key-temp-file-name',
|
||||
cert_file='cert-temp-file-name',
|
||||
ca_certs='ca-cert-temp-file-name')
|
||||
|
||||
def test_create_k8s_api_with_service(self):
|
||||
self._test_create_k8s_api('bay-uuid')
|
||||
|
||||
def test_create_k8s_api_with_bay(self):
|
||||
self._test_create_k8s_api('Bay')
|
||||
|
||||
def test_create_k8s_api_with_pod(self):
|
||||
self._test_create_k8s_api('bay-uuid')
|
||||
|
||||
def test_create_k8s_api_with_rc(self):
|
||||
self._test_create_k8s_api('bay-uuid')
|
||||
|
|
|
@ -61,7 +61,7 @@ class TestScaleManager(base.TestCase):
|
|||
self.assertEqual(expected_removal_hosts, removal_hosts)
|
||||
if num_of_removal > 0:
|
||||
mock_create_k8s_api.assert_called_once_with(mock_context,
|
||||
mock_bay.uuid)
|
||||
mock_bay)
|
||||
|
||||
@mock.patch('magnum.objects.Bay.get_by_uuid')
|
||||
@mock.patch('magnum.conductor.scale_manager.ScaleManager._is_scale_down')
|
||||
|
|
Loading…
Reference in New Issue