#!/bin/bash
# Diagnostic script for freshly installed node

echo "=== Fresh Node Diagnostics ==="

echo "1. Hostname and DNS check:"
hostname
hostname -f
cat /etc/hostname
echo "DNS servers:"
cat /etc/resolv.conf

echo -e "\n2. Network interfaces:"
ip addr show | grep -E "^[0-9]:|inet "

echo -e "\n3. Can we resolve cluster API?"
nslookup api-int.test01.kvant.cloud 2>&1 || echo "DNS resolution failed"
nslookup api.test01.kvant.cloud 2>&1 || echo "DNS resolution failed"

echo -e "\n4. Can we reach the API server?"
curl -k https://api-int.test01.kvant.cloud:6443/healthz 2>&1 | head -5 || echo "API unreachable"

echo -e "\n5. Checking ignition URL:"
curl -I http://10.153.11.108/boot/worker.ign 2>&1 | head -10 || echo "Ignition unreachable"

echo -e "\n6. Boot parameters used:"
cat /proc/cmdline

echo -e "\n7. Check if our parameters were applied:"
grep -o "copy_network=1" /proc/cmdline || echo "copy_network not found"
grep -o "peerdns=0" /proc/cmdline || echo "peerdns not found"
grep -o "OPENSHIFT_OVNKUBE" /proc/cmdline || echo "OVNKUBE param not found"

echo -e "\n8. Kubelet status:"
sudo systemctl status kubelet | head -20

echo -e "\n9. Check kubelet logs:"
sudo journalctl -u kubelet --no-pager | tail -50 | grep -E "(error|fail|unable|cannot)" | head -20

echo -e "\n10. Check if node tried to join:"
sudo journalctl -u kubelet --no-pager | grep -E "(api-int|Attempting to register|Unable to register)" | tail -10

echo -e "\n11. Network Manager connections:"
nmcli con show

echo -e "\n12. Check cluster domain:"
# Try to detect which cluster domain the node is trying to use
sudo grep -r "kvant.cloud" /etc/ 2>/dev/null | grep -v "Binary" | head -10