- Functional control plan

This commit is contained in:
Josako
2025-08-18 11:44:23 +02:00
parent 066f579294
commit 84a9334c80
17 changed files with 3619 additions and 55 deletions

View File

@@ -0,0 +1,309 @@
#!/bin/bash
# Kubernetes Dependency Checking
# File: dependency-checks.sh
# Check if a service is ready
check_service_ready() {
local service=$1
local namespace=${2:-$K8S_NAMESPACE}
local timeout=${3:-60}
log_operation "INFO" "Checking if service '$service' is ready in namespace '$namespace'"
# Check if deployment exists
if ! kubectl get deployment "$service" -n "$namespace" &>/dev/null; then
log_dependency_check "$service" "NOT_FOUND" "Deployment does not exist"
return 1
fi
# Check if deployment is ready
local ready_replicas
ready_replicas=$(kubectl get deployment "$service" -n "$namespace" -o jsonpath='{.status.readyReplicas}' 2>/dev/null)
local desired_replicas
desired_replicas=$(kubectl get deployment "$service" -n "$namespace" -o jsonpath='{.spec.replicas}' 2>/dev/null)
if [[ -z "$ready_replicas" ]]; then
ready_replicas=0
fi
if [[ -z "$desired_replicas" ]]; then
desired_replicas=1
fi
if [[ "$ready_replicas" -eq "$desired_replicas" && "$ready_replicas" -gt 0 ]]; then
log_dependency_check "$service" "READY" "All $ready_replicas/$desired_replicas replicas are ready"
return 0
else
log_dependency_check "$service" "NOT_READY" "Only $ready_replicas/$desired_replicas replicas are ready"
return 1
fi
}
# Wait for a service to become ready
wait_for_service_ready() {
local service=$1
local namespace=${2:-$K8S_NAMESPACE}
local timeout=${3:-300}
local check_interval=${4:-10}
log_operation "INFO" "Waiting for service '$service' to become ready (timeout: ${timeout}s)"
local elapsed=0
while [[ $elapsed -lt $timeout ]]; do
if check_service_ready "$service" "$namespace" 0; then
log_operation "SUCCESS" "Service '$service' is ready after ${elapsed}s"
return 0
fi
log_operation "DEBUG" "Service '$service' not ready yet, waiting ${check_interval}s... (${elapsed}/${timeout}s)"
sleep "$check_interval"
elapsed=$((elapsed + check_interval))
done
log_operation "ERROR" "Service '$service' failed to become ready within ${timeout}s"
return 1
}
# Check if infrastructure services are ready
check_infrastructure_ready() {
log_operation "INFO" "Checking infrastructure readiness"
local infrastructure_services
infrastructure_services=$(get_services_in_group "infrastructure")
if [[ $? -ne 0 ]]; then
log_operation "ERROR" "Failed to get infrastructure services"
return 1
fi
local all_ready=true
for service in $infrastructure_services; do
if ! check_service_ready "$service" "$K8S_NAMESPACE" 0; then
all_ready=false
log_operation "WARNING" "Infrastructure service '$service' is not ready"
fi
done
if [[ "$all_ready" == "true" ]]; then
log_operation "SUCCESS" "All infrastructure services are ready"
return 0
else
log_operation "ERROR" "Some infrastructure services are not ready"
log_operation "INFO" "You may need to start infrastructure first: kup infrastructure"
return 1
fi
}
# Check app-specific dependencies
check_app_dependencies() {
local service=$1
log_operation "INFO" "Checking dependencies for service '$service'"
case "$service" in
"eveai-workers"|"eveai-chat-workers")
# Workers need API to be running
if ! check_service_ready "eveai-api" "$K8S_NAMESPACE" 0; then
log_operation "ERROR" "Service '$service' requires eveai-api to be running"
log_operation "INFO" "Start API first: kup-api"
return 1
fi
;;
"eveai-beat")
# Beat needs Redis to be running
if ! check_service_ready "redis" "$K8S_NAMESPACE" 0; then
log_operation "ERROR" "Service '$service' requires redis to be running"
log_operation "INFO" "Start infrastructure first: kup infrastructure"
return 1
fi
;;
"eveai-app"|"eveai-api"|"eveai-chat-client"|"eveai-entitlements")
# Core apps need infrastructure
if ! check_infrastructure_ready; then
log_operation "ERROR" "Service '$service' requires infrastructure to be running"
return 1
fi
;;
*)
log_operation "DEBUG" "No specific dependencies defined for service '$service'"
;;
esac
log_operation "SUCCESS" "All dependencies satisfied for service '$service'"
return 0
}
# Check if a pod is running and ready
check_pod_ready() {
local pod_selector=$1
local namespace=${2:-$K8S_NAMESPACE}
local pods
pods=$(kubectl get pods -l "$pod_selector" -n "$namespace" --no-headers 2>/dev/null)
if [[ -z "$pods" ]]; then
return 1
fi
# Check if any pod is in Running state and Ready
while IFS= read -r line; do
local status=$(echo "$line" | awk '{print $3}')
local ready=$(echo "$line" | awk '{print $2}')
if [[ "$status" == "Running" && "$ready" =~ ^[1-9]/[1-9] ]]; then
# Extract ready count and total count
local ready_count=$(echo "$ready" | cut -d'/' -f1)
local total_count=$(echo "$ready" | cut -d'/' -f2)
if [[ "$ready_count" -eq "$total_count" ]]; then
return 0
fi
fi
done <<< "$pods"
return 1
}
# Check service health endpoint
check_service_health() {
local service=$1
local namespace=${2:-$K8S_NAMESPACE}
local health_endpoint
health_endpoint=$(get_service_health_endpoint "$service")
if [[ -z "$health_endpoint" ]]; then
log_operation "DEBUG" "No health endpoint defined for service '$service'"
return 0
fi
case "$service" in
"redis")
# Check Redis with ping
if kubectl exec -n "$namespace" deployment/redis -- redis-cli ping &>/dev/null; then
log_operation "SUCCESS" "Redis health check passed"
return 0
else
log_operation "WARNING" "Redis health check failed"
return 1
fi
;;
"minio")
# Check MinIO readiness
if kubectl exec -n "$namespace" deployment/minio -- mc ready local &>/dev/null; then
log_operation "SUCCESS" "MinIO health check passed"
return 0
else
log_operation "WARNING" "MinIO health check failed"
return 1
fi
;;
*)
# For other services, try HTTP health check
if [[ "$health_endpoint" =~ ^/.*:[0-9]+$ ]]; then
local path=$(echo "$health_endpoint" | cut -d':' -f1)
local port=$(echo "$health_endpoint" | cut -d':' -f2)
# Use port-forward to check health endpoint
local pod
pod=$(kubectl get pods -l "app=$service" -n "$namespace" --no-headers -o custom-columns=":metadata.name" | head -n1)
if [[ -n "$pod" ]]; then
if timeout 10 kubectl exec -n "$namespace" "$pod" -- curl -f -s "http://localhost:$port$path" &>/dev/null; then
log_operation "SUCCESS" "Health check passed for service '$service'"
return 0
else
log_operation "WARNING" "Health check failed for service '$service'"
return 1
fi
fi
fi
;;
esac
log_operation "DEBUG" "Could not perform health check for service '$service'"
return 0
}
# Comprehensive dependency check for a service group
check_group_dependencies() {
local group=$1
log_operation "INFO" "Checking dependencies for service group '$group'"
local services
services=$(get_services_in_group "$group")
if [[ $? -ne 0 ]]; then
return 1
fi
# Sort services by deployment order
local sorted_services
read -ra service_array <<< "$services"
sorted_services=$(sort_services_by_deploy_order "${service_array[@]}")
local all_dependencies_met=true
for service in $sorted_services; do
local dependencies
dependencies=$(get_service_dependencies "$service")
for dep in $dependencies; do
if ! check_service_ready "$dep" "$K8S_NAMESPACE" 0; then
log_operation "ERROR" "Dependency '$dep' not ready for service '$service'"
all_dependencies_met=false
fi
done
# Check app-specific dependencies
if ! check_app_dependencies "$service"; then
all_dependencies_met=false
fi
done
if [[ "$all_dependencies_met" == "true" ]]; then
log_operation "SUCCESS" "All dependencies satisfied for group '$group'"
return 0
else
log_operation "ERROR" "Some dependencies not satisfied for group '$group'"
return 1
fi
}
# Show dependency status for all services
show_dependency_status() {
echo "🔍 Dependency Status Overview:"
echo "=============================="
local all_services
all_services=$(get_services_in_group "all")
for service in $all_services; do
local status="❌ NOT READY"
local health_status=""
if check_service_ready "$service" "$K8S_NAMESPACE" 0; then
status="✅ READY"
# Check health if available
if check_service_health "$service" "$K8S_NAMESPACE"; then
health_status=" (healthy)"
else
health_status=" (unhealthy)"
fi
fi
echo " $service: $status$health_status"
done
}
# Export functions for use in other scripts
if [[ -n "$ZSH_VERSION" ]]; then
typeset -f check_service_ready wait_for_service_ready check_infrastructure_ready > /dev/null
typeset -f check_app_dependencies check_pod_ready check_service_health > /dev/null
typeset -f check_group_dependencies show_dependency_status > /dev/null
else
export -f check_service_ready wait_for_service_ready check_infrastructure_ready
export -f check_app_dependencies check_pod_ready check_service_health
export -f check_group_dependencies show_dependency_status
fi

