Skip to content
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

feat: Logs for Previous Pods #1700

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading