From 2847b2f29c6b891d2dbb84d24ad0aaffbe68f992 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Wed, 9 Oct 2024 16:43:07 +0300 Subject: [PATCH] fix: don't create Kind cluster twice (#5914) * fix: don't create Kind cluster twice Signed-off-by: Vladislav Sukhin * fix: remove tini warning Signed-off-by: Vladislav Sukhin --------- Signed-off-by: Vladislav Sukhin --- build/kind/entrypoint.sh | 187 +++++++++++++++++++------------------ build/kind/kind.Dockerfile | 4 +- 2 files changed, 100 insertions(+), 91 deletions(-) diff --git a/build/kind/entrypoint.sh b/build/kind/entrypoint.sh index a2f5248c02..de2ee74779 100644 --- a/build/kind/entrypoint.sh +++ b/build/kind/entrypoint.sh @@ -195,111 +195,118 @@ for file in /images/*.tar; do docker load <$file done -# Step 4: Create Kind cluster using a specific Kubernetes version -log "Creating Kubernetes cluster using Kind (Kubernetes v1.31.0)..." -kind create cluster --name testkube-cluster --image kindest/node:v1.31.0 --wait 5m -if [ $? -ne 0 ]; then - log "Failed to create Kind cluster." - send_telenetry "docker_installation_failed" "kind_error" "Kind cluster was not created" - exit 1 -fi - -# Step 5: Verify kubectl is connected to the cluster -log "Verifying cluster is up..." -kubectl cluster-info -if [ $? -ne 0 ]; then - log "Failed to verify cluster." - send_telenetry "docker_installation_failed" "kind_error" "Kind cluster is nor accessible" - exit 1 -fi - -# Step 6: Add the Testkube Helm repository -log "Adding Testkube Helm repository..." -helm repo add testkube https://kubeshop.github.io/helm-charts -helm repo update - -# Step 7: Install Testkube using Helm -log "Installing Testkube via Helm..." -helm install testkube testkube/testkube --namespace testkube --create-namespace --set testkube-api.cloud.key=$AGENT_KEY --set testkube-api.minio.enabled=false --set mongodb.enabled=false --set testkube-dashboard.enabled=false --set testkube-api.cloud.url=$CLOUD_URL --set testkube-api.containerEnv=docker -if [ $? -ne 0 ]; then - log "Testkube installation failed." - send_telenetry "docker_installation_failed" "helm_error" "Testkube installation failed" - exit 1 -fi +# Get the list of kind clusters +EXISTING_CLUSTERS=$(kind get clusters) +# Check if the testkube-cluster exists in the list +if echo "$EXISTING_CLUSTERS" | grep -wq "testkube-cluster"; then + log "Kind cluster already exists" +else + # Step 4: Create Kind cluster using a specific Kubernetes version + log "Creating Kubernetes cluster using Kind (Kubernetes v1.31.0)..." + kind create cluster --name testkube-cluster --image kindest/node:v1.31.0 --wait 5m + if [ $? -ne 0 ]; then + log "Failed to create Kind cluster." + send_telenetry "docker_installation_failed" "kind_error" "Kind cluster was not created" + exit 1 + fi -# Step 8: Verify Testkube is installed to the cluster -log "Verifying Testkube is up..." -counter=0 -log_pattern="starting Testkube API server" -while [ $counter -lt 15 ] -do - # Get all pod statuses in the Testkube namespace - pod_status=$(kubectl get pods -n testkube --no-headers) - - # Check if there are any pods in the Testkube namespace - if [ -z "$pod_status" ]; then - log "No pods found in testkube namespace." - send_telenetry "docker_installation_failed" "tetkube_error" "No pods found in testkube namespace" + # Step 5: Verify kubectl is connected to the cluster + log "Verifying cluster is up..." + kubectl cluster-info + if [ $? -ne 0 ]; then + log "Failed to verify cluster." + send_telenetry "docker_installation_failed" "kind_error" "Kind cluster is nor accessible" exit 1 fi - # Iterate through each pod, check status and log pattern - all_running=true - found_pattern=false + # Step 6: Add the Testkube Helm repository + log "Adding Testkube Helm repository..." + helm repo add testkube https://kubeshop.github.io/helm-charts + helm repo update + + # Step 7: Install Testkube using Helm + log "Installing Testkube via Helm..." + helm install testkube testkube/testkube --namespace testkube --create-namespace --set testkube-api.cloud.key=$AGENT_KEY --set testkube-api.minio.enabled=false --set mongodb.enabled=false --set testkube-dashboard.enabled=false --set testkube-api.cloud.url=$CLOUD_URL --set testkube-api.containerEnv=docker + if [ $? -ne 0 ]; then + log "Testkube installation failed." + send_telenetry "docker_installation_failed" "helm_error" "Testkube installation failed" + exit 1 + fi - log "Checking pods in Testkube namespace..." - while read -r line; do - pod_name=$(echo "$line" | awk '{print $1}') - status=$(echo "$line" | awk '{print $3}') - - if [ "$status" != "Running" ]; then - log "Pod $pod_name is not running. Status: $status." - all_running=false - break - else - log "Pod $pod_name is running." + # Step 8: Verify Testkube is installed to the cluster + log "Verifying Testkube is up..." + counter=0 + log_pattern="starting Testkube API server" + while [ $counter -lt 15 ] + do + # Get all pod statuses in the Testkube namespace + pod_status=$(kubectl get pods -n testkube --no-headers) + + # Check if there are any pods in the Testkube namespace + if [ -z "$pod_status" ]; then + log "No pods found in testkube namespace." + send_telenetry "docker_installation_failed" "tetkube_error" "No pods found in testkube namespace" + exit 1 fi - if [[ $pod_name == *"testkube-api-server"* ]]; then - pod_logs=$(kubectl logs "$pod_name" -n testkube) - - # Check if logs contain the desired pattern - if echo "$pod_logs" | grep -q "$log_pattern"; then - log "Log pattern found: $log_pattern." - found_pattern=true - else - log "Log pattern not found: $log_pattern." + # Iterate through each pod, check status and log pattern + all_running=true + found_pattern=false + + log "Checking pods in Testkube namespace..." + while read -r line; do + pod_name=$(echo "$line" | awk '{print $1}') + status=$(echo "$line" | awk '{print $3}') + + if [ "$status" != "Running" ]; then + log "Pod $pod_name is not running. Status: $status." + all_running=false break + else + log "Pod $pod_name is running." fi - fi - done <<< "$pod_status" - if [ "$all_running" = true ] && [ "$found_pattern" = true ] ; then - log "Waiting Testkube API for 30 seconds..." - sleep 30 - break - else - log "Waiting Testkube for 30 seconds..." - sleep 30 - fi + if [[ $pod_name == *"testkube-api-server"* ]]; then + pod_logs=$(kubectl logs "$pod_name" -n testkube) + + # Check if logs contain the desired pattern + if echo "$pod_logs" | grep -q "$log_pattern"; then + log "Log pattern found: $log_pattern." + found_pattern=true + else + log "Log pattern not found: $log_pattern." + break + fi + fi + done <<< "$pod_status" - counter=$(( counter + 1 )) -done + if [ "$all_running" = true ] && [ "$found_pattern" = true ] ; then + log "Waiting Testkube API for 30 seconds..." + sleep 30 + break + else + log "Waiting Testkube for 30 seconds..." + sleep 30 + fi -if [ $counter -eq 15 ]; then - log "Testkube validation failed." + counter=$(( counter + 1 )) + done + + if [ $counter -eq 15 ]; then + log "Testkube validation failed." send_telenetry "docker_installation_failed" "tetkube_error" "Testkube pods are not up and running" - exit 1 -fi -log "Testkube is up and running." + exit 1 + fi + log "Testkube is up and running." -# Step 9: Create Testkube k6 Test Workflow -log "Creating and running Testkube k6 Test Workflow..." -kubectl apply -f /examples/k6.yaml -n testkube + # Step 9: Create Testkube k6 Test Workflow + log "Creating and running Testkube k6 Test Workflow..." + kubectl apply -f /examples/k6.yaml -n testkube -log "Testkube installation successful!" -log "You can now use Testkube in your Kind Kubernetes cluster." -send_telenetry "docker_installation_succeed" + log "Testkube installation successful!" + log "You can now use Testkube in your Kind Kubernetes cluster." + send_telenetry "docker_installation_succeed" +fi # Step 10: Bring docker service back to foreground fg %1 diff --git a/build/kind/kind.Dockerfile b/build/kind/kind.Dockerfile index 1b60750c90..945e5b9a1c 100644 --- a/build/kind/kind.Dockerfile +++ b/build/kind/kind.Dockerfile @@ -2,7 +2,9 @@ # Step 1: Use a base image with Docker installed FROM docker:20.10.24-dind -# Step 2: Install necessary dependencies (curl, bash, tini) +ENV TINI_SUBREAPER=true + +# Step 2: Install necessary dependencies (curl, bash, tini, jq) RUN apk add --no-cache bash curl tini jq # Step 3: Install Kind (Kubernetes in Docker)