Skip to content

Commit

Permalink
cleanup on module level and cleanup on ressource conflict
Browse files Browse the repository at this point in the history
Signed-off-by: Katharina Trentau <[email protected]>
  • Loading branch information
Katharina Trentau committed Oct 2, 2024
1 parent 8901137 commit 3afa301
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
14 changes: 9 additions & 5 deletions Tests/kaas/k8s-default-storage-class/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ def gen_sonobuoy_result_file(error_n: int, error_msg: str, test_file_name: str):
result_file["status"] = test_status
result_file["details"]["messages"] = error_msg

# directory_path = os.path.dirname(f"./{test_name}.result.yaml")
# os.makedirs(directory_path, exist_ok=True)

with open(f"./{test_name}.result.yaml", "w") as file:
script_directory = os.path.dirname(os.path.abspath(__file__))
with open(f"{script_directory}/{test_name}.result.yaml", "w") as file:
yaml.dump(result_file, file)

# else:
# result_file = manual_result_file_template
# result_file["name"] = test_name
# result_file["status"] = test_status
# script_directory = os.path.dirname(os.path.abspath(__file__))
# with open(f"{script_directory}/{test_name}.result.yaml", "w") as file:
# yaml.dump(result_file, file)
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import os

from kubernetes import client
from kubernetes.client.rest import ApiException
from helper import gen_sonobuoy_result_file
from helper import SCSTestException
from helper import initialize_logging
Expand All @@ -39,6 +40,10 @@
logger = logging.getLogger("k8s-default-storage-class-check")

NUM_RETRIES = 30
NAMESPACE = "default"
PVC_NAME = "test-pvc"
PV_NAME = "test-pv"
POD_NAME = "test-pod"

def check_default_storageclass(k8s_client_storage):
api_response = k8s_client_storage.list_storage_class(_preload_content=False)
Expand Down Expand Up @@ -73,6 +78,13 @@ def check_default_storageclass(k8s_client_storage):
logger.info(f"One default Storage Class found:'{default_storage_class}'")
return default_storage_class

def cleanup(k8s_api_instance,namespace, pod_name, pvc_name):
logger.debug(f"delete pod:{pod_name}")
api_response = k8s_api_instance.delete_namespaced_pod(pod_name, namespace)
logger.debug(f"delete pvc:{pvc_name}")
api_response = k8s_api_instance.delete_namespaced_persistent_volume_claim(
pvc_name, namespace
)

def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_class):
"""
Expand All @@ -81,25 +93,10 @@ def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_
3. Check if PV got succesfully created using ReadWriteOnce
4. Delete resources used for testing
"""

# 0. Define CleanUp
def cleanup():
logger.debug(f"delete pod:{pod_name}")
api_response = k8s_api_instance.delete_namespaced_pod(pod_name, namespace)
logger.debug(f"delete pvc:{pvc_name}")
api_response = k8s_api_instance.delete_namespaced_persistent_volume_claim(
pvc_name, namespace
)

namespace = "default"
pvc_name = "test-pvc"
pv_name = "test-pv"
pod_name = "test-pod"

# 1. Create PersistantVolumeClaim
logger.debug(f"create pvc: {pvc_name}")
logger.debug(f"create pvc: {PVC_NAME}")

pvc_meta = client.V1ObjectMeta(name=pvc_name)
pvc_meta = client.V1ObjectMeta(name=PVC_NAME)
pvc_resources = client.V1ResourceRequirements(
requests={"storage": "1Gi"},
)
Expand All @@ -113,42 +110,42 @@ def cleanup():
)

api_response = k8s_api_instance.create_namespaced_persistent_volume_claim(
namespace, body_pvc
NAMESPACE, body_pvc
)

# 2. Create a pod which makes use of the PersitantVolumeClaim
logger.debug(f"create pod: {pod_name}")
logger.debug(f"create pod: {POD_NAME}")

