-
Notifications
You must be signed in to change notification settings - Fork 20
chore: fix the collect-logs.sh #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -34,31 +34,125 @@ echo -e "${GREEN}Starting log collection at ${TIMESTAMP}${NC}" | |||||||
| echo "Logs will be saved to: ${LOG_DIR}" | ||||||||
| echo "" | ||||||||
|
|
||||||||
| # Function to collect logs from a pod | ||||||||
| collect_pod_logs() { | ||||||||
| # Function to get pod UID for log file lookup | ||||||||
| get_pod_uid() { | ||||||||
| local pod_name=$1 | ||||||||
| kubectl get pod "${pod_name}" -n "${NAMESPACE}" -o jsonpath='{.metadata.uid}' 2>/dev/null || echo "" | ||||||||
| } | ||||||||
|
|
||||||||
| # Function to get Kind node name for cluster | ||||||||
| get_kind_node_name() { | ||||||||
| local cluster_name=$1 | ||||||||
| echo "${cluster_name}-control-plane" | ||||||||
| } | ||||||||
|
|
||||||||
| # Function to collect logs directly from Kind node filesystem (much faster and complete) | ||||||||
| collect_pod_logs_direct() { | ||||||||
| local pod_name=$1 | ||||||||
| local cluster_name=$2 | ||||||||
| local log_file_prefix=$3 | ||||||||
|
|
||||||||
| echo -e "${YELLOW}Collecting logs from pod ${pod_name} in cluster ${cluster_name}${NC}" | ||||||||
|
|
||||||||
|
|
||||||||
| echo -e "${YELLOW}Collecting logs from pod ${pod_name} in cluster ${cluster_name} (direct access)${NC}" | ||||||||
|
|
||||||||
| # Get pod UID for log directory lookup | ||||||||
| local pod_uid | ||||||||
| pod_uid=$(get_pod_uid "${pod_name}") | ||||||||
| if [ -z "$pod_uid" ]; then | ||||||||
| echo -e "${RED}Could not get UID for pod ${pod_name}, falling back to kubectl logs${NC}" | ||||||||
| collect_pod_logs_kubectl "$@" | ||||||||
| return | ||||||||
| fi | ||||||||
|
|
||||||||
| # Get all containers in the pod | ||||||||
| local containers | ||||||||
| containers=$(kubectl get pod "${pod_name}" -n "${NAMESPACE}" -o jsonpath='{.spec.containers[*].name}' 2>/dev/null || echo "") | ||||||||
|
|
||||||||
| if [ -z "$containers" ]; then | ||||||||
| echo -e "${RED}No containers found in pod ${pod_name}${NC}" | ||||||||
| return | ||||||||
| fi | ||||||||
|
|
||||||||
|
|
||||||||
| # Get Kind node name | ||||||||
| local node_name | ||||||||
| node_name=$(get_kind_node_name "${cluster_name}") | ||||||||
|
|
||||||||
| # Construct log directory path inside the Kind node | ||||||||
| local log_dir="/var/log/pods/${NAMESPACE}_${pod_name}_${pod_uid}" | ||||||||
|
|
||||||||
| # Collect logs for each container | ||||||||
| for container in $containers; do | ||||||||
| echo " - Container ${container}:" | ||||||||
|
|
||||||||
| # Get all log files for this container from the Kind node | ||||||||
| local container_log_dir="${log_dir}/${container}" | ||||||||
| local log_files | ||||||||
| log_files=$(docker exec "${node_name}" find "${container_log_dir}" -name "*.log" 2>/dev/null | sort -V || echo "") | ||||||||
|
|
||||||||
| if [ -z "$log_files" ]; then | ||||||||
| echo -e " ${RED}No direct log files found, falling back to kubectl logs${NC}" | ||||||||
| # Fallback to kubectl approach for this container | ||||||||
| local log_file="${log_file_prefix}-${container}.log" | ||||||||
| if kubectl logs "${pod_name}" -n "${NAMESPACE}" -c "${container}" > "${log_file}" 2>&1; then | ||||||||
| echo " -> ${log_file} (via kubectl)" | ||||||||
| else | ||||||||
| echo " -> Failed to get logs via kubectl" > "${log_file}" | ||||||||
| fi | ||||||||
| continue | ||||||||
| fi | ||||||||
|
|
||||||||
| # Copy individual log files for this container | ||||||||
| local file_count=0 | ||||||||
| for log_file_path in $log_files; do | ||||||||
| file_count=$((file_count + 1)) | ||||||||
| local base_name | ||||||||
| base_name=$(basename "${log_file_path}") | ||||||||
| local individual_log_file="${log_file_prefix}-${container}-${base_name}" | ||||||||
|
|
||||||||
| { | ||||||||
| echo "# Log file metadata" | ||||||||
| echo "# Timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" | ||||||||
| echo "# Source: ${log_file_path}" | ||||||||
| echo "# Pod: ${pod_name}" | ||||||||
| echo "# Container: ${container}" | ||||||||
| echo "# Cluster: ${cluster_name}" | ||||||||
| echo "# Namespace: ${NAMESPACE}" | ||||||||
| echo "# Method: Direct file access from Kind node" | ||||||||
| echo "# Node: ${node_name}" | ||||||||
| echo "# Part: ${file_count} of $(echo "$log_files" | wc -l)" | ||||||||
|
||||||||
| echo "# ==================================" | ||||||||
| echo "" | ||||||||
| docker exec "${node_name}" cat "${log_file_path}" 2>/dev/null || echo "Failed to read ${log_file_path}" | ||||||||
| } > "${individual_log_file}" | ||||||||
|
|
||||||||
| echo " -> ${individual_log_file}" | ||||||||
| done | ||||||||
| done | ||||||||
| } | ||||||||
|
|
||||||||
| # Function to collect logs using kubectl (fallback method) | ||||||||
| collect_pod_logs_kubectl() { | ||||||||
| local pod_name=$1 | ||||||||
| local cluster_name=$2 | ||||||||
| local log_file_prefix=$3 | ||||||||
|
|
||||||||
| echo -e "${YELLOW}Collecting logs from pod ${pod_name} in cluster ${cluster_name} (kubectl fallback)${NC}" | ||||||||
|
|
||||||||
| # Get all containers in the pod | ||||||||
|
||||||||
| # Get all containers in the pod | |
| # Get all containers in the pod | |
| local containers |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log_file variable is not declared as local, which could lead to it being a global variable and potentially cause naming conflicts with other parts of the script.
local log_file="${log_file_prefix}-${container}.log"| log_file="${log_file_prefix}-${container}.log" | |
| local log_file="${log_file_prefix}-${container}.log" |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous_log_file variable is not declared as local, which could lead to it being a global variable and potentially cause naming conflicts.
local previous_log_file="${log_file_prefix}-${container}-previous.log"| previous_log_file="${log_file_prefix}-${container}-previous.log" | |
| local previous_log_file="${log_file_prefix}-${container}-previous.log" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop iterates over
$log_fileswithout quotes. If log file paths contain spaces, this will break the iteration. While unlikely in Kubernetes log paths, it's a potential issue. The variable should be properly quoted or handled as an array:This approach safely handles paths with spaces and special characters.