Skip to content

Commit

Permalink
Apply k8s-default-storage-class as sonobuoy plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Toni Finger <toni.finger@cloudandheat.com>
  • Loading branch information
tonifinger committed Oct 17, 2023
1 parent 586d291 commit 7628927
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.idea
.DS_Store
node_modules
Tests/kaas/results/
*.tar.gz
21 changes: 21 additions & 0 deletions Tests/kaas/k8s-default-storage-class/Dockerfile_sonobuoy_plugin
Original file line number Diff line number Diff line change
@@ -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"]
35 changes: 35 additions & 0 deletions Tests/kaas/k8s-default-storage-class/helper.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -225,13 +226,16 @@ 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):

initialize_logging()
error_count = 0
return_code = 0
return_message = "return_message: FAILED"

try:
opts, args = getopt.gnu_getopt(argv, "k:h", ["kubeconfig=", "help"])
Expand All @@ -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)
Expand All @@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions Tests/kaas/k8s-default-storage-class/run_checks.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7628927

Please sign in to comment.