Fix IP registration: correct labels, better error handling, table verification
This commit is contained in:
parent
38897e474f
commit
8ecd75b53d
@ -724,7 +724,12 @@ spec:
|
||||
|
||||
# Helper function to run MySQL commands via kubectl exec
|
||||
run_mysql() {
|
||||
kubectl exec -n "$NAMESPACE" "$MYSQL_POD" -- mysql -u "$MYSQL_USER" -p"$MYSQL_PWD" "$MYSQL_DB" -sN -e "$1" 2>/dev/null || true
|
||||
local result
|
||||
result=$(kubectl exec -n "$NAMESPACE" "$MYSQL_POD" -- mysql -u "$MYSQL_USER" -p"$MYSQL_PWD" "$MYSQL_DB" -sN -e "$1" 2>&1) || {
|
||||
echo " MySQL error: $result" >&2
|
||||
return 1
|
||||
}
|
||||
echo "$result"
|
||||
}
|
||||
|
||||
# Function to register IP for an app
|
||||
@ -737,28 +742,59 @@ spec:
|
||||
fi
|
||||
|
||||
# Get or create app_id
|
||||
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'" || echo "")
|
||||
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'")
|
||||
|
||||
if [ -z "$APP_ID" ]; then
|
||||
echo " Creating app entry for: $app_name"
|
||||
run_mysql "INSERT INTO apps (name, token, creation_date, last_update, is_deleted) VALUES ('$app_name', UUID(), NOW(), NOW(), 0)" || true
|
||||
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'" || echo "")
|
||||
if run_mysql "INSERT INTO apps (name, token, creation_date, last_update, is_deleted) VALUES ('$app_name', UUID(), NOW(), NOW(), 0)"; then
|
||||
APP_ID=$(run_mysql "SELECT id FROM apps WHERE name='$app_name'")
|
||||
echo " Created app with ID: $APP_ID"
|
||||
else
|
||||
echo " Failed to create app entry for: $app_name"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$APP_ID" ]; then
|
||||
# Check if IP already exists
|
||||
EXISTING=$(run_mysql "SELECT id FROM app_ips_accesses WHERE app_id=$APP_ID AND ipaddress='$ip'" || echo "")
|
||||
EXISTING=$(run_mysql "SELECT id FROM app_ips_accesses WHERE app_id=$APP_ID AND ipaddress='$ip'")
|
||||
|
||||
if [ -z "$EXISTING" ]; then
|
||||
echo " Registering IP $ip for app $app_name (app_id: $APP_ID)"
|
||||
run_mysql "INSERT INTO app_ips_accesses (app_id, ipaddress, creation_date, last_update, is_deleted) VALUES ($APP_ID, '$ip', NOW(), NOW(), 0)" || true
|
||||
if run_mysql "INSERT INTO app_ips_accesses (app_id, ipaddress, creation_date, last_update, is_deleted) VALUES ($APP_ID, '$ip', NOW(), NOW(), 0)"; then
|
||||
echo " Successfully registered IP $ip"
|
||||
else
|
||||
echo " Failed to register IP $ip"
|
||||
fi
|
||||
else
|
||||
echo " IP $ip already registered for $app_name"
|
||||
fi
|
||||
else
|
||||
echo " Could not get APP_ID for $app_name"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Verify required tables exist
|
||||
echo "Checking database tables..."
|
||||
TABLES=$(run_mysql "SHOW TABLES")
|
||||
echo "Available tables: $TABLES"
|
||||
|
||||
if ! echo "$TABLES" | grep -q "apps"; then
|
||||
echo "ERROR: 'apps' table not found in database"
|
||||
echo "Database may not be initialized. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! echo "$TABLES" | grep -q "app_ips_accesses"; then
|
||||
echo "ERROR: 'app_ips_accesses' table not found in database"
|
||||
echo "Database may not be initialized. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Required tables found. Proceeding with IP registration..."
|
||||
echo ""
|
||||
|
||||
# 1. Collect common bridge/gateway IPs (handles SNAT across different clusters)
|
||||
COMMON_BRIDGE_IPS="10.244.0.1 10.96.0.1 172.17.0.1"
|
||||
echo "Common bridge IPs: $COMMON_BRIDGE_IPS"
|
||||
@ -793,10 +829,20 @@ spec:
|
||||
echo "Processing service: $service"
|
||||
|
||||
# Get actual pod IPs for this service
|
||||
# Try multiple label selectors as different charts use different labels
|
||||
POD_IPS=$(kubectl get pods -n "$NAMESPACE" \
|
||||
-l "app.kubernetes.io/instance=${service}" \
|
||||
-l "app.kubernetes.io/instance=${NAMESPACE}-${service}" \
|
||||
-o jsonpath='{.items[*].status.podIP}' 2>/dev/null) || POD_IPS=""
|
||||
|
||||
if [ -z "$POD_IPS" ]; then
|
||||
# Try backstage.io/kubernetes-id label
|
||||
POD_IPS=$(kubectl get pods -n "$NAMESPACE" \
|
||||
-l "backstage.io/kubernetes-id=${service}" \
|
||||
-o jsonpath='{.items[*].status.podIP}' 2>/dev/null) || POD_IPS=""
|
||||
fi
|
||||
|
||||
echo " Found pod IPs: ${POD_IPS:-none}"
|
||||
|
||||
# Register pod IPs
|
||||
for ip in $POD_IPS; do
|
||||
[ -n "$ip" ] && register_ip "$service" "$ip"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user