#!/bin/bash
# Script to import the fixed Crossplane provider image into containerd

set -e

IMAGE_TAR="/tmp/crossplane-provider-proxmox-fixed-v2.tar"
IMAGE_NAME="crossplane-provider-proxmox:fixed-v2"

echo "=========================================="
echo "Importing Crossplane Provider Image"
echo "=========================================="
echo ""

if [ ! -f "$IMAGE_TAR" ]; then
    echo "❌ Error: Image tar file not found: $IMAGE_TAR"
    echo "   Please ensure the image was built and saved."
    exit 1
fi

echo "✅ Image tar file found: $IMAGE_TAR"
echo ""

echo "Importing image to containerd (k8s.io namespace)..."
if sudo ctr -n k8s.io images import "$IMAGE_TAR"; then
    echo "✅ Image imported successfully"
else
    echo "❌ Failed to import image"
    exit 1
fi

echo ""
echo "Verifying image in containerd..."
sudo ctr -n k8s.io images ls | grep crossplane-provider-proxmox || echo "⚠️  Image not found with expected name"

echo ""
echo "=========================================="
echo "Next Steps:"
echo "=========================================="
echo "1. Update deployment:"
echo "   kubectl set image deployment/crossplane-provider-proxmox provider=$IMAGE_NAME -n crossplane-system"
echo ""
echo "2. Restart provider pod:"
echo "   kubectl delete pod -n crossplane-system -l app=crossplane-provider-proxmox"
echo ""
echo "3. Verify new code is running:"
echo "   kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=50"
echo ""

