diff options
author | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2021-06-30 07:34:18 +0000 |
---|---|---|
committer | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2021-06-30 07:36:41 +0000 |
commit | 7cbad7912c0d14cbc06f7491ae77222b4feb23ed (patch) | |
tree | 150caf423df863e2476c1a4cbf10b50cd3bb3ded /src/onaptests/steps/onboard/cds.py | |
parent | fa58b9135151948b4707a53bcab63e8da93c6808 (diff) |
[TEST] Check if cds blueprintsprocessor service type is 'NodePort'
Kubernetes API returns error response on ExposeCDSBlueprintprocessorNodePortStep cleanup if service is already ClusterIP.
Add conditional statement to check if service is still 'NodePort' type.
Issue-ID: TEST-347
Signed-off-by: Michal Jagiello <michal.jagiello@t-mobile.pl>
Change-Id: I406b1910cccace6066e25d865b5aa1b6601efe74
Diffstat (limited to 'src/onaptests/steps/onboard/cds.py')
-rw-r--r-- | src/onaptests/steps/onboard/cds.py | 98 |
1 files changed, 65 insertions, 33 deletions
diff --git a/src/onaptests/steps/onboard/cds.py b/src/onaptests/steps/onboard/cds.py index 3ef37f2..cbd69ce 100644 --- a/src/onaptests/steps/onboard/cds.py +++ b/src/onaptests/steps/onboard/cds.py @@ -3,9 +3,10 @@ from abc import ABC from pathlib import Path -from typing import Optional +from typing import Any, Dict from kubernetes import client, config +from kubernetes.client.exceptions import ApiException from onapsdk.cds import Blueprint, DataDictionarySet from onapsdk.cds.blueprint_processor import Blueprintprocessor from onapsdk.configuration import settings @@ -31,13 +32,34 @@ class ExposeCDSBlueprintprocessorNodePortStep(CDSBaseStep): """Initialize step.""" super().__init__(cleanup=cleanup) self.service_name: str = "cds-blueprints-processor-http" - self.client: Optional[client.CoreV1Api] = None + config.load_kube_config(settings.K8S_CONFIG) + self.k8s_client: client.CoreV1Api = client.CoreV1Api() @property def description(self) -> str: """Step description.""" return "Expose CDS blueprintsprocessor NodePort." + def is_service_node_port_type(self) -> bool: + """Check if CDS blueprints processor service type is 'NodePort' + + Raises: + OnapTestException: Kubernetes API error + + Returns: + bool: True if service type is 'NodePort', False otherwise + + """ + try: + service_data: Dict[str, Any] = self.k8s_client.read_namespaced_service( + self.service_name, + settings.K8S_NAMESPACE + ) + return service_data.spec.type == "NodePort" + except ApiException: + self._logger.exception("Kubernetes API exception") + raise OnapTestException + @BaseStep.store_state def execute(self) -> None: """Expose CDS blueprintprocessor port using kubernetes client. @@ -48,17 +70,21 @@ class ExposeCDSBlueprintprocessorNodePortStep(CDSBaseStep): """ super().execute() - config.load_kube_config(settings.K8S_CONFIG) - self.k8s_client = client.CoreV1Api() - try: - self.k8s_client.patch_namespaced_service( - self.service_name, - settings.K8S_NAMESPACE, - {"spec": {"ports": [{"port": 8080, "nodePort": 30449}], "type": "NodePort"}} - ) - except urllib3.exceptions.HTTPError: - self._logger.exception("Can't connect with k8s") - raise OnapTestException + if not self.is_service_node_port_type(): + try: + self.k8s_client.patch_namespaced_service( + self.service_name, + settings.K8S_NAMESPACE, + {"spec": {"ports": [{"port": 8080, "nodePort": 30449}], "type": "NodePort"}} + ) + except ApiException: + self._logger.exception("Kubernetes API exception") + raise OnapTestException + except urllib3.exceptions.HTTPError: + self._logger.exception("Can't connect with k8s") + raise OnapTestException + else: + self._logger.debug("Service already patched, skip") def cleanup(self) -> None: """Step cleanup. @@ -66,26 +92,32 @@ class ExposeCDSBlueprintprocessorNodePortStep(CDSBaseStep): Restore CDS blueprintprocessor service. """ - try: - self.k8s_client.patch_namespaced_service( - self.service_name, - settings.K8S_NAMESPACE, - [ - { - "op": "remove", - "path": "/spec/ports/0/nodePort" - }, - { - "op": "replace", - "path": "/spec/type", - "value": "ClusterIP" - } - ] - ) - return super().cleanup() - except urllib3.exceptions.HTTPError: - self._logger.exception("Can't connect with k8s") - raise OnapTestException + if self.is_service_node_port_type(): + try: + self.k8s_client.patch_namespaced_service( + self.service_name, + settings.K8S_NAMESPACE, + [ + { + "op": "remove", + "path": "/spec/ports/0/nodePort" + }, + { + "op": "replace", + "path": "/spec/type", + "value": "ClusterIP" + } + ] + ) + except ApiException: + self._logger.exception("Kubernetes API exception") + raise OnapTestException + except urllib3.exceptions.HTTPError: + self._logger.exception("Can't connect with k8s") + raise OnapTestException + else: + self._logger.debug("Service is not 'NodePort' type, skip") + return super().cleanup() class BootstrapBlueprintprocessor(CDSBaseStep): |