Skip to content

Commit

Permalink
Adding to the diagnose Logs for Previous Pods (who failed)
Browse files Browse the repository at this point in the history
  • Loading branch information
yodigos committed Nov 6, 2024
1 parent 720a09d commit aa10215
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cli/cmd/diagnose_util/logs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,36 @@ func FetchOdigosComponentsLogs(ctx context.Context, client *kube.Client, logDir

func fetchPodLogs(ctx context.Context, client *kube.Client, odigosNamespace string, pod v1.Pod, logDir string) {
for _, container := range pod.Spec.Containers {
fetchingContainerLogs(ctx, client, odigosNamespace, pod, container, logDir)
fetchingContainerLogs(ctx, client, odigosNamespace, pod, container, logDir, false)

// Check if the pod has been restarted
if pod.Status.ContainerStatuses != nil {
for _, status := range pod.Status.ContainerStatuses {
if status.RestartCount > 0 {
// Fetch logs from the previous instance of the container
fetchingContainerLogs(ctx, client, odigosNamespace, pod, container, logDir, true)
}
}
}

}
}

func fetchingContainerLogs(ctx context.Context, client *kube.Client, odigosNamespace string, pod v1.Pod, container v1.Container, logDir string) {
func fetchingContainerLogs(ctx context.Context, client *kube.Client, odigosNamespace string, pod v1.Pod, container v1.Container, logDir string, previous bool) {
logPrefix := fmt.Sprintf("Fetching logs for Pod: %s, Container: %s, Node: %s", pod.Name, container.Name, pod.Spec.NodeName)
fmt.Printf(logPrefix + "\n")

// Define the log file path for saving compressed logs
logFilePath := filepath.Join(logDir, pod.Name+"_"+container.Name+"_"+pod.Spec.NodeName+".log.gz")
logFileName := getLogFileName(pod, container, previous)
logFilePath := filepath.Join(logDir, logFileName)
logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Printf(logPrefix+" - Failed - Error creating log file: %v\n", err)
return
}
defer logFile.Close()

req := client.CoreV1().Pods(odigosNamespace).GetLogs(pod.Name, &v1.PodLogOptions{})
req := client.CoreV1().Pods(odigosNamespace).GetLogs(pod.Name, &v1.PodLogOptions{Previous: previous})
logStream, err := req.Stream(ctx)
if err != nil {
fmt.Printf(logPrefix+" - Failed - Error creating log stream: %v\n", err)
Expand All @@ -76,6 +87,13 @@ func fetchingContainerLogs(ctx context.Context, client *kube.Client, odigosNames
}
}

func getLogFileName(pod v1.Pod, container v1.Container, previous bool) string {
if previous {
return pod.Name + "_" + container.Name + "_" + pod.Spec.NodeName + "_previous.log.gz"
}
return pod.Name + "_" + container.Name + "_" + pod.Spec.NodeName + ".log.gz"
}

func saveLogsToGzipFileInBatches(logFile *os.File, logStream io.ReadCloser, bufferSize int) error {
// Create a gzip writer to compress the logs
gzipWriter := gzip.NewWriter(logFile)
Expand Down

0 comments on commit aa10215

Please sign in to comment.