Commit 5f761f4e authored by Sergio Gimenez's avatar Sergio Gimenez
Browse files

[WIP] Deployment locally seems to work

parent cd5066e2
Loading
Loading
Loading
Loading
+351 −0
Changes for automation/2-kind-cluster-with-i2edge/deploy-i2edge-fixed.sh: 351 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/bash

# Script to deploy i2edge on a kind cluster
# This script handles all the necessary setup including storage configuration

set -e  # Exit on error

echo "======================================"
echo "i2edge Deployment Script"
echo "======================================"

# Configuration variables
CLUSTER_NAME="operator-platform"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
I2EDGE_PATH="/home/sergio/i2cat/OperatorPlatform/i2edge"
I2EDGE_IMAGE="i2edge:develop"
I2EDGE_NAMESPACE="i2edge"
KUBECONFIG_PATH="$SCRIPT_DIR/../1-kind-cluster/operator-platform-external-kubeconfig.yaml"

# Export KUBECONFIG
if [ -f "$KUBECONFIG_PATH" ]; then
    export KUBECONFIG="$KUBECONFIG_PATH"
    echo "✓ Using kubeconfig: $KUBECONFIG_PATH"
else
    echo "Error: Kubeconfig not found at $KUBECONFIG_PATH"
    echo "Please ensure the kind cluster is created first"
    exit 1
fi

# =============================================
# Step 0: Verify Prerequisites
# =============================================
echo ""
echo "======================================"
echo "Step 0: Verifying prerequisites"
echo "======================================"

# Check if cluster is accessible
if ! kubectl cluster-info &> /dev/null; then
    echo "Error: Cannot access Kubernetes cluster"
    echo "Tip: Ensure your kind cluster is running"
    echo "Run: cd ../1-kind-cluster && ./deploy.sh"
    exit 1
fi

echo "✓ Cluster is accessible"
echo "Current context: $(kubectl config current-context)"

# Verify i2edge path exists
if [ ! -d "$I2EDGE_PATH" ]; then
    echo "Error: i2edge directory not found at $I2EDGE_PATH"
    echo "Please ensure the i2edge repository is at the expected location"
    exit 1
fi

echo "✓ i2edge directory found at $I2EDGE_PATH"

# Check if dependencies are installed
if ! kubectl get namespace monitoring &> /dev/null; then
    echo ""
    echo "⚠️  Warning: monitoring namespace not found"
    echo "It appears dependencies (Prometheus, NFD) are not installed"
    echo "Please run: ./install-dependencies.sh first"
    read -p "Do you want to continue anyway? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
else
    echo "✓ Dependencies appear to be installed"
fi

# =============================================
# Step 1: Build i2edge Docker Image
# =============================================
echo ""
echo "======================================"
echo "Step 1: Building i2edge Docker image"
echo "======================================"

cd "$I2EDGE_PATH"

if docker images | grep -q "i2edge.*develop"; then
    echo "i2edge:develop image already exists"
    read -p "Do you want to rebuild the image? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "Rebuilding Docker image..."
        docker build -t "$I2EDGE_IMAGE" .
    else
        echo "Skipping image build"
    fi
else
    echo "Building Docker image for the first time..."
    docker build -t "$I2EDGE_IMAGE" .
fi

if docker images | grep -q "i2edge.*develop"; then
    echo "✓ Docker image is available"
    docker images | grep i2edge
else
    echo "Error: Failed to build or find Docker image"
    exit 1
fi

# =============================================
# Step 2: Load Image into Kind Cluster
# =============================================
echo ""
echo "======================================"
echo "Step 2: Loading image into kind cluster"
echo "======================================"

echo "Loading $I2EDGE_IMAGE into kind cluster $CLUSTER_NAME..."
echo "This may take a moment..."

if kind load docker-image "$I2EDGE_IMAGE" --name "$CLUSTER_NAME"; then
    echo "✓ Image loaded into kind cluster successfully"
else
    echo "Error: Failed to load image into kind cluster"
    exit 1
fi

# =============================================
# Step 3: Create Required Directories on Nodes
# =============================================
echo ""
echo "======================================"
echo "Step 3: Creating storage directories"
echo "======================================"

echo "Creating required directories on kind nodes for hostPath volumes..."

