#!/bin/bash set -e # Colors for output GREEN='\033[0;32m' BLUE='\033[0;34m' RED='\033[0;31m' NC='\033[0m' # No Color CLUSTER_NAME="easyhaproxy-test" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BIN_DIR="${SCRIPT_DIR}/.kind" KIND_BIN="${BIN_DIR}/kind" KUBECTL_BIN="${BIN_DIR}/kubectl" HELM_BIN="${BIN_DIR}/helm" PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" # Port configuration (matches test_kubernetes.py) HTTP_PORT=10080 HTTPS_PORT=10443 STATS_PORT=11936 echo -e "${BLUE}[1/9] Setting up kind cluster '${CLUSTER_NAME}'...${NC}" # Ensure kind is installed if [ ! -f "${KIND_BIN}" ]; then echo "Installing kind locally..." mkdir -p "${BIN_DIR}" curl -Lo "${KIND_BIN}" "https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64" chmod +x "${KIND_BIN}" echo -e "${GREEN}✓ kind installed to ${KIND_BIN}${NC}" fi # Ensure kubectl is installed if ! command -v kubectl &> /dev/null; then if [ ! -f "${KUBECTL_BIN}" ]; then echo "Installing kubectl locally..." mkdir -p "${BIN_DIR}" VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt) curl -Lo "${KUBECTL_BIN}" "https://dl.k8s.io/release/${VERSION}/bin/linux/amd64/kubectl" chmod +x "${KUBECTL_BIN}" echo -e "${GREEN}✓ kubectl installed to ${KUBECTL_BIN}${NC}" fi KUBECTL="${KUBECTL_BIN}" else KUBECTL="kubectl" fi # Ensure helm is installed if ! command -v helm &> /dev/null; then if [ ! -f "${HELM_BIN}" ]; then echo "Installing helm locally..." mkdir -p "${BIN_DIR}" HELM_VERSION="v3.13.3" HELM_TAR="${BIN_DIR}/helm.tar.gz" curl -Lo "${HELM_TAR}" "https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz" tar -xzf "${HELM_TAR}" -C "${BIN_DIR}" --strip-components=1 linux-amd64/helm rm "${HELM_TAR}" chmod +x "${HELM_BIN}" echo -e "${GREEN}✓ helm installed to ${HELM_BIN}${NC}" fi HELM="${HELM_BIN}" else HELM="helm" fi # Check if cluster already exists echo -e "${BLUE}[1/9] Checking for existing cluster...${NC}" if ${KIND_BIN} get clusters 2>/dev/null | grep -q "^${CLUSTER_NAME}$"; then echo -e "${BLUE}Cluster '${CLUSTER_NAME}' already exists, deleting it first...${NC}" ${KIND_BIN} delete cluster --name "${CLUSTER_NAME}" fi # Create cluster config echo -e "${BLUE}[1/9] Writing cluster config...${NC}" CLUSTER_CONFIG="${BIN_DIR}/cluster-config.yaml" mkdir -p "${BIN_DIR}" cat > "${CLUSTER_CONFIG}" < "${MANIFEST_PATH}" # Install EasyHAProxy echo -e "${BLUE}[7/9] Creating easyhaproxy namespace...${NC}" ${KUBECTL} create namespace easyhaproxy echo -e "${BLUE}[7/9] Applying EasyHAProxy manifest...${NC}" ${KUBECTL} apply -f "${MANIFEST_PATH}" # Label the control-plane node echo -e "${BLUE}[8/9] Labeling control-plane node...${NC}" ${KUBECTL} label nodes "${CLUSTER_NAME}-control-plane" \ "easyhaproxy/node=master" --overwrite # Wait for EasyHAProxy to be ready echo -e "${BLUE}[9/9] Waiting for EasyHAProxy pods to be ready...${NC}" if ${KUBECTL} wait --for=condition=Ready pods \ -n easyhaproxy -l "app.kubernetes.io/name=easyhaproxy" \ --timeout=30s 2>/dev/null; then echo -e "${GREEN}✓ EasyHAProxy pods are ready${NC}" else echo -e "${RED}✗ Pods not ready within 30s. Checking status...${NC}" ${KUBECTL} get pods -n easyhaproxy -o wide echo -e "\n${BLUE}Events:${NC}" ${KUBECTL} get events -n easyhaproxy --sort-by=.lastTimestamp exit 1 fi echo -e "${GREEN}✓ All setup complete! Cluster is ready.${NC}" echo "" echo -e "${BLUE}Cluster Information:${NC}" echo -e " Cluster name: ${CLUSTER_NAME}" echo -e " HTTP port: localhost:${HTTP_PORT}" echo -e " HTTPS port: localhost:${HTTPS_PORT}" echo -e " Stats port: localhost:${STATS_PORT}" echo "" echo -e "${BLUE}Useful commands:${NC}" echo -e " Apply example ingress: ${KUBECTL} apply -f ${SCRIPT_DIR}/service.yml" echo -e " Check EasyHAProxy logs: ${KUBECTL} logs -n easyhaproxy -l app.kubernetes.io/name=easyhaproxy -f" echo -e " Test with curl: curl -H 'Host: example.org' http://localhost:${HTTP_PORT}" echo -e " Delete cluster: ${SCRIPT_DIR}/teardown-cluster.sh" echo ""