From 45a3e4635a5e985a734fcbbd4070a901fe4881a6 Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Thu, 5 Sep 2024 07:59:05 +0200 Subject: [PATCH] feat: validate CRDs on specific k8s versions Signed-off-by: Mattia Lavacca --- Makefile | 5 ++ ...y-crds-kind.sh => test-crds-validation.sh} | 58 ++++++++++++++++++- hack/verify-all.sh | 10 +++- 3 files changed, 69 insertions(+), 4 deletions(-) rename hack/{verify-crds-kind.sh => test-crds-validation.sh} (66%) diff --git a/Makefile b/Makefile index 39cd316631..8bcd4fbff2 100644 --- a/Makefile +++ b/Makefile @@ -83,6 +83,11 @@ test: cd "conformance/echo-basic" && go test -race -cover ./... cd "gwctl" && go test -race -cover ./... +# Run tests for CRDs validation +.PHONY: test.crds-validation +test.crds-validation: + ./hack/test-crds-validation.sh $(VERSION) + # Run conformance tests against controller implementation .PHONY: conformance conformance: diff --git a/hack/verify-crds-kind.sh b/hack/test-crds-validation.sh similarity index 66% rename from hack/verify-crds-kind.sh rename to hack/test-crds-validation.sh index 319224f1ac..d5d0eba6be 100755 --- a/hack/verify-crds-kind.sh +++ b/hack/test-crds-validation.sh @@ -41,15 +41,69 @@ cleanup() { trap cleanup INT TERM EXIT +# TODO(mlavacca): find a good way to keep this dependency up to date. +KIND_VERSION="v0.24.0" + +# list of kind images taken from https://github.com/kubernetes-sigs/kind/releases/tag/v0.24.0. +# they need to be updated when kind is updated. +KIND_IMAGES=( + "kindest/node:v1.27.17@sha256:3fd82731af34efe19cd54ea5c25e882985bafa2c9baefe14f8deab1737d9fabe" + "kindest/node:v1.28.13@sha256:45d319897776e11167e4698f6b14938eb4d52eb381d9e3d7a9086c16c69a8110" + "kindest/node:v1.29.8@sha256:d46b7aa29567e93b27f7531d258c372e829d7224b25e3fc6ffdefed12476d3aa" + "kindest/node:v1.30.4@sha256:976ea815844d5fa93be213437e3ff5754cd599b040946b5cca43ca45c2047" + "kindest/node:v1.31.0@sha256:53df588e04085fd41ae12de0c3fe4c72f7013bba32a20e7325357a1ac94ba865" +) + +# Cleanup logic for cleanup on exit +CLEANED_UP=false +cleanup() { + if [ "$CLEANED_UP" = "true" ]; then + return + fi + + if [ "${KIND_CREATE_ATTEMPTED:-}" = true ]; then + kind delete cluster --name "${CLUSTER_NAME}" || true + fi + CLEANED_UP=true +} + +trap cleanup INT TERM EXIT + +if [ "$#" -gt 1 ]; then + echo "Error: Too many arguments provided. Only 1 argument is allowed." + exit 1 +fi + +default_index=$((1)) + +if [ "$#" -eq 1 ]; then + # Check if the argument is a valid number + if ! [[ "$1" =~ ^-?[0-9]+$ ]]; then + echo "Error: Argument is not a valid integer." + exit 1 + fi + index=$(($1)) +else + index=$((default_index)) +fi + +# Check if the number is greater than 5 or smaller than 1 +if [ "$index" -gt 5 ] || [ "$index" -lt 1 ]; then + echo "Error: Argument must be between 1 and 5." + exit 1 +fi + +k8s_image=${KIND_IMAGES[$((index-1))]} + # For exit code res=0 # Install kind -(cd $GOPATH && go install sigs.k8s.io/kind@v0.20.0) || res=$? +(cd "${GOPATH}" && go install sigs.k8s.io/kind@${KIND_VERSION}) || res=$? # Create cluster KIND_CREATE_ATTEMPTED=true -kind create cluster --name "${CLUSTER_NAME}" +kind create cluster --name "${CLUSTER_NAME}" --image "${k8s_image}" || res=$? # Verify CEL validation for CHANNEL in experimental standard; do diff --git a/hack/verify-all.sh b/hack/verify-all.sh index 3ab0706fd7..19e192abb2 100755 --- a/hack/verify-all.sh +++ b/hack/verify-all.sh @@ -54,10 +54,16 @@ fi EXCLUDE="verify-all.sh" +# TODO(mlavacca): once the prow configuration will be updated with a new target for +# test-crds-validation.sh, we can remove it from the find command, otherwise it will be run twice. +SCRIPTS=$(find "${SCRIPT_ROOT}"/hack -name "verify-*.sh" -o -name "test-crds-validation.sh") + ret=0 -for t in `ls $SCRIPT_ROOT/hack/verify-*.sh` +for t in $SCRIPTS; do - if is-excluded $t ; then + echo $t + continue + if is-excluded "${t}" ; then echo "Skipping $t" continue fi