View File

@@ -0,0 +1,417 @@
#!/bin/bash
# Kubernetes Core Functions
# File: k8s-functions.sh
# Deploy a service group
deploy_service_group() {
local group=$1
log_operation "INFO" "Deploying service group: $group"
if [[ -z "$K8S_CONFIG_DIR" ]]; then
log_operation "ERROR" "K8S_CONFIG_DIR not set"
return 1
fi
# Get YAML files for the group
local yaml_files
yaml_files=$(get_yaml_files_for_group "$group")
if [[ $? -ne 0 ]]; then
log_operation "ERROR" "Failed to get YAML files for group: $group"
return 1
fi
# Check dependencies first
if ! check_group_dependencies "$group"; then
log_operation "WARNING" "Some dependencies not satisfied, but proceeding with deployment"
fi
# Deploy each YAML file
local success=true
for yaml_file in $yaml_files; do
local full_path="$K8S_CONFIG_DIR/$yaml_file"
if [[ ! -f "$full_path" ]]; then
log_operation "ERROR" "YAML file not found: $full_path"
success=false
continue
fi
log_operation "INFO" "Applying YAML file: $yaml_file"
log_kubectl_command "kubectl apply -f $full_path"
if kubectl apply -f "$full_path"; then
log_operation "SUCCESS" "Successfully applied: $yaml_file"
else
log_operation "ERROR" "Failed to apply: $yaml_file"
success=false
fi
done
if [[ "$success" == "true" ]]; then
log_operation "SUCCESS" "Service group '$group' deployed successfully"
# Wait for services to be ready
wait_for_group_ready "$group"
return 0
else
log_operation "ERROR" "Failed to deploy service group '$group'"
return 1
fi
}
# Stop a service group
stop_service_group() {
local group=$1
local mode=${2:-"--keep-data"} # --keep-data, --stop-only, --delete-all
log_operation "INFO" "Stopping service group: $group (mode: $mode)"
local services
services=$(get_services_in_group "$group")
if [[ $? -ne 0 ]]; then
return 1
fi
# Sort services in reverse deployment order for graceful shutdown
local service_array
read -ra service_array <<< "$services"
local sorted_services
sorted_services=$(sort_services_by_deploy_order "${service_array[@]}")
# Reverse the order
local reversed_services=()
local service_list=($sorted_services)
for ((i=${#service_list[@]}-1; i>=0; i--)); do
reversed_services+=("${service_list[i]}")
done
local success=true
for service in "${reversed_services[@]}"; do
if ! stop_individual_service "$service" "$mode"; then
success=false
fi
done
if [[ "$success" == "true" ]]; then
log_operation "SUCCESS" "Service group '$group' stopped successfully"
return 0
else
log_operation "ERROR" "Failed to stop some services in group '$group'"
return 1
fi
}
# Start a service group (for stopped services)
start_service_group() {
local group=$1
log_operation "INFO" "Starting service group: $group"
local services
services=$(get_services_in_group "$group")
if [[ $? -ne 0 ]]; then
return 1
fi
# Sort services by deployment order
local service_array
read -ra service_array <<< "$services"
local sorted_services
sorted_services=$(sort_services_by_deploy_order "${service_array[@]}")
local success=true
for service in $sorted_services; do
if ! start_individual_service "$service"; then
success=false
fi
done
if [[ "$success" == "true" ]]; then
log_operation "SUCCESS" "Service group '$group' started successfully"
return 0
else
log_operation "ERROR" "Failed to start some services in group '$group'"
return 1
fi
}
# Deploy an individual service
deploy_individual_service() {
local service=$1
local group=${2:-""}
log_operation "INFO" "Deploying individual service: $service"
# Get YAML file for the service
local yaml_file
yaml_file=$(get_yaml_file_for_service "$service")
if [[ $? -ne 0 ]]; then
return 1
fi
local full_path="$K8S_CONFIG_DIR/$yaml_file"
if [[ ! -f "$full_path" ]]; then
log_operation "ERROR" "YAML file not found: $full_path"
return 1
fi
# Check dependencies
if ! check_app_dependencies "$service"; then
log_operation "WARNING" "Dependencies not satisfied, but proceeding with deployment"
fi
log_operation "INFO" "Applying YAML file: $yaml_file for service: $service"
log_kubectl_command "kubectl apply -f $full_path"
if kubectl apply -f "$full_path"; then
log_operation "SUCCESS" "Successfully deployed service: $service"
# Wait for service to be ready
wait_for_service_ready "$service" "$K8S_NAMESPACE" 180
return 0
else
log_operation "ERROR" "Failed to deploy service: $service"
return 1
fi
}
# Stop an individual service
stop_individual_service() {
local service=$1
local mode=${2:-"--keep-data"}
log_operation "INFO" "Stopping individual service: $service (mode: $mode)"
case "$mode" in
"--keep-data")
# Scale deployment to 0 but keep everything else
log_kubectl_command "kubectl scale deployment $service --replicas=0 -n $K8S_NAMESPACE"
if kubectl scale deployment "$service" --replicas=0 -n "$K8S_NAMESPACE" 2>/dev/null; then
log_operation "SUCCESS" "Scaled down service: $service"
else
log_operation "WARNING" "Failed to scale down service: $service (may not exist)"
fi
;;
"--stop-only")
# Same as keep-data for Kubernetes
log_kubectl_command "kubectl scale deployment $service --replicas=0 -n $K8S_NAMESPACE"
if kubectl scale deployment "$service" --replicas=0 -n "$K8S_NAMESPACE" 2>/dev/null; then
log_operation "SUCCESS" "Stopped service: $service"
else
log_operation "WARNING" "Failed to stop service: $service (may not exist)"
fi
;;
"--delete-all")
# Delete the deployment and associated resources
log_kubectl_command "kubectl delete deployment $service -n $K8S_NAMESPACE"
if kubectl delete deployment "$service" -n "$K8S_NAMESPACE" 2>/dev/null; then
log_operation "SUCCESS" "Deleted deployment: $service"
else
log_operation "WARNING" "Failed to delete deployment: $service (may not exist)"
fi
# Also delete service if it exists
log_kubectl_command "kubectl delete service ${service}-service -n $K8S_NAMESPACE"
kubectl delete service "${service}-service" -n "$K8S_NAMESPACE" 2>/dev/null || true
;;
*)
log_operation "ERROR" "Unknown stop mode: $mode"
return 1
;;
esac
return 0
}
# Start an individual service (restore replicas)
start_individual_service() {
local service=$1
log_operation "INFO" "Starting individual service: $service"
# Check if deployment exists
if ! kubectl get deployment "$service" -n "$K8S_NAMESPACE" &>/dev/null; then
log_operation "ERROR" "Deployment '$service' does not exist. Use deploy function instead."
return 1
fi
# Get the original replica count (assuming 1 if not specified)
local desired_replicas=1
# For services that typically have multiple replicas
case "$service" in
"eveai-workers"|"eveai-chat-workers")
desired_replicas=2
;;
esac
log_kubectl_command "kubectl scale deployment $service --replicas=$desired_replicas -n $K8S_NAMESPACE"
if kubectl scale deployment "$service" --replicas="$desired_replicas" -n "$K8S_NAMESPACE"; then
log_operation "SUCCESS" "Started service: $service with $desired_replicas replicas"
# Wait for service to be ready
wait_for_service_ready "$service" "$K8S_NAMESPACE" 180
return 0
else
log_operation "ERROR" "Failed to start service: $service"
return 1
fi
}
# Wait for a service group to be ready
wait_for_group_ready() {
local group=$1
local timeout=${2:-300}
log_operation "INFO" "Waiting for service group '$group' to be ready"
local services
services=$(get_services_in_group "$group")
if [[ $? -ne 0 ]]; then
return 1
fi
local all_ready=true
for service in $services; do
if ! wait_for_service_ready "$service" "$K8S_NAMESPACE" "$timeout"; then
all_ready=false
log_operation "WARNING" "Service '$service' in group '$group' failed to become ready"
fi
done
if [[ "$all_ready" == "true" ]]; then
log_operation "SUCCESS" "All services in group '$group' are ready"
return 0
else
log_operation "ERROR" "Some services in group '$group' failed to become ready"
return 1
fi
}
# Get service status
get_service_status() {
local service=$1
local namespace=${2:-$K8S_NAMESPACE}
if ! kubectl get deployment "$service" -n "$namespace" &>/dev/null; then
echo "NOT_DEPLOYED"
return 1
fi
local ready_replicas
ready_replicas=$(kubectl get deployment "$service" -n "$namespace" -o jsonpath='{.status.readyReplicas}' 2>/dev/null)
local desired_replicas
desired_replicas=$(kubectl get deployment "$service" -n "$namespace" -o jsonpath='{.spec.replicas}' 2>/dev/null)
if [[ -z "$ready_replicas" ]]; then
ready_replicas=0
fi
if [[ -z "$desired_replicas" ]]; then
desired_replicas=0
fi
if [[ "$desired_replicas" -eq 0 ]]; then
echo "STOPPED"
elif [[ "$ready_replicas" -eq "$desired_replicas" && "$ready_replicas" -gt 0 ]]; then
echo "RUNNING"
elif [[ "$ready_replicas" -gt 0 ]]; then
echo "PARTIAL"
else
echo "STARTING"
fi
}
# Show detailed service status
show_service_status() {
local service=${1:-""}
if [[ -n "$service" ]]; then
# Show status for specific service
echo "🔍 Status for service: $service"
echo "================================"
local status
status=$(get_service_status "$service")
echo "Status: $status"
if kubectl get deployment "$service" -n "$K8S_NAMESPACE" &>/dev/null; then
echo ""
echo "Deployment details:"
kubectl get deployment "$service" -n "$K8S_NAMESPACE"
echo ""
echo "Pod details:"
kubectl get pods -l "app=$service" -n "$K8S_NAMESPACE"
echo ""
echo "Recent events:"
kubectl get events --field-selector involvedObject.name="$service" -n "$K8S_NAMESPACE" --sort-by='.lastTimestamp' | tail -5
else
echo "Deployment not found"
fi
else
# Show status for all services
echo "🔍 Service Status Overview:"
echo "=========================="
local all_services
all_services=$(get_services_in_group "all")
for svc in $all_services; do
local status
status=$(get_service_status "$svc")
local status_icon
case "$status" in
"RUNNING") status_icon="✅" ;;
"PARTIAL") status_icon="⚠️" ;;
"STARTING") status_icon="🔄" ;;
"STOPPED") status_icon="⏹️" ;;
"NOT_DEPLOYED") status_icon="❌" ;;
*) status_icon="❓" ;;
esac
echo " $svc: $status_icon $status"
done
fi
}
# Restart a service (stop and start)
restart_service() {
local service=$1
log_operation "INFO" "Restarting service: $service"
if ! stop_individual_service "$service" "--stop-only"; then
log_operation "ERROR" "Failed to stop service: $service"
return 1
fi
sleep 5
if ! start_individual_service "$service"; then
log_operation "ERROR" "Failed to start service: $service"
return 1
fi
log_operation "SUCCESS" "Successfully restarted service: $service"
}
# Export functions for use in other scripts
if [[ -n "$ZSH_VERSION" ]]; then
typeset -f deploy_service_group stop_service_group start_service_group > /dev/null
typeset -f deploy_individual_service stop_individual_service start_individual_service > /dev/null
typeset -f wait_for_group_ready get_service_status show_service_status restart_service > /dev/null
else
export -f deploy_service_group stop_service_group start_service_group
export -f deploy_individual_service stop_individual_service start_individual_service
export -f wait_for_group_ready get_service_status show_service_status restart_service
fi

