Merge "Fix status check of k8s resources"

This commit is contained in:
Zuul 2023-09-11 01:53:30 +00:00 committed by Gerrit Code Review
commit 623abd04b3
4 changed files with 73 additions and 6 deletions

View File

@ -309,8 +309,8 @@ class Service(NamespacedResource):
return False
service_info = self.read()
if service_info.spec.cluster_ip in ['', None] or _check_is_ip(
service_info.spec.cluster_ip):
if (service_info.spec.cluster_ip == "None"
or _check_is_ip(service_info.spec.cluster_ip)):
try:
endpoint_info = self.k8s_client.read_namespaced_endpoints(
namespace=self.namespace, name=self.name)
@ -463,9 +463,11 @@ class StatefulSet(NamespacedResource):
statefulset_info = self.read()
replicas = statefulset_info.status.replicas
if replicas == statefulset_info.status.ready_replicas:
volume_claim_templates = (
statefulset_info.spec.volume_claim_templates)
if volume_claim_templates is None:
return True
for i in range(0, statefulset_info.spec.replicas):
volume_claim_templates = (
statefulset_info.spec.volume_claim_templates)
for volume_claim_template in volume_claim_templates:
pvc_name = "-".join(
[volume_claim_template.metadata.name,

View File

@ -316,7 +316,7 @@ def fake_service():
namespace='default'
),
spec=client.V1ServiceSpec(
cluster_ip=''
cluster_ip='None'
)
)
@ -420,6 +420,18 @@ def fake_k8s_objs_stateful_set():
return obj
def fake_k8s_objs_stateful_set_no_volume_claim_templates():
obj = [
{
'namespace': 'test',
'status': 'Creating',
'object': fake_v1_stateful_set_no_volume_claim_templates()
}
]
return obj
def fake_k8s_objs_stateful_set_error():
obj = [
{
@ -696,6 +708,37 @@ def fake_v1_stateful_set():
)
def fake_v1_stateful_set_no_volume_claim_templates():
client_config = client.Configuration.get_default_copy()
client_config.client_side_validation = False
return client.V1StatefulSet(
api_version='apps/v1',
kind='StatefulSet',
metadata=client.V1ObjectMeta(
name='curry-test001',
namespace='curryns'
),
spec=client.V1StatefulSetSpec(
replicas=1,
selector=client.V1LabelSelector(
match_labels={'app': 'nginx'}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(
name='curry-test001',
namespace='curryns'
)
),
service_name='nginx'
),
status=client.V1StatefulSetStatus(
replicas=1,
ready_replicas=1,
local_vars_configuration=client_config
),
)
def fake_v1_stateful_set_error():
client_config = client.Configuration.get_default_copy()
client_config.client_side_validation = False

View File

@ -257,6 +257,24 @@ class TestKubernetes(base.TestCase):
self.assertEqual(flag, True)
@mock.patch.object(client.AppsV1Api, 'read_namespaced_stateful_set')
def test_create_wait_k8s_stateful_set_no_volume_claim_templates(
self, mock_read_namespaced_stateful_set):
k8s_objs = fakes.fake_k8s_objs_stateful_set_no_volume_claim_templates()
k8s_client_dict = self.k8s_client_dict
stateful_set_obj = fakes. \
fake_v1_stateful_set_no_volume_claim_templates()
mock_read_namespaced_stateful_set.return_value = stateful_set_obj
checked_objs = self.kubernetes. \
create_wait_k8s(k8s_objs, k8s_client_dict,
self.vnf_instance)
flag = True
for obj in checked_objs:
if obj.get('status') != 'Create_complete':
flag = False
self.assertEqual(flag, True)
@mock.patch.object(client.CoreV1Api,
'read_namespaced_persistent_volume_claim')
@mock.patch.object(client.AppsV1Api, 'read_namespaced_stateful_set')

View File

@ -340,6 +340,10 @@ class Kubernetes(abstract_driver.VnfAbstractDriver,
for i in range(0, stateful_set.spec.replicas):
volume_claim_templates = stateful_set.spec.\
volume_claim_templates
if volume_claim_templates is None:
k8s_obj['status'] = 'Create_complete'
k8s_obj['message'] = 'StatefulSet is created'
break
for volume_claim_template in volume_claim_templates:
pvc_name = "-".join(
[volume_claim_template.metadata.name, name, str(i)])
@ -376,7 +380,7 @@ class Kubernetes(abstract_driver.VnfAbstractDriver,
service = k8s_client_dict[api_version].read_namespaced_service(
namespace=namespace, name=name)
status_flag = False
if service.spec.cluster_ip in ['', None] or \
if service.spec.cluster_ip == "None" or \
self._check_is_ip(service.spec.cluster_ip):
try:
endpoint = k8s_client_dict['v1'].\