Fix Kubernetes service resource check

This patch fixes the Kubernetes service resource check performed
after instantiate of CNF. It modifies the resource check to retry
if the endpoint is not created after the service has been created
because it may take time a bit to create the endpoint. This error
may occur when the VNF package to be instantiated is large.

Also, this patch fixes some typos in the V1 API, changing
"endpoinds" to "endpoints".

Closes-Bug: #2078288
Change-Id: I3f1b95c72a1ca0943ca13b8b4b56ace181e53598
This commit is contained in:
Ken Fujimoto 2024-08-26 06:37:41 +00:00
parent e438778acf
commit 07e7f30e9a
5 changed files with 39 additions and 16 deletions

View File

@ -153,7 +153,7 @@ class InitApiFalse(TackerException):
message = _('Failed to init resource.')
class ReadEndpoindsFalse(TackerException):
class ReadEndpointsFalse(TackerException):
message = _('The method to read a resource failed.')

View File

@ -349,9 +349,12 @@ class Service(NamespacedResource):
if endpoint_info:
return True
except Exception as ex:
sol_title = "Read Endpoint failed"
raise sol_ex.K8sOperationFailed(sol_title=sol_title,
sol_detail=str(ex))
if isinstance(ex, client.ApiException) and ex.status == 404:
return False
else:
sol_title = "Read Endpoint failed"
raise sol_ex.K8sOperationFailed(sol_title=sol_title,
sol_detail=str(ex))
class ServiceAccount(NamespacedResource):

View File

@ -350,7 +350,7 @@ def fake_service_false():
)
def fake_endpoinds():
def fake_endpoints():
return client.V1Endpoints(
api_version='v1',
kind='Endpoints',

View File

@ -121,10 +121,10 @@ class TestKubernetes(base.TestCase):
@mock.patch.object(client.CoreV1Api, 'read_namespaced_service')
@mock.patch.object(client.CoreV1Api, 'read_namespaced_endpoints')
def test_create_wait_k8s_success_service(
self, mock_endpoinds, mock_read_service):
self, mock_endpoints, mock_read_service):
k8s_objs = fakes.fake_k8s_objs_service()
k8s_client_dict = self.k8s_client_dict
mock_endpoinds.return_value = fakes.fake_endpoinds()
mock_endpoints.return_value = fakes.fake_endpoints()
mock_read_service.return_value = fakes.fake_service()
checked_objs = self.kubernetes.\
create_wait_k8s(k8s_objs, k8s_client_dict,
@ -134,22 +134,38 @@ class TestKubernetes(base.TestCase):
@mock.patch.object(client.CoreV1Api, 'read_namespaced_service')
@mock.patch.object(client.CoreV1Api, 'read_namespaced_endpoints')
def test_create_wait_k8s_failure_service(
self, mock_endpoinds, mock_read_service):
self, mock_endpoints, mock_read_service):
k8s_objs = fakes.fake_k8s_objs_service_false_cluster_ip()
k8s_client_dict = self.k8s_client_dict
mock_endpoinds.return_value = None
mock_endpoints.return_value = None
mock_read_service.return_value = fakes.fake_service_false()
self.assertRaises(vnfm.CNFCreateWaitFailed,
self.kubernetes.create_wait_k8s,
k8s_objs, k8s_client_dict, self.vnf_instance)
@mock.patch.object(client.CoreV1Api, 'read_namespaced_service')
def test_create_wait_k8s_failure_service_read_endpoinds(
self, mock_read_service):
@mock.patch.object(client.CoreV1Api, 'read_namespaced_endpoints')
def test_create_wait_k8s_failure_service_read_endpoints(
self, mock_endpoints, mock_read_service):
k8s_objs = fakes.fake_k8s_objs_service_false_cluster_ip()
k8s_client_dict = self.k8s_client_dict
mock_read_service.return_value = fakes.fake_service()
self.assertRaises(exceptions.ReadEndpoindsFalse,
ex = client.ApiException(status=401)
mock_endpoints.side_effect = ex
self.assertRaises(exceptions.ReadEndpointsFalse,
self.kubernetes.create_wait_k8s,
k8s_objs, k8s_client_dict, self.vnf_instance)
@mock.patch.object(client.CoreV1Api, 'read_namespaced_service')
@mock.patch.object(client.CoreV1Api, 'read_namespaced_endpoints')
def test_create_wait_k8s_failure_service_read_endpoints_not_exist(
self, mock_endpoints, mock_read_service):
k8s_objs = fakes.fake_k8s_objs_service_false_cluster_ip()
k8s_client_dict = self.k8s_client_dict
mock_read_service.return_value = fakes.fake_service()
ex = client.ApiException(status=404)
mock_endpoints.side_effect = ex
self.assertRaises(vnfm.CNFCreateWaitFailed,
self.kubernetes.create_wait_k8s,
k8s_objs, k8s_client_dict, self.vnf_instance)

View File

@ -296,10 +296,14 @@ class Kubernetes(abstract_driver.VnfAbstractDriver,
if endpoint:
status_flag = True
except Exception as e:
msg = _('read endpoinds failed.kind:{kind}.reason:{e}'.format(
kind=service.kind, e=e))
LOG.error(msg)
raise exceptions.ReadEndpoindsFalse(error=msg)
if isinstance(e, client.ApiException) and e.status == 404:
pass
else:
msg = _(
'read endpoints failed.kind:{kind}.reason:{e}'.format(
kind=service.kind, e=e))
LOG.error(msg)
raise exceptions.ReadEndpointsFalse(error=msg)
if status_flag:
k8s_obj['status'] = 'Create_complete'
k8s_obj['message'] = "Service is created"