View File

@@ -0,0 +1,222 @@
#!/bin/bash
# Kubernetes Logging Utilities
# File: logging-utils.sh
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function for colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_debug() {
echo -e "${PURPLE}[DEBUG]${NC} $1"
}
print_operation() {
echo -e "${CYAN}[OPERATION]${NC} $1"
}
# Main logging function
log_operation() {
local level=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Ensure log directory exists
if [[ -n "$K8S_LOG_DIR" ]]; then
mkdir -p "$K8S_LOG_DIR"
# Log to main operations file
echo "$timestamp [$level] $message" >> "$K8S_LOG_DIR/k8s-operations.log"
# Log errors to separate error file
if [[ "$level" == "ERROR" ]]; then
echo "$timestamp [ERROR] $message" >> "$K8S_LOG_DIR/service-errors.log"
print_error "$message"
elif [[ "$level" == "WARNING" ]]; then
print_warning "$message"
elif [[ "$level" == "SUCCESS" ]]; then
print_success "$message"
elif [[ "$level" == "DEBUG" ]]; then
print_debug "$message"
elif [[ "$level" == "OPERATION" ]]; then
print_operation "$message"
else
print_status "$message"
fi
else
# Fallback if no log directory is set
case $level in
"ERROR")
print_error "$message"
;;
"WARNING")
print_warning "$message"
;;
"SUCCESS")
print_success "$message"
;;
"DEBUG")
print_debug "$message"
;;
"OPERATION")
print_operation "$message"
;;
*)
print_status "$message"
;;
esac
fi
}
# Log kubectl command execution
log_kubectl_command() {
local command="$1"
local result="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
if [[ -n "$K8S_LOG_DIR" ]]; then
echo "$timestamp [KUBECTL] $command" >> "$K8S_LOG_DIR/kubectl-commands.log"
if [[ -n "$result" ]]; then
echo "$timestamp [KUBECTL_RESULT] $result" >> "$K8S_LOG_DIR/kubectl-commands.log"
fi
fi
}
# Log dependency check results
log_dependency_check() {
local service="$1"
local status="$2"
local details="$3"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
if [[ -n "$K8S_LOG_DIR" ]]; then
echo "$timestamp [DEPENDENCY] Service: $service, Status: $status, Details: $details" >> "$K8S_LOG_DIR/dependency-checks.log"
fi
if [[ "$status" == "READY" ]]; then
log_operation "SUCCESS" "Dependency check passed for $service"
elif [[ "$status" == "NOT_READY" ]]; then
log_operation "WARNING" "Dependency check failed for $service: $details"
else
log_operation "ERROR" "Dependency check error for $service: $details"
fi
}
# Show recent logs
show_recent_logs() {
local log_type=${1:-operations}
local lines=${2:-20}
if [[ -z "$K8S_LOG_DIR" ]]; then
echo "No log directory configured"
return 1
fi
case $log_type in
"operations"|"ops")
if [[ -f "$K8S_LOG_DIR/k8s-operations.log" ]]; then
echo "Recent operations (last $lines lines):"
tail -n "$lines" "$K8S_LOG_DIR/k8s-operations.log"
else
echo "No operations log found"
fi
;;
"errors"|"err")
if [[ -f "$K8S_LOG_DIR/service-errors.log" ]]; then
echo "Recent errors (last $lines lines):"
tail -n "$lines" "$K8S_LOG_DIR/service-errors.log"
else
echo "No error log found"
fi
;;
"kubectl"|"cmd")
if [[ -f "$K8S_LOG_DIR/kubectl-commands.log" ]]; then
echo "Recent kubectl commands (last $lines lines):"
tail -n "$lines" "$K8S_LOG_DIR/kubectl-commands.log"
else
echo "No kubectl command log found"
fi
;;
"dependencies"|"deps")
if [[ -f "$K8S_LOG_DIR/dependency-checks.log" ]]; then
echo "Recent dependency checks (last $lines lines):"
tail -n "$lines" "$K8S_LOG_DIR/dependency-checks.log"
else
echo "No dependency check log found"
fi
;;
*)
echo "Available log types: operations, errors, kubectl, dependencies"
return 1
;;
esac
}
# Clear logs
clear_logs() {
local log_type=${1:-all}
if [[ -z "$K8S_LOG_DIR" ]]; then
echo "No log directory configured"
return 1
fi
case $log_type in
"all")
rm -f "$K8S_LOG_DIR"/*.log
log_operation "INFO" "All logs cleared"
;;
"operations"|"ops")
rm -f "$K8S_LOG_DIR/k8s-operations.log"
echo "Operations log cleared"
;;
"errors"|"err")
rm -f "$K8S_LOG_DIR/service-errors.log"
echo "Error log cleared"
;;
"kubectl"|"cmd")
rm -f "$K8S_LOG_DIR/kubectl-commands.log"
echo "Kubectl command log cleared"
;;
"dependencies"|"deps")
rm -f "$K8S_LOG_DIR/dependency-checks.log"
echo "Dependency check log cleared"
;;
*)
echo "Available log types: all, operations, errors, kubectl, dependencies"
return 1
;;
esac
}
# Export functions for use in other scripts
if [[ -n "$ZSH_VERSION" ]]; then
typeset -f log_operation log_kubectl_command log_dependency_check > /dev/null
typeset -f show_recent_logs clear_logs > /dev/null
typeset -f print_status print_success print_warning print_error print_debug print_operation > /dev/null
else
export -f log_operation log_kubectl_command log_dependency_check
export -f show_recent_logs clear_logs
export -f print_status print_success print_warning print_error print_debug print_operation
fi

View File

@@ -0,0 +1,253 @@
#!/bin/bash
# Kubernetes Service Group Definitions
# File: service-groups.sh
# Service group definitions
declare -A SERVICE_GROUPS
# Infrastructure services (Redis, MinIO)
SERVICE_GROUPS[infrastructure]="redis minio"
# Application services (all EveAI apps)
SERVICE_GROUPS[apps]="eveai-app eveai-api eveai-chat-client eveai-workers eveai-chat-workers eveai-beat eveai-entitlements"
# Static files and ingress
SERVICE_GROUPS[static]="static-files eveai-ingress"
# Monitoring services
SERVICE_GROUPS[monitoring]="prometheus grafana flower"
# All services combined
SERVICE_GROUPS[all]="redis minio eveai-app eveai-api eveai-chat-client eveai-workers eveai-chat-workers eveai-beat eveai-entitlements static-files eveai-ingress prometheus grafana flower"
# Service to YAML file mapping
declare -A SERVICE_YAML_FILES
# Infrastructure services
SERVICE_YAML_FILES[redis]="redis-minio-services.yaml"
SERVICE_YAML_FILES[minio]="redis-minio-services.yaml"
# Application services
SERVICE_YAML_FILES[eveai-app]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-api]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-chat-client]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-workers]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-chat-workers]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-beat]="eveai-services.yaml"
SERVICE_YAML_FILES[eveai-entitlements]="eveai-services.yaml"
# Static and ingress services
SERVICE_YAML_FILES[static-files]="static-files-service.yaml"
SERVICE_YAML_FILES[eveai-ingress]="eveai-ingress.yaml"
# Monitoring services
SERVICE_YAML_FILES[prometheus]="monitoring-services.yaml"
SERVICE_YAML_FILES[grafana]="monitoring-services.yaml"
SERVICE_YAML_FILES[flower]="monitoring-services.yaml"
# Service deployment order (for dependencies)
declare -A SERVICE_DEPLOY_ORDER
# Infrastructure first (order 1)
SERVICE_DEPLOY_ORDER[redis]=1
SERVICE_DEPLOY_ORDER[minio]=1
# Core apps next (order 2)
SERVICE_DEPLOY_ORDER[eveai-app]=2
SERVICE_DEPLOY_ORDER[eveai-api]=2
SERVICE_DEPLOY_ORDER[eveai-chat-client]=2
SERVICE_DEPLOY_ORDER[eveai-entitlements]=2
# Workers after core apps (order 3)
SERVICE_DEPLOY_ORDER[eveai-workers]=3
SERVICE_DEPLOY_ORDER[eveai-chat-workers]=3
SERVICE_DEPLOY_ORDER[eveai-beat]=3
# Static files and ingress (order 4)
SERVICE_DEPLOY_ORDER[static-files]=4
SERVICE_DEPLOY_ORDER[eveai-ingress]=4
# Monitoring last (order 5)
SERVICE_DEPLOY_ORDER[prometheus]=5
SERVICE_DEPLOY_ORDER[grafana]=5
SERVICE_DEPLOY_ORDER[flower]=5
# Service health check endpoints
declare -A SERVICE_HEALTH_ENDPOINTS
SERVICE_HEALTH_ENDPOINTS[eveai-app]="/healthz/ready:5001"
SERVICE_HEALTH_ENDPOINTS[eveai-api]="/healthz/ready:5003"
SERVICE_HEALTH_ENDPOINTS[eveai-chat-client]="/healthz/ready:5004"
SERVICE_HEALTH_ENDPOINTS[redis]="ping"
SERVICE_HEALTH_ENDPOINTS[minio]="ready"
# Get services in a group
get_services_in_group() {
local group=$1
if [[ -n "${SERVICE_GROUPS[$group]}" ]]; then
echo "${SERVICE_GROUPS[$group]}"
else
log_operation "ERROR" "Unknown service group: $group"
local available_groups=("${!SERVICE_GROUPS[@]}")
echo "Available groups: ${available_groups[*]}"
return 1
fi
}
# Get YAML file for a service
get_yaml_file_for_service() {
local service=$1
if [[ -n "${SERVICE_YAML_FILES[$service]}" ]]; then
echo "${SERVICE_YAML_FILES[$service]}"
else
log_operation "ERROR" "No YAML file defined for service: $service"
return 1
fi
}
# Get deployment order for a service
get_service_deploy_order() {
local service=$1
echo "${SERVICE_DEPLOY_ORDER[$service]:-999}"
}
# Get health check endpoint for a service
get_service_health_endpoint() {
local service=$1
echo "${SERVICE_HEALTH_ENDPOINTS[$service]:-}"
}
# Sort services by deployment order
sort_services_by_deploy_order() {
local services=("$@")
local sorted_services=()
# Create array of service:order pairs
local service_orders=()
for service in "${services[@]}"; do
local order=$(get_service_deploy_order "$service")
service_orders+=("$order:$service")
done
# Sort by order and extract service names
IFS=$'\n' sorted_services=($(printf '%s\n' "${service_orders[@]}" | sort -n | cut -d: -f2))
echo "${sorted_services[@]}"
}
# Get services that should be deployed before a given service
get_service_dependencies() {
local target_service=$1
local target_order=$(get_service_deploy_order "$target_service")
local dependencies=()
# Find all services with lower deployment order
for service in "${!SERVICE_DEPLOY_ORDER[@]}"; do
local service_order="${SERVICE_DEPLOY_ORDER[$service]}"
if [[ "$service_order" -lt "$target_order" ]]; then
dependencies+=("$service")
fi
done
echo "${dependencies[@]}"
}
# Check if a service belongs to a group
is_service_in_group() {
local service=$1
local group=$2
local group_services="${SERVICE_GROUPS[$group]}"
if [[ " $group_services " =~ " $service " ]]; then
return 0
else
return 1
fi
}
# Get all unique YAML files for a group
get_yaml_files_for_group() {
local group=$1
local services
services=$(get_services_in_group "$group")
if [[ $? -ne 0 ]]; then
return 1
fi
local yaml_files=()
local unique_files=()
for service in $services; do
local yaml_file=$(get_yaml_file_for_service "$service")
if [[ -n "$yaml_file" ]]; then
yaml_files+=("$yaml_file")
fi
done
# Remove duplicates
IFS=$'\n' unique_files=($(printf '%s\n' "${yaml_files[@]}" | sort -u))
echo "${unique_files[@]}"
}
# Display service group information
show_service_groups() {
echo "📋 Available Service Groups:"
echo "============================"
for group in "${!SERVICE_GROUPS[@]}"; do
echo ""
echo "🔹 $group:"
local services="${SERVICE_GROUPS[$group]}"
for service in $services; do
local order=$(get_service_deploy_order "$service")
local yaml_file=$(get_yaml_file_for_service "$service")
echo "$service (order: $order, file: $yaml_file)"
done
done
}
# Validate service group configuration
validate_service_groups() {
local errors=0
echo "🔍 Validating service group configuration..."
# Check if all services have YAML files defined
for group in "${!SERVICE_GROUPS[@]}"; do
local services="${SERVICE_GROUPS[$group]}"
for service in $services; do
if [[ -z "${SERVICE_YAML_FILES[$service]}" ]]; then
log_operation "ERROR" "Service '$service' in group '$group' has no YAML file defined"
((errors++))
fi
done
done
# Check if YAML files exist
if [[ -n "$K8S_CONFIG_DIR" ]]; then
for yaml_file in "${SERVICE_YAML_FILES[@]}"; do
if [[ ! -f "$K8S_CONFIG_DIR/$yaml_file" ]]; then
log_operation "WARNING" "YAML file '$yaml_file' not found in $K8S_CONFIG_DIR"
fi
done
fi
if [[ $errors -eq 0 ]]; then
log_operation "SUCCESS" "Service group configuration is valid"
return 0
else
log_operation "ERROR" "Found $errors configuration errors"
return 1
fi
}
# Export functions for use in other scripts
if [[ -n "$ZSH_VERSION" ]]; then
typeset -f get_services_in_group get_yaml_file_for_service get_service_deploy_order > /dev/null
typeset -f get_service_health_endpoint sort_services_by_deploy_order get_service_dependencies > /dev/null
typeset -f is_service_in_group get_yaml_files_for_group show_service_groups validate_service_groups > /dev/null
else
export -f get_services_in_group get_yaml_file_for_service get_service_deploy_order
export -f get_service_health_endpoint sort_services_by_deploy_order get_service_dependencies
export -f is_service_in_group get_yaml_files_for_group show_service_groups validate_service_groups
fi