#!/bin/bash # Test replica-based build locking mechanism # This script demonstrates how the build system uses replicas for atomic locking set -e NAMESPACE="apps--droneio--prd" DEPLOYMENT="buildah-external" echo "๐Ÿงช Testing Replica-Based Build Locking" echo "======================================" # Function to get current replicas get_replicas() { kubectl get deployment $DEPLOYMENT -n $NAMESPACE -o jsonpath='{.spec.replicas}' } # Function to check if build can start can_start_build() { local replicas=$(get_replicas) if [ "$replicas" = "0" ]; then echo "โœ… Build can start (replicas=0)" return 0 else echo "โŒ Build already running (replicas=$replicas)" return 1 fi } # Function to start build (scale up) start_build() { echo "๐Ÿ”’ Acquiring build lock (scaling up)..." kubectl scale deployment $DEPLOYMENT --replicas=1 -n $NAMESPACE echo "โณ Waiting for pod to be ready..." kubectl wait --for=condition=ready pod -l app=buildah-external -n $NAMESPACE --timeout=120s echo "โœ… Build lock acquired!" } # Function to end build (scale down) end_build() { echo "๐Ÿ”ฝ Releasing build lock (scaling down)..." kubectl scale deployment $DEPLOYMENT --replicas=0 -n $NAMESPACE echo "โณ Waiting for pods to terminate..." kubectl wait --for=delete pod -l app=buildah-external -n $NAMESPACE --timeout=60s || echo "Pods may still be terminating" echo "โœ… Build lock released!" } # Test sequence echo "๐Ÿ“Š Current deployment status:" kubectl get deployment $DEPLOYMENT -n $NAMESPACE echo "" echo "๐Ÿ” Checking if build can start..." if can_start_build; then echo "" echo "๐Ÿš€ Starting test build..." start_build echo "" echo "๐Ÿ“Š Deployment during build:" kubectl get deployment $DEPLOYMENT -n $NAMESPACE kubectl get pods -l app=buildah-external -n $NAMESPACE echo "" echo "๐Ÿ” Testing concurrent build attempt..." if can_start_build; then echo "๐Ÿšจ ERROR: Concurrent build should be blocked!" exit 1 else echo "โœ… Concurrent build correctly blocked!" fi echo "" echo "๐Ÿ›‘ Ending test build..." end_build echo "" echo "๐Ÿ“Š Final deployment status:" kubectl get deployment $DEPLOYMENT -n $NAMESPACE echo "" echo "๐Ÿ” Verifying build can start again..." if can_start_build; then echo "โœ… Build system ready for next build!" else echo "๐Ÿšจ ERROR: Build system not properly reset!" exit 1 fi else echo "" echo "โš ๏ธ Cannot test - build already running" echo "Use: kubectl scale deployment $DEPLOYMENT --replicas=0 -n $NAMESPACE" fi echo "" echo "๐ŸŽ‰ Replica-based locking test completed successfully!" echo "" echo "๐Ÿ’ก Benefits:" echo " โœ… Atomic operations (no race conditions)" echo " โœ… No lock files to manage" echo " โœ… Kubernetes-native approach" echo " โœ… Resource efficient (only runs when needed)" echo " โœ… Automatic cleanup on failure"