mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-24 04:38:53 +00:00
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 <lili_wan@intuit.com> * Merge conflicts from main Signed-off-by: lwan3 <lili_wan@intuit.com> --------- Signed-off-by: lwan3 <lili_wan@intuit.com> Co-authored-by: lwan3 <lili_wan@intuit.com> Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
@@ -49,29 +49,17 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
|
|||||||
// Iterate through each pod
|
// Iterate through each pod
|
||||||
|
|
||||||
for _, pod := range list.Items {
|
for _, pod := range list.Items {
|
||||||
var failures []common.Failure
|
|
||||||
podName := pod.Name
|
podName := pod.Name
|
||||||
podLogOptions := v1.PodLogOptions{
|
for _, c := range pod.Spec.Containers {
|
||||||
TailLines: &tailLines,
|
var failures []common.Failure
|
||||||
}
|
podLogOptions := v1.PodLogOptions{
|
||||||
|
TailLines: &tailLines,
|
||||||
podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context)
|
Container: c.Name,
|
||||||
if err != nil {
|
}
|
||||||
failures = append(failures, common.Failure{
|
podLogs, err := a.Client.Client.CoreV1().Pods(pod.Namespace).GetLogs(podName, &podLogOptions).DoRaw(a.Context)
|
||||||
Text: fmt.Sprintf("Error %s from Pod %s", err.Error(), pod.Name),
|
if err != nil {
|
||||||
Sensitive: []common.Sensitive{
|
|
||||||
{
|
|
||||||
Unmasked: pod.Name,
|
|
||||||
Masked: util.MaskString(pod.Name),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
} else {
|
|
||||||
rawlogs := string(podLogs)
|
|
||||||
if errorPattern.MatchString(strings.ToLower(rawlogs)) {
|
|
||||||
failures = append(failures, common.Failure{
|
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{
|
Sensitive: []common.Sensitive{
|
||||||
{
|
{
|
||||||
Unmasked: pod.Name,
|
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 {
|
||||||
if len(failures) > 0 {
|
preAnalysis[fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, c.Name)] = common.PreAnalysis{
|
||||||
preAnalysis[fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)] = common.PreAnalysis{
|
FailureDetails: failures,
|
||||||
FailureDetails: failures,
|
Pod: pod,
|
||||||
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 {
|
for key, value := range preAnalysis {
|
||||||
|
Reference in New Issue
Block a user