pod_vol = client.V1Volume(
name=pv_name,
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(pvc_name),
name=PV_NAME,
persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(PVC_NAME),
)
pod_con = client.V1Container(
name="nginx",
image="nginx",
ports=[client.V1ContainerPort(container_port=80)],
volume_mounts=[
client.V1VolumeMount(name=pv_name, mount_path="/usr/share/nginx/html")
client.V1VolumeMount(name=PV_NAME, mount_path="/usr/share/nginx/html")
],
)
pod_spec = client.V1PodSpec(volumes=[pod_vol], containers=[pod_con])
pod_body = client.V1Pod(
api_version="v1",
kind="Pod",
metadata=client.V1ObjectMeta(name=pod_name),
metadata=client.V1ObjectMeta(name=POD_NAME),
spec=pod_spec,
)

api_response = k8s_api_instance.create_namespaced_pod(
namespace, pod_body, _preload_content=False
NAMESPACE, pod_body, _preload_content=False
)
pod_info = json.loads(api_response.read().decode("utf-8"))
pod_status = pod_info["status"]["phase"]

retries = 0
while pod_status != "Running" and retries <= NUM_RETRIES:
api_response = k8s_api_instance.read_namespaced_pod(
pod_name, namespace, _preload_content=False
POD_NAME, NAMESPACE, _preload_content=False
)
pod_info = json.loads(api_response.read().decode("utf-8"))
pod_status = pod_info["status"]["phase"]
Expand All @@ -158,9 +155,9 @@ def cleanup():

# assert pod_status == "Running"
if pod_status != "Running":
cleanup(k8s_api_instance, NAMESPACE, POD_NAME, PVC_NAME)
raise SCSTestException(
"pod is not Running not able to setup test Enviornment",
cleanup(),
return_code=13,
)

Expand All @@ -175,25 +172,25 @@ def cleanup():
logger.debug("searching for corresponding pv")
for pv in pv_list:
logger.debug(f"parsing pv: {pv['metadata']['name']}")
if pv["spec"]["claimRef"]["name"] == pvc_name:
logger.debug(f"found pv to pvc: {pvc_name}")
if pv["spec"]["claimRef"]["name"] == PVC_NAME:
logger.debug(f"found pv to pvc: {PVC_NAME}")

if pv["status"]["phase"] != "Bound":
raise SCSTestException(
"Not able to bind pv to pvc",
cleanup(),
cleanup(k8s_api_instance, NAMESPACE, POD_NAME, PVC_NAME),
return_code=41,
)

if "ReadWriteOnce" not in pv["spec"]["accessModes"]:
raise SCSTestException(
"access mode 'ReadWriteOnce' is not supported",
cleanup(),
cleanup(k8s_api_instance, NAMESPACE, POD_NAME, PVC_NAME),
return_code=42,
)

# 4. Delete resources used for testing
cleanup()
cleanup(k8s_api_instance, NAMESPACE, POD_NAME, PVC_NAME)

return 0

Expand Down Expand Up @@ -227,7 +224,7 @@ def main(argv):
logger.debug("setup_k8s_client(kubeconfig)")
k8s_core_api, k8s_storage_api = setup_k8s_client(kubeconfig)
except Exception as exception_message:
logger.info(f"{exception_message}")
logger.info(f"L228 {exception_message}")
return_message = f"{exception_message}"
return_code = 1

Expand All @@ -236,11 +233,11 @@ def main(argv):
logger.info("check_default_storageclass()")
default_class_name = check_default_storageclass(k8s_storage_api)
except SCSTestException as test_exception:
logger.info(f"{test_exception}")
logger.info(f"L237 {test_exception}")
return_message = f"{test_exception}"
return_code = test_exception.return_code
except Exception as exception_message:
logger.info(f"{exception_message}")
logger.info(f"L241 {exception_message}")
return_message = f"{exception_message}"
return_code = 1

Expand All @@ -251,9 +248,16 @@ def main(argv):
k8s_core_api, default_class_name
)
except SCSTestException as test_exception:
logger.info(f"{test_exception}")
logger.info(f"L252 {test_exception}")
return_message = f"{test_exception}"
return_code = test_exception.return_code
except ApiException as api_exception:
if api_exception.status == 409:
print("(409) conflicting resources, try to cleaning up left overs")
cleanup(k8s_core_api, NAMESPACE, POD_NAME, PVC_NAME)
else:
print(f"An API error occurred: {api_exception}")
return_code = 1
except Exception as exception_message:
logger.info(f"{exception_message}")
return_message = f"{exception_message}"
Expand Down

0 comments on commit 3afa301

Please sign in to comment.