From 98286a965e4c4c680deeb43d3397b51089968366 Mon Sep 17 00:00:00 2001 From: lili-wan <109381849+lili-wan@users.noreply.github.com> Date: Wed, 21 Feb 2024 04:33:44 -0800 Subject: [PATCH] fix: log analyzer failed with multiple containers in the pod (#920) * Log analyzer failed with multiple containers in the pod #884 Signed-off-by: lwan3 * Merge conflicts from main Signed-off-by: lwan3 --------- Signed-off-by: lwan3 Co-authored-by: lwan3 Co-authored-by: Alex Jones --- pkg/analyzer/log.go | 55 +++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pkg/analyzer/log.go b/pkg/analyzer/log.go index 2f6aa5b..282b223 100644 --- a/pkg/analyzer/log.go +++ b/pkg/analyzer/log.go @@ -49,29 +49,17 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) { // Iterate through each pod for _, pod := range list.Items { - var failures []common.Failure podName := pod.Name - podLogOptions := v1.PodLogOptions{ - TailLines: &tailLines, - } - - podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context) - if err != nil { - failures = append(failures, common.Failure{ - Text: fmt.Sprintf("Error %s from Pod %s", err.Error(), pod.Name), - Sensitive: []common.Sensitive{ - { - Unmasked: pod.Name, - Masked: util.MaskString(pod.Name), - }, - }, - }) - - } else { - rawlogs := string(podLogs) - if errorPattern.MatchString(strings.ToLower(rawlogs)) { + for _, c := range pod.Spec.Containers { + var failures []common.Failure + podLogOptions := v1.PodLogOptions{ + TailLines: &tailLines, + Container: c.Name, + } + podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context) + if err != nil { failures = append(failures, common.Failure{ - Text: printErrorLines(pod.Name, pod.Namespace, rawlogs, errorPattern), + Text: fmt.Sprintf("Error %s from Pod %s", err.Error(), pod.Name), Sensitive: []common.Sensitive{ { Unmasked: pod.Name, @@ -79,14 +67,27 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) { }, }, }) + } else { + rawlogs := string(podLogs) + if errorPattern.MatchString(strings.ToLower(rawlogs)) { + failures = append(failures, common.Failure{ + Text: printErrorLines(pod.Name, pod.Namespace, rawlogs, errorPattern), + Sensitive: []common.Sensitive{ + { + Unmasked: pod.Name, + Masked: util.MaskString(pod.Name), + }, + }, + }) + } } - } - if len(failures) > 0 { - preAnalysis[fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)] = common.PreAnalysis{ - FailureDetails: failures, - Pod: pod, + if len(failures) > 0 { + preAnalysis[fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, c.Name)] = common.PreAnalysis{ + FailureDetails: failures, + Pod: pod, + } + AnalyzerErrorsMetric.WithLabelValues(kind, pod.Name, pod.Namespace).Set(float64(len(failures))) } - AnalyzerErrorsMetric.WithLabelValues(kind, pod.Name, pod.Namespace).Set(float64(len(failures))) } } for key, value := range preAnalysis {