# Create directories on all nodes
for node in operator-platform-control-plane operator-platform-worker operator-platform-worker2; do
    echo "Creating directories on $node..."
    docker exec "$node" mkdir -p /opt/i2edge/{artefactfile,charts,conffile,deployedartefacts,smdbdata}
    if [ $? -eq 0 ]; then
        echo "  ✓ Directories created on $node"
    else
        echo "  ⚠️  Warning: Failed to create directories on $node"
    fi
done

echo "✓ Storage directories prepared"

# =============================================
# Step 4: Fix Storage Configuration (if needed)
# =============================================
echo ""
echo "======================================"
echo "Step 4: Verifying storage configuration"
echo "======================================"

# Check if PV files need storageClassName fix
PV_FILES=(
    "deploy/i2edge/i2edge-artefacts-pv.yaml"
    "deploy/i2edge/i2edge-configfiles-pv.yaml"
    "deploy/i2edge/i2edge-deployedartefacts-pv.yaml"
    "deploy/chartmuseum/chartmuseum-charts-pv.yaml"
    "deploy/mongodb/mongodb-smdbdata-pv.yaml"
)

NEEDS_FIX=false
for pv_file in "${PV_FILES[@]}"; do
    if ! grep -q 'storageClassName: ""' "$pv_file"; then
        NEEDS_FIX=true
        break
    fi
done

if [ "$NEEDS_FIX" = true ]; then
    echo "⚠️  PV files need storageClassName configuration"
    echo "Applying storage class fix..."
    
    for pv_file in "${PV_FILES[@]}"; do
        if ! grep -q 'storageClassName: ""' "$pv_file"; then
            # Add storageClassName after persistentVolumeReclaimPolicy
            sed -i '/persistentVolumeReclaimPolicy: Retain/a\  storageClassName: ""' "$pv_file"
            echo "  ✓ Fixed $pv_file"
        fi
    done
    
    # Fix PVC files
    PVC_FILES=(
        "deploy/i2edge/i2edge-artefacts-pvc.yaml"
        "deploy/i2edge/i2edge-configfiles-pvc.yaml"
        "deploy/i2edge/i2edge-deployedartefacts-pvc.yaml"
        "deploy/chartmuseum/chartmuseum-charts-pvc.yaml"
        "deploy/mongodb/mongodb-smdbdata-pvc.yaml"
    )
    
    for pvc_file in "${PVC_FILES[@]}"; do
        if ! grep -q 'storageClassName: ""' "$pvc_file"; then
            # Add storageClassName after accessModes
            sed -i '/accessModes:/a\  storageClassName: ""' "$pvc_file"
            echo "  ✓ Fixed $pvc_file"
        fi
    done
    
    echo "✓ Storage configuration fixed"
else
    echo "✓ Storage configuration is correct"
fi

# =============================================
# Step 5: Deploy i2edge using Kustomize
# =============================================
echo ""
echo "======================================"
echo "Step 5: Deploying i2edge components"
echo "======================================"

cd "$I2EDGE_PATH/deploy"

# Check if namespace already exists
if kubectl get namespace "$I2EDGE_NAMESPACE" &> /dev/null; then
    echo "⚠️  Namespace $I2EDGE_NAMESPACE already exists"
    read -p "Do you want to delete and redeploy? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "Deleting existing i2edge deployment..."
        kubectl delete -k . --ignore-not-found=true
        echo "Waiting for resources to be deleted..."
        sleep 5
    else
        echo "Skipping deployment"
        exit 0
    fi
fi

# Apply kustomization
echo "Applying kustomization..."
kubectl apply -k .

if [ $? -eq 0 ]; then
    echo "✓ i2edge components deployed successfully"
else
    echo "Error: Failed to deploy i2edge components"
    exit 1
fi

# =============================================
# Step 6: Wait for Deployments to be Ready
# =============================================
echo ""
echo "======================================"
echo "Step 6: Waiting for deployments"
echo "======================================"

echo "Waiting for pods to be created..."
sleep 5

echo "Waiting for MongoDB deployment..."
kubectl wait --for=condition=available --timeout=180s deployment/mongodb -n "$I2EDGE_NAMESPACE" || echo "⚠️  MongoDB timeout (may still be starting)"

echo "Waiting for ChartMuseum deployment..."
kubectl wait --for=condition=available --timeout=180s deployment/chartmuseum -n "$I2EDGE_NAMESPACE" || echo "⚠️  ChartMuseum timeout (may still be starting)"

