Files
k3s-gitops/apps/cluster-health-dashboard/setup.sh

269 lines
8.5 KiB
Bash

#!/bin/bash
# Cluster Health Dashboard - Quick Setup Script
# This script helps you set up the dashboard pipeline
set -e
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ Kubernetes Cluster Health Dashboard - Quick Setup ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if running in correct directory
if [ ! -f "Jenkinsfile" ]; then
echo -e "${RED}❌ Error: Jenkinsfile not found!${NC}"
echo "Please run this script from the cluster-health-dashboard directory"
exit 1
fi
echo -e "${BLUE}🔍 Checking prerequisites...${NC}"
echo ""
# Check kubectl
echo -n "Checking kubectl... "
if command -v kubectl &> /dev/null; then
KUBECTL_VERSION=$(kubectl version --client --short 2>/dev/null | cut -d" " -f3)
echo -e "${GREEN}✅ Found (${KUBECTL_VERSION})${NC}"
else
echo -e "${RED}❌ Not found${NC}"
echo "Please install kubectl first"
exit 1
fi
# Check jq
echo -n "Checking jq... "
if command -v jq &> /dev/null; then
JQ_VERSION=$(jq --version)
echo -e "${GREEN}✅ Found (${JQ_VERSION})${NC}"
else
echo -e "${YELLOW}⚠️ Not found${NC}"
echo "Installing jq..."
if [[ "$OSTYPE" == "darwin"* ]]; then
brew install jq
else
sudo apt-get update && sudo apt-get install -y jq
fi
fi
# Check curl
echo -n "Checking curl... "
if command -v curl &> /dev/null; then
echo -e "${GREEN}✅ Found${NC}"
else
echo -e "${RED}❌ Not found${NC}"
exit 1
fi
# Check cluster access
echo -n "Checking Kubernetes cluster access... "
if kubectl cluster-info &> /dev/null; then
CLUSTER_VERSION=$(kubectl version --short 2>/dev/null | grep Server | cut -d" " -f3)
echo -e "${GREEN}✅ Connected (${CLUSTER_VERSION})${NC}"
else
echo -e "${RED}❌ Cannot connect to cluster${NC}"
echo "Please configure kubectl first"
exit 1
fi
# Check Prometheus
echo ""
echo -e "${BLUE}🔍 Checking Prometheus...${NC}"
echo ""
echo -n "Looking for Prometheus service... "
if kubectl get svc -A | grep -q prometheus; then
PROM_NAMESPACE=$(kubectl get svc -A | grep prometheus-server | awk '{print $1}' | head -1)
PROM_SERVICE=$(kubectl get svc -A | grep prometheus-server | awk '{print $2}' | head -1)
PROM_URL="http://${PROM_SERVICE}.${PROM_NAMESPACE}.svc.cluster.local"
echo -e "${GREEN}✅ Found${NC}"
echo " Namespace: ${PROM_NAMESPACE}"
echo " Service: ${PROM_SERVICE}"
echo " URL: ${PROM_URL}"
# Test Prometheus access
echo -n " Testing Prometheus API... "
if kubectl run test-prom --rm -i --restart=Never --image=curlimages/curl:latest -- \
curl -s "${PROM_URL}/api/v1/query?query=up" | grep -q '"status":"success"' 2>/dev/null; then
echo -e "${GREEN}✅ Accessible${NC}"
else
echo -e "${YELLOW}⚠️ Cannot verify (might still work)${NC}"
fi
else
echo -e "${YELLOW}⚠️ Not found in cluster${NC}"
echo " Dashboard will work but without Prometheus metrics"
echo " You can install Prometheus later"
fi
# Get cluster info
echo ""
echo -e "${BLUE}📊 Your cluster overview:${NC}"
echo ""
NODE_COUNT=$(kubectl get nodes --no-headers | wc -l)
NAMESPACE_COUNT=$(kubectl get namespaces --no-headers | wc -l)
POD_COUNT=$(kubectl get pods --all-namespaces --no-headers | wc -l)
echo " Nodes: ${NODE_COUNT}"
echo " Namespaces: ${NAMESPACE_COUNT}"
echo " Total Pods: ${POD_COUNT}"
# Calculate total resources
TOTAL_CPU=0
TOTAL_MEMORY_GB=0
while IFS= read -r line; do
CPU=$(echo $line | jq -r '.status.capacity.cpu')
MEMORY_KI=$(echo $line | jq -r '.status.capacity.memory' | sed 's/Ki//')
TOTAL_CPU=$((TOTAL_CPU + CPU))
MEMORY_GB=$((MEMORY_KI / 1024 / 1024))
TOTAL_MEMORY_GB=$((TOTAL_MEMORY_GB + MEMORY_GB))
done < <(kubectl get nodes -o json | jq -c '.items[]')
echo " Total CPU: ${TOTAL_CPU} cores"
echo " Total Memory: ${TOTAL_MEMORY_GB} GB"
# Estimate costs
CPU_PRICE=0.04
MEMORY_PRICE=0.005
MONTHLY_CPU_COST=$(echo "${TOTAL_CPU} * 24 * 30 * ${CPU_PRICE}" | bc)
MONTHLY_MEMORY_COST=$(echo "${TOTAL_MEMORY_GB} * 24 * 30 * ${MEMORY_PRICE}" | bc)
TOTAL_COST=$(echo "${MONTHLY_CPU_COST} + ${MONTHLY_MEMORY_COST}" | bc)
echo ""
echo -e "${BLUE}💰 Estimated monthly costs (at default rates):${NC}"
echo " CPU: \$${MONTHLY_CPU_COST}"
echo " Memory: \$${MONTHLY_MEMORY_COST}"
echo " Total: \$${TOTAL_COST}"
echo ""
echo -e "${YELLOW} 💡 You can adjust pricing in Jenkinsfile${NC}"
# Telegram setup
echo ""
echo -e "${BLUE}📱 Telegram Setup${NC}"
echo ""
read -p "Do you want to set up Telegram notifications? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "To set up Telegram:"
echo "1. Open Telegram and find @BotFather"
echo "2. Send: /newbot"
echo "3. Follow instructions to get your bot token"
echo ""
echo "Then get your Chat ID:"
echo "1. Find @userinfobot in Telegram"
echo "2. Send: /start"
echo "3. Copy your ID"
echo ""
read -p "Press Enter when you have both token and chat ID..."
echo ""
read -p "Enter Bot Token: " BOT_TOKEN
read -p "Enter Chat ID: " CHAT_ID
# Test
echo -n "Testing Telegram connection... "
RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" \
-d text="🎉 Cluster Health Dashboard setup test!")
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo -e "${GREEN}✅ Success!${NC}"
echo ""
echo "Add these credentials to Jenkins:"
echo ""
echo "Credential 1:"
echo " Kind: Secret text"
echo " Secret: ${BOT_TOKEN}"
echo " ID: telegram-bot-token"
echo ""
echo "Credential 2:"
echo " Kind: Secret text"
echo " Secret: ${CHAT_ID}"
echo " ID: telegram-chat-id"
echo ""
else
echo -e "${RED}❌ Failed${NC}"
echo "Response: $RESPONSE"
fi
else
echo "Skipping Telegram setup"
fi
# Git setup
echo ""
echo -e "${BLUE}📦 Git Repository Setup${NC}"
echo ""
read -p "Add to GitOps repository? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter path to k3s-gitops repository: " GITOPS_PATH
if [ -d "$GITOPS_PATH" ]; then
TARGET_DIR="${GITOPS_PATH}/apps/cluster-health-dashboard"
mkdir -p "$TARGET_DIR"
cp Jenkinsfile "$TARGET_DIR/"
cp README.md "$TARGET_DIR/"
echo -e "${GREEN}✅ Files copied to ${TARGET_DIR}${NC}"
echo ""
echo "Next steps:"
echo " cd ${GITOPS_PATH}"
echo " git add apps/cluster-health-dashboard/"
echo " git commit -m 'feat: add cluster health dashboard pipeline'"
echo " git push origin main"
else
echo -e "${RED}❌ Directory not found: ${GITOPS_PATH}${NC}"
fi
fi
# Summary
echo ""
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ Setup Summary ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
echo -e "${GREEN}✅ Prerequisites verified${NC}"
echo -e "${GREEN}✅ Cluster access confirmed${NC}"
echo ""
echo -e "${BLUE}📋 Next Steps:${NC}"
echo ""
echo "1. Create Jenkins Pipeline:"
echo " - Jenkins → New Item → Pipeline"
echo " - Name: cluster-health-dashboard"
echo " - Pipeline script from SCM → Git"
echo " - Repository: your-gitea/k3s-gitops"
echo " - Script Path: apps/cluster-health-dashboard/Jenkinsfile"
echo ""
echo "2. Configure Credentials (if using Telegram):"
echo " - Add 'telegram-bot-token'"
echo " - Add 'telegram-chat-id'"
echo ""
echo "3. Run First Build:"
echo " - Build with Parameters"
echo " - REPORT_PERIOD: 24h"
echo " - SEND_TELEGRAM: true"
echo ""
echo "4. View Dashboard:"
echo " - Build #1 → Cluster Health Dashboard (sidebar)"
echo ""
echo -e "${BLUE}📚 Documentation:${NC}"
echo " See README.md for detailed setup and troubleshooting"
echo ""
echo -e "${GREEN}🎉 Setup complete! Good luck!${NC}"
echo ""