@@ -34,31 +34,125 @@ echo -e "${GREEN}Starting log collection at ${TIMESTAMP}${NC}"
3434echo " Logs will be saved to: ${LOG_DIR} "
3535echo " "
3636
37- # Function to collect logs from a pod
38- collect_pod_logs () {
37+ # Function to get pod UID for log file lookup
38+ get_pod_uid () {
39+ local pod_name=$1
40+ kubectl get pod " ${pod_name} " -n " ${NAMESPACE} " -o jsonpath=' {.metadata.uid}' 2> /dev/null || echo " "
41+ }
42+
43+ # Function to get Kind node name for cluster
44+ get_kind_node_name () {
45+ local cluster_name=$1
46+ echo " ${cluster_name} -control-plane"
47+ }
48+
49+ # Function to collect logs directly from Kind node filesystem (much faster and complete)
50+ collect_pod_logs_direct () {
3951 local pod_name=$1
4052 local cluster_name=$2
4153 local log_file_prefix=$3
42-
43- echo -e " ${YELLOW} Collecting logs from pod ${pod_name} in cluster ${cluster_name}${NC} "
44-
54+
55+ echo -e " ${YELLOW} Collecting logs from pod ${pod_name} in cluster ${cluster_name} (direct access)${NC} "
56+
57+ # Get pod UID for log directory lookup
58+ local pod_uid
59+ pod_uid=$( get_pod_uid " ${pod_name} " )
60+ if [ -z " $pod_uid " ]; then
61+ echo -e " ${RED} Could not get UID for pod ${pod_name} , falling back to kubectl logs${NC} "
62+ collect_pod_logs_kubectl " $@ "
63+ return
64+ fi
65+
4566 # Get all containers in the pod
67+ local containers
4668 containers=$( kubectl get pod " ${pod_name} " -n " ${NAMESPACE} " -o jsonpath=' {.spec.containers[*].name}' 2> /dev/null || echo " " )
47-
4869 if [ -z " $containers " ]; then
4970 echo -e " ${RED} No containers found in pod ${pod_name}${NC} "
5071 return
5172 fi
52-
73+
74+ # Get Kind node name
75+ local node_name
76+ node_name=$( get_kind_node_name " ${cluster_name} " )
77+
78+ # Construct log directory path inside the Kind node
79+ local log_dir=" /var/log/pods/${NAMESPACE} _${pod_name} _${pod_uid} "
80+
81+ # Collect logs for each container
82+ for container in $containers ; do
83+ echo " - Container ${container} :"
84+
85+ # Get all log files for this container from the Kind node
86+ local container_log_dir=" ${log_dir} /${container} "
87+ local log_files
88+ log_files=$( docker exec " ${node_name} " find " ${container_log_dir} " -name " *.log" 2> /dev/null | sort -V || echo " " )
89+
90+ if [ -z " $log_files " ]; then
91+ echo -e " ${RED} No direct log files found, falling back to kubectl logs${NC} "
92+ # Fallback to kubectl approach for this container
93+ local log_file=" ${log_file_prefix} -${container} .log"
94+ if kubectl logs " ${pod_name} " -n " ${NAMESPACE} " -c " ${container} " > " ${log_file} " 2>&1 ; then
95+ echo " -> ${log_file} (via kubectl)"
96+ else
97+ echo " -> Failed to get logs via kubectl" > " ${log_file} "
98+ fi
99+ continue
100+ fi
101+
102+ # Copy individual log files for this container
103+ local file_count=0
104+ for log_file_path in $log_files ; do
105+ file_count=$(( file_count + 1 ))
106+ local base_name
107+ base_name=$( basename " ${log_file_path} " )
108+ local individual_log_file=" ${log_file_prefix} -${container} -${base_name} "
109+
110+ {
111+ echo " # Log file metadata"
112+ echo " # Timestamp: $( date -u ' +%Y-%m-%d %H:%M:%S UTC' ) "
113+ echo " # Source: ${log_file_path} "
114+ echo " # Pod: ${pod_name} "
115+ echo " # Container: ${container} "
116+ echo " # Cluster: ${cluster_name} "
117+ echo " # Namespace: ${NAMESPACE} "
118+ echo " # Method: Direct file access from Kind node"
119+ echo " # Node: ${node_name} "
120+ echo " # Part: ${file_count} of $( echo " $log_files " | wc -l) "
121+ echo " # =================================="
122+ echo " "
123+ docker exec " ${node_name} " cat " ${log_file_path} " 2> /dev/null || echo " Failed to read ${log_file_path} "
124+ } > " ${individual_log_file} "
125+
126+ echo " -> ${individual_log_file} "
127+ done
128+ done
129+ }
130+
131+ # Function to collect logs using kubectl (fallback method)
132+ collect_pod_logs_kubectl () {
133+ local pod_name=$1
134+ local cluster_name=$2
135+ local log_file_prefix=$3
136+
137+ echo -e " ${YELLOW} Collecting logs from pod ${pod_name} in cluster ${cluster_name} (kubectl fallback)${NC} "
138+
139+ # Get all containers in the pod
140+ containers=$( kubectl get pod " ${pod_name} " -n " ${NAMESPACE} " -o jsonpath=' {.spec.containers[*].name}' 2> /dev/null || echo " " )
141+
142+ if [ -z " $containers " ]; then
143+ echo -e " ${RED} No containers found in pod ${pod_name}${NC} "
144+ return
145+ fi
146+
53147 # Collect logs for each container
54148 for container in $containers ; do
55149 log_file=" ${log_file_prefix} -${container} .log"
56150 echo " - Container ${container} -> ${log_file} "
57-
151+
58152 # Get current logs
59153 kubectl logs " ${pod_name} " -n " ${NAMESPACE} " -c " ${container} " > " ${log_file} " 2>&1 || \
60154 echo " Failed to get logs for container ${container} " > " ${log_file} "
61-
155+
62156 # Try to get previous logs if pod was restarted
63157 previous_log_file=" ${log_file_prefix} -${container} -previous.log"
64158 if kubectl logs " ${pod_name} " -n " ${NAMESPACE} " -c " ${container} " --previous > " ${previous_log_file} " 2>&1 ; then
@@ -69,6 +163,27 @@ collect_pod_logs() {
69163 done
70164}
71165
166+ # Function to collect logs from a pod (tries direct access first, falls back to kubectl)
167+ collect_pod_logs () {
168+ local pod_name=$1
169+ local cluster_name=$2
170+ local log_file_prefix=$3
171+
172+ # Get pod info for debugging
173+ local pod_info_file=" ${log_file_prefix} -pod-info.txt"
174+ echo " - Pod info -> ${pod_info_file} "
175+ {
176+ echo " === Pod Description ==="
177+ kubectl describe pod " ${pod_name} " -n " ${NAMESPACE} "
178+ echo " "
179+ echo " === Pod YAML ==="
180+ kubectl get pod " ${pod_name} " -n " ${NAMESPACE} " -o yaml
181+ } > " ${pod_info_file} " 2>&1
182+
183+ # Try direct access first (much better), fallback to kubectl if needed
184+ collect_pod_logs_direct " $@ "
185+ }
186+
72187# Collect hub cluster logs
73188echo -e " ${GREEN} === Collecting Hub Cluster Logs ===${NC} "
74189kind export kubeconfig --name " ${HUB_CLUSTER} " 2> /dev/null || {
0 commit comments