echo "Waiting for i2edge deployment..."
kubectl wait --for=condition=available --timeout=180s deployment/i2edge -n "$I2EDGE_NAMESPACE" || echo "⚠️  i2edge timeout (may still be starting)"

# =============================================
# Step 7: Display Deployment Status
# =============================================
echo ""
echo "======================================"
echo "Deployment Status"
echo "======================================"

echo ""
echo "Pods in $I2EDGE_NAMESPACE namespace:"
kubectl get pods -n "$I2EDGE_NAMESPACE"

echo ""
echo "Services in $I2EDGE_NAMESPACE namespace:"
kubectl get services -n "$I2EDGE_NAMESPACE"

echo ""
echo "Persistent Volume Claims:"
kubectl get pvc -n "$I2EDGE_NAMESPACE"

# =============================================
# Step 8: Check for Issues
# =============================================
echo ""
echo "Checking for pod issues..."

FAILED_PODS=$(kubectl get pods -n "$I2EDGE_NAMESPACE" --field-selector=status.phase!=Running --no-headers 2>/dev/null | wc -l)

if [ "$FAILED_PODS" -gt 0 ]; then
    echo ""
    echo "⚠️  Some pods are not running. Checking details..."
    kubectl get pods -n "$I2EDGE_NAMESPACE" --field-selector=status.phase!=Running
    
    # Check for ImagePullBackOff
    if kubectl get pods -n "$I2EDGE_NAMESPACE" | grep -q "ImagePullBackOff\|ErrImagePull"; then
        echo ""
        echo "Found ImagePullBackOff error. Attempting to fix..."
        echo "Reloading image and restarting pods..."
        kind load docker-image "$I2EDGE_IMAGE" --name "$CLUSTER_NAME"
        kubectl delete pod -n "$I2EDGE_NAMESPACE" -l io.kompose.service=i2edge
        echo "Waiting for pod to restart..."
        sleep 10
    fi
fi

# =============================================
# Step 9: Display Access Information
# =============================================
echo ""
echo "======================================"
echo "Access Information"
echo "======================================"

# Get node port for i2edge service
NODE_PORT=$(kubectl get svc -n "$I2EDGE_NAMESPACE" i2edge -o jsonpath='{.spec.ports[0].nodePort}' 2>/dev/null)

if [ -n "$NODE_PORT" ]; then
    echo "✓ i2edge API is exposed via NodePort: $NODE_PORT"
    echo ""
    echo "Access the API at:"
    echo "  http://localhost:$NODE_PORT"
    echo ""
    echo "Swagger UI (API Documentation):"
    echo "  http://localhost:$NODE_PORT/docs"
    echo ""
    echo "Test the API:"
    echo "  curl http://localhost:$NODE_PORT"
else
    echo "⚠️  Could not retrieve NodePort information"
fi

echo ""
echo "======================================"
echo "✓ i2edge deployment completed!"
echo "======================================"
echo ""
echo "Useful commands:"
echo "  # Check pod status:"
echo "  kubectl get pods -n $I2EDGE_NAMESPACE"
echo ""
echo "  # View i2edge logs:"
echo "  kubectl logs -n $I2EDGE_NAMESPACE deployment/i2edge -f"
echo ""
echo "  # Access i2edge shell:"
echo "  kubectl exec -it -n $I2EDGE_NAMESPACE deployment/i2edge -- /bin/bash"
echo ""
echo "  # Delete deployment:"
echo "  kubectl delete -k $I2EDGE_PATH/deploy"
echo ""
+142 −0
Changes for automation/2-kind-cluster-with-i2edge/undeploy-i2edge-fixed.sh: 142 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/bash

# Script to undeploy i2edge from the kind cluster
# This script removes all i2edge resources

set -e  # Exit on error

echo "======================================"
echo "i2edge Undeploy Script"
echo "======================================"

# Configuration variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
I2EDGE_PATH="/home/sergio/i2cat/OperatorPlatform/i2edge"
I2EDGE_NAMESPACE="i2edge"
KUBECONFIG_PATH="$SCRIPT_DIR/../1-kind-cluster/operator-platform-external-kubeconfig.yaml"

