@@ -242,12 +242,31 @@ spec:
242242 NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)
243243 echo "🔍 Namespace: $NAMESPACE"
244244
245- # Extract both type and number from hostname
246- POD_TYPE=$(hostname | sed 's/{{ include "osmosis-fullnode.fullname" . }}-sentinel-\(node\|seed\)-.*$/\1/')
247- NODE_NUMBER=$(hostname | sed 's/{{ include "osmosis-fullnode.fullname" . }}-sentinel-\(node\|seed\)-\([0-9]*\).*$/\2/')
248- echo "🔍 Hostname: $(hostname)"
245+ # Get current hostname
246+ CURRENT_HOSTNAME=$(hostname)
247+ echo "🔍 Hostname: $CURRENT_HOSTNAME"
248+
249+ # Extract pod type and number from hostname
250+ # Expected format: {fullname}-{type}-{number}-sentinel-{cronJobId}-{podId}
251+ # Example: fullnodes-stage-osmosis-fullnode-node-0-sentinel-29224300-mktf4
252+ FULLNAME="{{ include "osmosis-fullnode.fullname" . }}"
253+
254+ # Extract type (node or seed) - look for pattern after fullname
255+ if echo "$CURRENT_HOSTNAME" | grep -q "${FULLNAME}-node-"; then
256+ POD_TYPE="node"
257+ # Extract node number
258+ NODE_NUMBER=$(echo "$CURRENT_HOSTNAME" | sed "s/^${FULLNAME}-node-\([0-9]*\)-sentinel-.*$/\1/")
259+ elif echo "$CURRENT_HOSTNAME" | grep -q "${FULLNAME}-seed-"; then
260+ POD_TYPE="seed"
261+ # Extract seed number
262+ NODE_NUMBER=$(echo "$CURRENT_HOSTNAME" | sed "s/^${FULLNAME}-seed-\([0-9]*\)-sentinel-.*$/\1/")
263+ else
264+ POD_TYPE="unknown"
265+ NODE_NUMBER="unknown"
266+ fi
267+
249268 echo "🔍 Pod Type: $POD_TYPE"
250- echo "🔍 Extracted Node Number: $NODE_NUMBER"
269+ echo "🔍 Node Number: $NODE_NUMBER"
251270
252271 OSMOSIS_APP="{{ include "osmosis-fullnode.fullname" . }}"
253272 OSMOSIS_POD="{{ include "osmosis-fullnode.fullname" . }}-0"
@@ -269,25 +288,38 @@ spec:
269288 exit 1
270289 fi
271290
272- # Check for CrashLoopBackOff status
273- POD_STATUS=$(kubectl get pod $OSMOSIS_POD -n $NAMESPACE -o jsonpath='{.status.containerStatuses[0].state.waiting.reason}' 2>/dev/null || echo "Running")
274- RESTART_COUNT=$(kubectl get pod $OSMOSIS_POD -n $NAMESPACE -o jsonpath='{.status.containerStatuses[0].restartCount}')
291+ # Check for CrashLoopBackOff status - handle case where pod is running
292+ POD_STATUS=$(kubectl get pod $OSMOSIS_POD -n $NAMESPACE -o jsonpath='{.status.containerStatuses[0].state.waiting.reason}' 2>/dev/null)
293+ if [ -z "$POD_STATUS" ]; then
294+ # Pod is not in waiting state, check if it's running
295+ POD_PHASE=$(kubectl get pod $OSMOSIS_POD -n $NAMESPACE -o jsonpath='{.status.phase}' 2>/dev/null)
296+ POD_STATUS="${POD_PHASE:-Unknown}"
297+ fi
298+
299+ RESTART_COUNT=$(kubectl get pod $OSMOSIS_POD -n $NAMESPACE -o jsonpath='{.status.containerStatuses[0].restartCount}' 2>/dev/null || echo "0")
275300
276301 # Check directory size
277302 TOTAL_SIZE=$(kubectl exec $(hostname) -n $NAMESPACE -- du -sb $MONITOR_PATH 2>/dev/null | awk '{print $1}' || echo 0)
278303 SIZE_THRESHOLD=$(echo "${MAX_DIR_SIZE_GB:-100} * 1024 * 1024 * 1024" | bc) # Convert GB to bytes
279304
280- echo "📊 Total size: $(echo "$TOTAL_SIZE / 1024 / 1024" | bc)MB"
281- echo "📊 Threshold: $(echo "$SIZE_THRESHOLD / 1024 / 1024" | bc)MB"
305+ # Convert sizes to MB for display
306+ TOTAL_SIZE_MB=$(echo "scale=0; $TOTAL_SIZE / 1024 / 1024" | bc)
307+ THRESHOLD_MB=$(echo "scale=0; $SIZE_THRESHOLD / 1024 / 1024" | bc)
308+
309+ echo "📊 Total size: ${TOTAL_SIZE_MB}MB"
310+ echo "📊 Threshold: ${THRESHOLD_MB}MB"
282311 echo "📊 Pod Status: $POD_STATUS"
283312 echo "📊 Restart Count: $RESTART_COUNT"
284313
285- # Check if cleanup is needed
286- if [ "$POD_STATUS" = "CrashLoopBackOff" ] || [ "${RESTART_COUNT:-0}" -gt "${MAX_NODE_RESTART_COUNT}" ]; then
287- echo "⚠️ Pod $OSMOSIS_POD is in CrashLoopBackOff state or has too many restarts"
314+ # Check if cleanup is needed - properly quote variables and handle empty values
315+ MAX_RESTART_COUNT="${MAX_NODE_RESTART_COUNT:-10}"
316+ CURRENT_RESTART_COUNT="${RESTART_COUNT:-0}"
317+
318+ if [ "$POD_STATUS" = "CrashLoopBackOff" ] || [ "$CURRENT_RESTART_COUNT" -gt "$MAX_RESTART_COUNT" ]; then
319+ echo "⚠️ Pod $OSMOSIS_POD is in CrashLoopBackOff state or has too many restarts ($CURRENT_RESTART_COUNT > $MAX_RESTART_COUNT)"
288320 handle_cleanup $NAMESPACE $OSMOSIS_APP $OSMOSIS_POD
289321 elif [ $(echo "$TOTAL_SIZE > $SIZE_THRESHOLD" | bc -l) -eq 1 ]; then
290- echo "⚠️ Directory size ($(echo "$TOTAL_SIZE / 1024 / 1024" | bc) MB) exceeds threshold ($(echo "$SIZE_THRESHOLD / 1024 / 1024" | bc) MB)"
322+ echo "⚠️ Directory size (${TOTAL_SIZE_MB} MB) exceeds threshold (${THRESHOLD_MB} MB)"
291323 handle_cleanup $NAMESPACE $OSMOSIS_APP $OSMOSIS_POD
292324 else
293325 echo "✅ Pod $OSMOSIS_POD is running normally and directory size is within limits"
0 commit comments