#!/bin/bash
# Script to troubleshoot and fix CNI network issues on joined node

echo "=== CNI Network Troubleshooting ==="

# Check CNI configuration
echo "1. Checking CNI configuration..."
ls -la /etc/kubernetes/cni/net.d/ 2>/dev/null || echo "CNI directory not found"
ls -la /etc/cni/net.d/ 2>/dev/null || echo "Alternative CNI directory not found"

# Check if ovs-configuration exists (for OVN-Kubernetes)
echo -e "\n2. Checking OVS configuration..."
sudo systemctl status ovs-configuration 2>/dev/null || echo "ovs-configuration not found"

# Check network operator pods on this node
echo -e "\n3. Checking network pods on this node..."
sudo crictl ps | grep -E "(ovn|ovs|multus|sdn)" || echo "No network pods found"

# Check machine-config-daemon status
echo -e "\n4. Checking machine-config-daemon..."
sudo systemctl status machine-config-daemon | head -n 10

# Force network operator to reconcile
echo -e "\n5. From master node, run these commands to force network reconciliation:"
cat << 'EOF'
# Check network operator status
oc get co network

# Check network pods on new node
oc get pods -n openshift-ovn-kubernetes -o wide | grep worker-lpar01
oc get pods -n openshift-multus -o wide | grep worker-lpar01
oc get pods -n openshift-sdn -o wide | grep worker-lpar01

# Force reconciliation by restarting network operator
oc delete pod -n openshift-network-operator -l name=network-operator

# Check machine config pool
oc get mcp worker

# If node shows as NotReady due to CNI
oc describe node worker-lpar01 | grep -A5 Conditions
EOF

echo -e "\n6. Temporary workaround - wait for network operator:"
echo "The CNI configuration should be automatically created within 2-5 minutes"
echo "Monitor with: watch -n 5 'ls -la /etc/kubernetes/cni/net.d/'"

# Check for br-ex creation (for OVN)
echo -e "\n7. Checking if br-ex needs to be created..."
if ! ip link show br-ex &>/dev/null; then
    echo "br-ex not found - network operator should create it"
fi

# Monitor logs
echo -e "\n8. Monitor relevant logs:"
echo "sudo journalctl -u crio -f"
echo "sudo journalctl -u kubelet -f | grep -i cni"