# Export KUBECONFIG
if [ -f "$KUBECONFIG_PATH" ]; then
    export KUBECONFIG="$KUBECONFIG_PATH"
    echo "✓ Using kubeconfig: $KUBECONFIG_PATH"
else
    echo "Error: Kubeconfig not found at $KUBECONFIG_PATH"
    exit 1
fi

# Check if cluster is accessible
if ! kubectl cluster-info &> /dev/null; then
    echo "Error: Cannot access Kubernetes cluster"
    exit 1
fi

echo "✓ Cluster is accessible"
echo "Current context: $(kubectl config current-context)"

# Check if i2edge namespace exists
if ! kubectl get namespace "$I2EDGE_NAMESPACE" &> /dev/null; then
    echo "ℹ️  Namespace $I2EDGE_NAMESPACE does not exist"
    echo "i2edge is not deployed"
    exit 0
fi

echo ""
echo "⚠️  WARNING: This will delete all i2edge resources including:"
echo "  - Deployments (i2edge, mongodb, chartmuseum)"
echo "  - Services"
echo "  - Persistent Volume Claims"
echo "  - Persistent Volumes"
echo "  - ConfigMaps and Secrets"
echo "  - Namespace: $I2EDGE_NAMESPACE"
echo ""
read -p "Are you sure you want to continue? (yes/no) " -r
echo

if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
    echo "Undeploy cancelled"
    exit 0
fi

echo ""
echo "======================================"
echo "Deleting i2edge resources"
echo "======================================"

cd "$I2EDGE_PATH/deploy"

# Delete using kustomize
echo "Deleting i2edge deployment..."
kubectl delete -k . --timeout=60s

if [ $? -eq 0 ]; then
    echo "✓ i2edge resources deleted successfully"
else
    echo "⚠️  Some resources may not have been deleted"
fi

echo ""
echo "======================================"
echo "Verifying cleanup"
echo "======================================"

# Check if namespace is gone
if kubectl get namespace "$I2EDGE_NAMESPACE" &> /dev/null; then
    echo "⚠️  Namespace $I2EDGE_NAMESPACE still exists"
    echo "It may take a moment to fully terminate"
    
    echo "Waiting for namespace to be deleted (timeout: 60s)..."
    kubectl wait --for=delete namespace/"$I2EDGE_NAMESPACE" --timeout=60s 2>/dev/null || echo "⚠️  Namespace deletion timeout"
else
    echo "✓ Namespace deleted"
fi

# Check for remaining PVs
echo ""
echo "Checking for remaining Persistent Volumes..."
REMAINING_PVS=$(kubectl get pv 2>/dev/null | grep -E "artefactfile|charts|conffile|deployedartefacts|smdbdata" | wc -l)

if [ "$REMAINING_PVS" -gt 0 ]; then
    echo "⚠️  Found $REMAINING_PVS remaining Persistent Volumes:"
    kubectl get pv | grep -E "artefactfile|charts|conffile|deployedartefacts|smdbdata"
    echo ""
    echo "These PVs may need manual deletion:"
    echo "  kubectl delete pv artefactfile charts conffile deployedartefacts smdbdata"
else
    echo "✓ All Persistent Volumes cleaned up"
fi

echo ""
echo "======================================"
echo "Optional: Cleanup storage directories"
echo "======================================"

echo ""
echo "Storage directories on kind nodes:"
echo "  /opt/i2edge/artefactfile"
echo "  /opt/i2edge/charts"
echo "  /opt/i2edge/conffile"
echo "  /opt/i2edge/deployedartefacts"
echo "  /opt/i2edge/smdbdata"
echo ""
read -p "Do you want to remove these directories from the nodes? (y/n) " -n 1 -r
echo

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "Removing storage directories from nodes..."
    for node in operator-platform-control-plane operator-platform-worker operator-platform-worker2; do
        docker exec "$node" rm -rf /opt/i2edge 2>/dev/null && echo "  ✓ Cleaned $node" || echo "  ⚠️  Could not clean $node"
    done
    echo "✓ Storage directories removed"
else
    echo "Storage directories preserved"
fi

echo ""
echo "======================================"
echo "✓ i2edge undeploy completed!"
echo "======================================"
echo ""
echo "Note: The i2edge Docker image is still available locally"
echo "To remove it, run:"
echo "  docker rmi i2edge:develop"
echo ""