diff --git a/.run/Delete Test Clusters.run.xml b/.run/Delete Test Clusters.run.xml index 888d88a2ee..ced70fe480 100644 --- a/.run/Delete Test Clusters.run.xml +++ b/.run/Delete Test Clusters.run.xml @@ -1,16 +1,16 @@ - diff --git a/.run/Deploy KLM from registry.run.xml b/.run/Deploy KLM from registry.run.xml index 18545b261c..e39e667d68 100644 --- a/.run/Deploy KLM from registry.run.xml +++ b/.run/Deploy KLM from registry.run.xml @@ -1,8 +1,8 @@ - diff --git a/.run/Deploy KLM from sources.run.xml b/.run/Deploy KLM from sources.run.xml index 52a0ed8ec8..1af9aa302e 100644 --- a/.run/Deploy KLM from sources.run.xml +++ b/.run/Deploy KLM from sources.run.xml @@ -1,8 +1,8 @@ - diff --git a/.run/Deploy kyma.run.xml b/.run/Deploy kyma.run.xml index 4679e2ece1..36d82b29cf 100644 --- a/.run/Deploy kyma.run.xml +++ b/.run/Deploy kyma.run.xml @@ -1,6 +1,6 @@ - diff --git a/.run/E2E Tests.run.xml b/.run/E2E Tests.run.xml index fc8ed9ce88..523c21cc7e 100644 --- a/.run/E2E Tests.run.xml +++ b/.run/E2E Tests.run.xml @@ -1,6 +1,6 @@ - diff --git a/.run/Install CRDs.run.xml b/.run/Install CRDs.run.xml index f9c8388393..b450e051aa 100644 --- a/.run/Install CRDs.run.xml +++ b/.run/Install CRDs.run.xml @@ -1,8 +1,8 @@ - diff --git a/.run/Un-Deploy kyma.run.xml b/.run/Un-Deploy kyma.run.xml index 11315f9df5..d0425b3133 100644 --- a/.run/Un-Deploy kyma.run.xml +++ b/.run/Un-Deploy kyma.run.xml @@ -1,8 +1,8 @@ - diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 553d17e4a4..a72042fadf 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,49 +4,32 @@ "version": "2.0.0", "tasks": [ { - "label": "Delete Test Clusters", + "label": "Create New Test Clusters", "type": "shell", - "command": "k3d cluster rm kcp skr", + "command": "${cwd}/scripts/tests/create_test_clusters.sh", + "dependsOn": ["Delete Test Clusters"] }, { - "label": "Ensure Test Clusters", + "label": "Delete Test Clusters", "type": "shell", - "command": "${cwd}/scripts/tests/create_test_clusters.sh", + "command": "${cwd}/scripts/tests/clusters_cleanup.sh" }, { - "label": "Create New Test Clusters", + "label": "Ensure Test Clusters", "type": "shell", - "command": "${cwd}/scripts/tests/create_test_clusters.sh", - "dependsOn": [ - "Delete Test Clusters" - ] + "command": "${cwd}/scripts/tests/create_test_clusters.sh" }, // re-run if encountering error that the watcher CRD is missing { "label": "Deploy KLM from sources", "type": "shell", - "command": "TAG=\"$(date +%Y%m%d%H%M%S)\" && make docker-build IMG=${LOCAL_IMG}:${TAG}; make docker-push IMG=${LOCAL_IMG}:${TAG}; make local-deploy-with-watcher IMG=${CLUSTER_IMG}:${TAG}", - "options": { - "env": { - "LOCAL_IMG": "localhost:5111/lifecycle-manager", - "CLUSTER_IMG": "k3d-kcp-registry.localhost:5000/lifecycle-manager", - "KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml" - - } - } + "command": "${cwd}/scripts/tests/deploy_klm_from_sources.sh" }, // re-run if encountering error that the watcher CRD is missing { "label": "Deploy KLM from registry", "type": "shell", - "command": "make local-deploy-with-watcher IMG=${IMG_REGISTRY_HOST}/${input:klmImageRegistry}/${IMG_NAME}:${input:klmImageTag}", - "options": { - "env": { - "IMG_REGISTRY_HOST": "europe-docker.pkg.dev/kyma-project", - "IMG_NAME": "lifecycle-manager", - "KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml", - } - } + "command": "${cwd}/scripts/tests/deploy_klm_from_registry.sh --image-registry ${input:klmImageRegistry} --image-tag ${input:klmImageTag}" }, { "label": "Deploy template-operator", @@ -61,46 +44,25 @@ { "label": "Deploy kyma", "type": "shell", - "command": "export SKR_HOST=${input:skrHost} && ${cwd}/scripts/tests/deploy_kyma.sh", - "options": { - "env": { - "KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml", - } - } + "command": "${cwd}/scripts/tests/deploy_kyma.sh ${input:skrHost}" }, { "label": "Un-Deploy kyma", "type": "shell", - "command": "kubectl -n kcp-system delete kyma kyma-sample; kubectl -n kcp-system delete secret kyma-sample", - "options": { - "env": { - "KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml", - } - } + "command": "${cwd}/scripts/tests/undeploy_kyma.sh" }, { "label": "E2E Tests", "type": "shell", - "command": "make -C ${cwd}/tests/e2e ${input:e2eTestTarget}", + "command": "${cwd}/scripts/tests/e2e.sh ${input:e2eTestTarget}", "group": { "kind": "test" - }, - "options": { - "env": { - "KCP_KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml", - "SKR_KUBECONFIG": "${env:HOME}/.k3d/skr-local.yaml" - } } }, { "label": "Install CRDs", "type": "shell", - "command": "make install", - "options": { - "env": { - "KUBECONFIG": "${env:HOME}/.k3d/kcp-local.yaml", - } - } + "command": "${cwd}/scripts/tests/install_crds.sh" } ], "inputs": [ @@ -132,7 +94,7 @@ "mandatory-module", "mandatory-module-metrics", "misconfigured-kyma-secret", - "ocm-compatible-module-template", + "ocm-compatible-module-template" ] }, { @@ -152,15 +114,12 @@ "id": "klmImageRegistry", "type": "pickString", "description": "Registry of KLM image", - "options": [ - "dev", - "prod" - ] + "options": ["dev", "prod"] }, { "id": "klmImageTag", "type": "promptString", - "description": "Tag of the KLM image, e.g. 'latest'", + "description": "Tag of the KLM image, e.g. 'latest'" }, { "id": "skrHost", @@ -172,6 +131,6 @@ // use localhost when running KLM locally on the machine "localhost" ] - }, + } ] } diff --git a/scripts/tests/README.md b/scripts/tests/README.md new file mode 100644 index 0000000000..50990df1dd --- /dev/null +++ b/scripts/tests/README.md @@ -0,0 +1,60 @@ +# Overview of All Scripts + +## Version Checker: `version.sh` +The script checks if the Command Line Tools (CLI) for `kubectl`, `docker`, `GoLang`, `k3d`, and `istioctl` have the correct versions. +It ensures that the versions are in a valid format using [Semantic Versioning](https://semver.org/). +If the check detects outdated versions, it gives a warning and exits with success. For more information, see all the possible exit statuses: + +* `0` - Success - the version is up-to-date or outdated and it uses the correct Semantic Versioning +* `1` - At least one of the CLI tools is not installed +* `2` - Invalid version found, for example, incorrect Semantic Versioning + +### Current Versions +| CLI Tool | Version | +| --------- | ------- | +| `kubectl` | v1.31.3 | +| `go` | v1.23.3 | +| `k3d` | v5.6.0 | +| `docker` | v27.3.1 | +| `istioctl`| v1.24.1 | + + +## Create Test Clusters: `create_test_cluster.sh` +The script creates two test clusters using `k3d`: +- `kcp` for the control plane +- `skr` for the Kyma runtime +If the clusters already exist, the script exits with success. +If you get a notification, while running the script, saying that the Watcher CR is missing, re-run the script. +The script is accompanied by the `Ensure Test Clusters` task in the VSCode tasks, and by the corresponding run configuration for GoLand. + +This script internally depends on `version.sh` to check the versions of the required tools. If you want to skip the version check, use the `--skip-version-check` flag. + +## Cleaning the Clusters: `clusters_cleanup.sh` +The script deletes the `kcp` and `skr` test clusters using `k3d`. + +## Deploying KLM from Sources: `deploy_klm_from_sources.sh` +The script deploys Lifecycle Manager using the current state of the locally cloned and developed repository. +It doesn't require any additional flags or parameters. + +## Deploying KLM from the Registry: `deploy_klm_from_registry.sh` +The script deploys Lifecycle Manager from the given image registry and the given image tag. +It requires the following parameters: +- `--image-registry`: The accepted values are `prod` and `dev`. +- `--image-tag`: The tag of the image to be used. For example, `latest`. + +## Deploy Kyma: `deploy_kyma.sh` +Use the script to deploy Kyma using one of the **required** parameters: +- `localhost`: To run Lifecycle Manager locally on your machine. +- `host.k3d.internal`: To deploy Lifecycle Manager to a cluster. + +## Undeploy Kyma: `undeploy_kyma.sh` +The script undeploys Kyma from the cluster by deleting the Kyma and the corresponding Secret from the `kcp-system` namespace. + +## End-To-End Tests: `e2e.sh` +The script runs end-to-end tests taking the test target as input. +The test targets are defined in the `tests` directory of the project root. +The script runs the test target and outputs the results to the console. +The errors that occurred during the test are handled directly by `make`. + +## Installing CRDs: `install_crds.sh` +The script installs Custom Resource Definitions (CRDs) required for Lifecycle Manager. The CRDs' set is the same as in the `make install` of the Makefile in the project root directory. diff --git a/scripts/tests/clusters_cleanup.sh b/scripts/tests/clusters_cleanup.sh new file mode 100755 index 0000000000..5788a9136f --- /dev/null +++ b/scripts/tests/clusters_cleanup.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# Remove the k3d cluster and the skr cluster +k3d cluster rm kcp skr + +echo "[$(basename $0)] Cleanup completed" diff --git a/scripts/tests/create_test_clusters.sh b/scripts/tests/create_test_clusters.sh index 7f4761356e..0b5a1d3ff6 100755 --- a/scripts/tests/create_test_clusters.sh +++ b/scripts/tests/create_test_clusters.sh @@ -1,11 +1,34 @@ #!/bin/bash +# Parse arguments +SKIP_VERSION_CHECK=false +if [ "$1" == "--skip-version-check" ]; then + SKIP_VERSION_CHECK=true + shift +fi + +# Check for invalid or extra arguments +if [ $# -ne 0 ]; then + echo "[$(basename $0)] Invalid argument(s): $@" + echo "Usage: $(basename $0) [--skip-version-check]" + exit 1 +fi + # Change to the directory where the script is located cd "$(dirname "$0")" +# Run version check unless skipped +if [ "$SKIP_VERSION_CHECK" = false ]; then + ./version.sh + if [ $? -ne 0 ]; then + echo "[$(basename $0)] Versioning check failed. Exiting..." + exit 1 + fi +fi + # create SKR cluster if k3d cluster list | grep -q "^skr\s"; then - echo "Reusing existing SKR cluster..." + echo "[$(basename $0)] Reusing existing SKR cluster..." else k3d cluster create skr \ -p 10080:80@loadbalancer \ @@ -19,7 +42,7 @@ fi # create KCP cluster if k3d cluster list | grep -q "^kcp\s"; then - echo "Reusing existing KCP cluster..." + echo "[$(basename $0)] Reusing existing KCP cluster..." else k3d cluster create kcp \ -p 9443:443@loadbalancer \ @@ -51,3 +74,6 @@ fi # export kubeconfigs k3d kubeconfig get skr > ~/.k3d/skr-local.yaml k3d kubeconfig get kcp > ~/.k3d/kcp-local.yaml +echo "[$(basename $0)] Kubeconfig for SKR and KCP exported successfully" + +echo "[$(basename $0)] Test clusters created successfully" diff --git a/scripts/tests/deploy_klm_from_registry.sh b/scripts/tests/deploy_klm_from_registry.sh index 2d709d559e..8ae26cbc44 100755 --- a/scripts/tests/deploy_klm_from_registry.sh +++ b/scripts/tests/deploy_klm_from_registry.sh @@ -1,10 +1,29 @@ #!/bin/bash -echo "Enter the KLM Image Registry (dev/prod):" -read KLM_IMAGE_REGISTRY +if [ "$#" -ne 4 ]; then + echo "Error: Exactly 2 arguments are required for both flags." + echo "Usage: $0 --image-registry [dev../prod..] --image-tag latest" + exit 1 +fi -echo "Enter the KLM Image Tag (e.g., latest):" -read KLM_IMAGE_TAG +# Changing directory to the root of the project with git +cd "$(git rev-parse --show-toplevel)" + +while [[ "$#" -gt 0 ]]; do + case $1 in + --image-registry) KLM_IMAGE_REGISTRY="$2"; shift ;; + --image-tag) KLM_IMAGE_TAG="$2"; shift ;; + *) + echo "Unknown parameter passed: $1"; + echo "Usage: $0 --image-registry [dev../prod..] --image-tag latest"; + exit 1 ;; + esac + shift +done export KUBECONFIG=${HOME}/.k3d/kcp-local.yaml +IMG_REGISTRY_HOST="europe-docker.pkg.dev/kyma-project" +IMG_NAME="lifecycle-manager" make local-deploy-with-watcher IMG=${IMG_REGISTRY_HOST}/${KLM_IMAGE_REGISTRY}/${IMG_NAME}:${KLM_IMAGE_TAG} + +echo "[$(basename $0)] KLM deployed successfully from registry" diff --git a/scripts/tests/deploy_klm_from_sources.sh b/scripts/tests/deploy_klm_from_sources.sh new file mode 100755 index 0000000000..c7a4d12307 --- /dev/null +++ b/scripts/tests/deploy_klm_from_sources.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Changing current directory to the root of the project +cd $(git rev-parse --show-toplevel) + +# Export necessary environment variables +export KUBECONFIG=${HOME}/.k3d/kcp-local.yaml +export LOCAL_IMG="localhost:5111/lifecycle-manager" +export CLUSTER_IMG="k3d-kcp-registry.localhost:5000/lifecycle-manager" +export TAG=$(date +%Y%m%d%H%M%S) + +make docker-build IMG=${LOCAL_IMG}:${TAG} +make docker-push IMG=${LOCAL_IMG}:${TAG} +make local-deploy-with-watcher IMG=${CLUSTER_IMG}:${TAG} diff --git a/scripts/tests/deploy_kyma.sh b/scripts/tests/deploy_kyma.sh index fe963686eb..29589393fc 100755 --- a/scripts/tests/deploy_kyma.sh +++ b/scripts/tests/deploy_kyma.sh @@ -1,5 +1,27 @@ #!/bin/bash +print_help() { + echo "Usage: $0 [localhost | host.k3d.internal]" + echo "localhost: Use this option when running KLM locally on the machine" + echo "host.k3d.internal: Use this option when deploying KLM onto a cluster" +} + +if [ "$#" -ne 1 ]; then + print_help + exit 1 +fi + +SKR_HOST=$1 +if [ "$SKR_HOST" != "localhost" ] && [ "$SKR_HOST" != "host.k3d.internal" ]; then + echo "Invalid option: $SKR_HOST" + print_help + exit 1 +fi + +# Exporting the path to the kubeconfig file +export KUBECONFIG=$HOME/.k3d/kcp-local.yaml +export SKR_HOST + cat < /dev/null; then + echo "kubectl is not installed. Please install kubectl." + versioning_error=true +fi + +if ! command -v go &> /dev/null; then + echo "Go is not installed. Please install Go." + versioning_error=true +fi + +if ! command -v k3d &> /dev/null; then + echo "k3d is not installed. Please install k3d." + versioning_error=true +fi + +if ! command -v docker &> /dev/null; then + echo "Docker is not installed. Please install Docker." + versioning_error=true +fi + +if ! command -v istioctl &> /dev/null; then + echo "istioctl is not installed. Please install istioctl." + versioning_error=true +fi + +if $versioning_error; then + exit 1 +fi + +# Versions installed on current system +KUBECTL_VERSION_INSTALLED=$(kubectl version --client | grep -E '[0-9]{1,}.[0-9]{1,}.[0-9]{1,}' | head -n1 | awk '{print $3}' | sed 's/v//') +GO_VERSION_INSTALLED=$(go version | awk '{print $3}' | sed 's/go//') +K3D_VERSION_INSTALLED=$(k3d --version | head -n1 | awk '{print $3}' | sed 's/v//') +DOCKER_VERSION_INSTALLED=$(docker --version | awk '{print $3}' | cut -d',' -f1) +ISTIOCTL_VERSION_INSTALLED=$(istioctl version --short --remote=false | awk '{print $3}' | sed 's/v//') + +# Function to compare two versions +# Returns: +# 0 if the versions are equal +# 1 if the first version is less than the second version +# 2 if the first version is greater than the second version +function version_comparator() { + if [[ "$1" == "$2" ]]; then + echo 0 + return + fi + + local first_version; first_version=$(echo -e "$1\n$2" | sort --version-sort | head -n1) + + if [[ "$first_version" == "$1" ]]; then + echo 1 + return + fi + echo 2 + return +} + +function print_warning() { + echo "[WARNING] Using a version of $1 that is older than the recommended version: $2" +} + +# Check for regex patterns with semver (Semantic Versioning) +if [[ ! $KUBECTL_VERSION_INSTALLED =~ $SEM_VER_REGEX ]]; then + echo "Invalid kubectl version: $KUBECTL_VERSION_INSTALLED" + exit 2 +fi + +if [[ ! $GO_VERSION_INSTALLED =~ $SEM_VER_REGEX ]]; then + echo "Invalid Go version: $GO_VERSION_INSTALLED" + exit 2 +fi + +if [[ ! $K3D_VERSION_INSTALLED =~ $SEM_VER_REGEX ]]; then + echo "Invalid k3d version: $K3D_VERSION_INSTALLED" + exit 2 +fi + +if [[ ! $DOCKER_VERSION_INSTALLED =~ $SEM_VER_REGEX ]]; then + echo "Invalid Docker version: $DOCKER_VERSION_INSTALLED" + exit 2 +fi + +if [[ ! $ISTIOCTL_VERSION_INSTALLED =~ $SEM_VER_REGEX ]]; then + echo "Invalid Docker version: $ISTIOCTL_VERSION_INSTALLED" + exit 2 +fi + +# Check if the installed versions are up to date +[[ $(version_comparator "$KUBECTL_VERSION_INSTALLED" "$KUBECTL_VERSION_DEFAULT") -eq 1 ]] \ + && print_warning "kubectl" "$KUBECTL_VERSION_DEFAULT" \ + || echo "kubectl version is up to date, using: v$KUBECTL_VERSION_INSTALLED" + +[[ $(version_comparator "$GO_VERSION_INSTALLED" "$GO_VERSION_DEFAULT") -eq 1 ]] \ + && print_warning "Go" "$GO_VERSION_DEFAULT" \ + || echo "GoLang version is up to date, using: go$GO_VERSION_INSTALLED" + +[[ $(version_comparator "$K3D_VERSION_INSTALLED" "$K3D_VERSION_DEFAULT") -eq 1 ]] \ + && print_warning "k3d" "$K3D_VERSION_DEFAULT" \ + || echo "k3d version is up to date, using: v$K3D_VERSION_INSTALLED" + +[[ $(version_comparator "$DOCKER_VERSION_INSTALLED" "$DOCKER_VERSION_DEFAULT") -eq 1 ]] \ + && print_warning "docker" "$DOCKER_VERSION_DEFAULT" \ + || echo "docker version is up to date, using: v$DOCKER_VERSION_INSTALLED" + +[[ $(version_comparator "$ISTIOCTL_VERSION_INSTALLED" "$ISTIOCTL_VERSION_DEFAULT") -eq 1 ]] \ + && print_warning "docker" "$ISTIOCTL_VERSION_DEFAULT" \ + || echo "istioctl version is up to date, using: v$ISTIOCTL_VERSION_INSTALLED" + +# Exit with success +exit 0