From 7628927d601e0998533dbb852dc422b7af434c3e Mon Sep 17 00:00:00 2001 From: Toni Finger Date: Thu, 7 Sep 2023 11:52:36 +0200 Subject: [PATCH] Apply k8s-default-storage-class as sonobuoy plugin Signed-off-by: Toni Finger --- .gitignore | 2 + .../Dockerfile_sonobuoy_plugin | 21 +++++++ .../kaas/k8s-default-storage-class/helper.py | 35 ++++++++++++ .../k8s-default-storage-class-check.py | 20 +++++++ .../k8s-default-storage-class-plugin.yaml | 13 +++++ .../k8s-default-storage-class/run_checks.sh | 55 +++++++++++++++++++ 6 files changed, 146 insertions(+) create mode 100644 Tests/kaas/k8s-default-storage-class/Dockerfile_sonobuoy_plugin create mode 100644 Tests/kaas/k8s-default-storage-class/helper.py rename Tests/kaas/{ => k8s-default-storage-class}/k8s-default-storage-class-check.py (93%) create mode 100644 Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-plugin.yaml create mode 100755 Tests/kaas/k8s-default-storage-class/run_checks.sh diff --git a/.gitignore b/.gitignore index ad0a4485d..320bc8f1c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .idea .DS_Store node_modules +Tests/kaas/results/ +*.tar.gz diff --git a/Tests/kaas/k8s-default-storage-class/Dockerfile_sonobuoy_plugin b/Tests/kaas/k8s-default-storage-class/Dockerfile_sonobuoy_plugin new file mode 100644 index 000000000..bf15cfad0 --- /dev/null +++ b/Tests/kaas/k8s-default-storage-class/Dockerfile_sonobuoy_plugin @@ -0,0 +1,21 @@ +FROM ubuntu + +# Install kubectl +# Note: Latest version may be found on: +# https://aur.archlinux.org/packages/kubectl-bin/ +ADD https://storage.googleapis.com/kubernetes-release/release/v1.14.1/bin/linux/amd64/kubectl /usr/local/bin/kubectl + +ENV HOME=/config + +# Basic check it works. +RUN apt-get update && \ + apt-get -y install net-tools && \ + apt-get -y install curl && \ + chmod +x /usr/local/bin/kubectl && \ + kubectl version --client + + +COPY ./ ./ +RUN chmod +x ./run_checks.sh + +ENTRYPOINT ["./run_checks.sh"] diff --git a/Tests/kaas/k8s-default-storage-class/helper.py b/Tests/kaas/k8s-default-storage-class/helper.py new file mode 100644 index 000000000..dbf863b37 --- /dev/null +++ b/Tests/kaas/k8s-default-storage-class/helper.py @@ -0,0 +1,35 @@ +import getopt +import sys +import time +import json +import logging +import yaml + +manual_result_file_template = { + 'name': None, + 'status': None, + 'details':{ + #'stdout': "stdout from the test" + 'messages': None + # - message from the test + # - another message + } + } + +def gen_sonobuoy_result_file(error_n: int, error_msg: str, test_file_name: str): + + test_name = test_file_name.replace(".py", "") + + test_status="passed" + + if error_n != 0 : + test_status = test_name + "_" + str(error_n) + + result_file = manual_result_file_template + + result_file['name'] = test_name + result_file['status'] = test_status + result_file['details']['messages'] = error_msg + + with open(f'./{test_name}.result.yaml', 'w') as file: + documents = yaml.dump(result_file, file) diff --git a/Tests/kaas/k8s-default-storage-class-check.py b/Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-check.py similarity index 93% rename from Tests/kaas/k8s-default-storage-class-check.py rename to Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-check.py index ef2df0d06..d13a7e27f 100644 --- a/Tests/kaas/k8s-default-storage-class-check.py +++ b/Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-check.py @@ -28,6 +28,7 @@ from kubernetes import client, config from kubernetes.client.rest import ApiException +from helper import gen_sonobuoy_result_file import logging.config @@ -225,6 +226,8 @@ def check_default_persistentvolumeclaim_readwriteonce(k8s_api_instance, storage_ api_response = k8s_api_instance.delete_namespaced_persistent_volume_claim( pvc_name, namespace ) + + return 0 def main(argv): @@ -232,6 +235,7 @@ def main(argv): initialize_logging() error_count = 0 return_code = 0 + return_message = "return_message: FAILED" try: opts, args = getopt.gnu_getopt(argv, "k:h", ["kubeconfig=", "help"]) @@ -251,23 +255,31 @@ def main(argv): else: print_usage(kubeconfig) return 2 + + print(return_code, return_message, __file__) # Setup kubernetes client try: logger.debug("setup_k8s_client(kubeconfig)") k8s_core_api, k8s_storage_api = setup_k8s_client(kubeconfig) except Exception: + logger.info(f"{exception_message}") + return_message = f"{exception_message}" return_code = 1 + print(return_code, return_message, __file__) + # Check if default storage class is defined (MENTETORY) try: logger.info("check_default_storageclass()") default_class_name = check_default_storageclass(k8s_storage_api) except SCSTestException as test_exception: logger.info(f"{test_exception}") + return_message = f"{test_exception}" return_code = test_exception.return_code except Exception as exception_message: logger.info(f"{exception_message}") + return_message = f"{exception_messagev}" return_code = 1 # Check if default_persistent volume has ReadWriteOnce defined (MENTETORY) @@ -276,12 +288,20 @@ def main(argv): return_code = check_default_persistentvolumeclaim_readwriteonce(k8s_core_api, default_class_name) except SCSTestException as test_exception: logger.info(f"{test_exception}") + return_message = f"{test_exception}" return_code = test_exception.return_code except Exception as exception_message: logger.info(f"{exception_message}") + return_message = f"{exception_message}" return_code = 1 logger.debug(f"return_code:{return_code}") + + if return_code == 0: + return_message = f"the tests passed" + + gen_sonobuoy_result_file(return_code, return_message, __file__) + return return_code diff --git a/Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-plugin.yaml b/Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-plugin.yaml new file mode 100644 index 000000000..2918d8d7e --- /dev/null +++ b/Tests/kaas/k8s-default-storage-class/k8s-default-storage-class-plugin.yaml @@ -0,0 +1,13 @@ +sonobuoy-config: + driver: Job + plugin-name: k8s-default-storage-class + result-format: manual + resutl-file: k8s-default-storage-class-check.result.yaml + +spec: + args: + - k8s-default-storage-class-check + command: + - ./run_checks.sh + image: ghcr.io/sovereigncloudstack/standars/k8s-default-storage-class:v0.1.2 + name: k8s-default-storage-class diff --git a/Tests/kaas/k8s-default-storage-class/run_checks.sh b/Tests/kaas/k8s-default-storage-class/run_checks.sh new file mode 100755 index 000000000..6205a8594 --- /dev/null +++ b/Tests/kaas/k8s-default-storage-class/run_checks.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +############################################################################### +##### HELPERS ##### +############################################################################### + +set -x + +# This is the entrypoint for the image and meant to wrap the +# logic of gathering/reporting results to the Sonobuoy worker. + +results_dir="${RESULTS_DIR:-/tmp/results}" +mkdir -p ${results_dir} + +# saveResults prepares the results for handoff to the Sonobuoy worker. +# See: https://github.com/vmware-tanzu/sonobuoy/blob/main/site/content/docs/main/plugins.md +saveResults() { + cd ${results_dir} + echo ${results_dir} + + # Sonobuoy worker expects a tar file. + tar czf results.tar.gz * + + # Signal to the worker that we are done and where to find the results. + printf ${results_dir}/results.tar.gz > ${results_dir}/done +} + +# Ensure that we tell the Sonobuoy worker we are done regardless of results. +trap saveResults EXIT + + +############################################################################### +##### RUN TEST SCRIPTS ##### +############################################################################### + +# Each script name is expected to be given as an arg. If no args, error out +# but print one result file for clarity in the results. +if [ "$#" -eq "0" ]; then + echo "No arguments; expects each argument to be script name" > ${results_dir}/out + exit 1 +fi + +# Iterate through the python tests passed as arguments +i=0 +while [ "$1" != "" ]; do + # Run each arg as a command and save the output in the results directory. + echo "run testscript: [$1.py]" + + python $1.py > ${results_dir}/out_$1 + cp $1.result.yaml ${results_dir} + i=$((i + 1)) + + # Shift all the parameters down by one + shift +done