Code refactoring in conductor/k8s_api.py
This patch removes redundant method create_certificate_files defined in magnum/conductor/k8s_api.py and uses the one defined in magnum/conductor/handlers/common/cert_manager.py Change-Id: I509fd24b3a6febd1b621321c380f38afdfaccec2 Closes-bug: #1505085
This commit is contained in:
@@ -18,8 +18,7 @@ from k8sclient.client import api_client
|
||||
from k8sclient.client.apis import apiv_api
|
||||
from oslo_log import log as logging
|
||||
|
||||
from magnum.conductor.handlers.common import cert_manager
|
||||
|
||||
from magnum.conductor.handlers.common.cert_manager import create_client_files
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
@@ -47,7 +46,8 @@ class K8sAPI(apiv_api.ApivApi):
|
||||
self.key_file = None
|
||||
|
||||
if bay.magnum_cert_ref:
|
||||
self._create_certificate_files(bay)
|
||||
(self.ca_file, self.key_file,
|
||||
self.cert_file) = create_client_files(bay)
|
||||
|
||||
# build a connection with Kubernetes master
|
||||
client = api_client.ApiClient(bay.api_address,
|
||||
@@ -57,21 +57,6 @@ class K8sAPI(apiv_api.ApivApi):
|
||||
|
||||
super(K8sAPI, self).__init__(client)
|
||||
|
||||
def _create_certificate_files(self, bay):
|
||||
"""Read certificate and key for a bay and stores in files.
|
||||
|
||||
:param bay: Bay object
|
||||
"""
|
||||
magnum_cert_obj = cert_manager.get_bay_magnum_cert(bay)
|
||||
self.cert_file = self._create_temp_file_with_content(
|
||||
magnum_cert_obj.get_certificate())
|
||||
private_key = magnum_cert_obj.get_decrypted_private_key()
|
||||
self.key_file = self._create_temp_file_with_content(
|
||||
private_key)
|
||||
ca_cert_obj = cert_manager.get_bay_ca_certificate(bay)
|
||||
self.ca_file = self._create_temp_file_with_content(
|
||||
ca_cert_obj.get_certificate())
|
||||
|
||||
def __del__(self):
|
||||
if self.ca_file:
|
||||
self.ca_file.close()
|
||||
|
||||
@@ -13,9 +13,7 @@
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
from mock import patch
|
||||
|
||||
from magnum.conductor import k8s_api
|
||||
from magnum.tests import base
|
||||
|
||||
|
||||
@@ -43,9 +41,6 @@ class TestK8sAPI(base.TestCase):
|
||||
'private-key-content': 'priv-key-temp-file-name'
|
||||
}
|
||||
|
||||
def _mock_named_file_creation(self, content):
|
||||
return TestK8sAPI.file_dict[content]
|
||||
|
||||
def _mock_cert_mgr_get_cert(self, cert_ref, **kwargs):
|
||||
cert_obj = mock.MagicMock()
|
||||
cert_obj.get_certificate.return_value = (
|
||||
@@ -56,39 +51,3 @@ class TestK8sAPI(base.TestCase):
|
||||
TestK8sAPI.content_dict[cert_ref]['decrypted_private_key'])
|
||||
|
||||
return cert_obj
|
||||
|
||||
@patch(
|
||||
'magnum.conductor.handlers.common.cert_manager.get_bay_ca_certificate')
|
||||
@patch('magnum.conductor.handlers.common.cert_manager.get_bay_magnum_cert')
|
||||
@patch('k8sclient.client.api_client.ApiClient')
|
||||
def test_create_k8s_api(self,
|
||||
mock_api_client,
|
||||
mock_get_bay_magnum_cert,
|
||||
mock_get_bay_ca_cert):
|
||||
bay_obj = mock.MagicMock()
|
||||
bay_obj.uuid = 'bay-uuid'
|
||||
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_get_bay_magnum_cert.return_value = self._mock_cert_mgr_get_cert(
|
||||
'fake-magnum-cert-ref')
|
||||
mock_get_bay_ca_cert.return_value = self._mock_cert_mgr_get_cert(
|
||||
'fake-ca-cert-ref')
|
||||
|
||||
file_dict = TestK8sAPI.file_dict
|
||||
for content in file_dict.keys():
|
||||
file_hdl = file_dict[content]
|
||||
file_hdl.name = TestK8sAPI.file_name[content]
|
||||
|
||||
context = 'context'
|
||||
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_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')
|
||||
|
||||
Reference in New Issue
Block a user