Skip to content

Commit d0e0726

Browse files
committed
fix: include K6 helper pod logs in ChaosCenter backend with proper error handling
1 parent ebfc81a commit d0e0726

File tree

1 file changed

+27
-10
lines changed
  • chaoscenter/subscriber/pkg/k8s

1 file changed

+27
-10
lines changed

chaoscenter/subscriber/pkg/k8s/log.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,59 @@ func (k8s *k8sSubscriber) GetLogs(podName, namespace, container string) (string,
5555
// create pod log for normal pods and chaos-engine pods
5656
func (k8s *k8sSubscriber) CreatePodLog(podLog types.PodLogRequest) (types.PodLog, error) {
5757
logDetails := types.PodLog{}
58+
59+
// Try fetching logs from the main container first
5860
mainLog, err := k8s.GetLogs(podLog.PodName, podLog.PodNamespace, "main")
59-
// try getting argo pod logs
61+
6062
if err != nil {
61-
logrus.Errorf("Failed to get argo pod %v logs, err: %v", podLog.PodName, err)
62-
logDetails.MainPod = "Failed to get argo pod logs"
63+
logrus.Warnf("main container log not found for pod %v, retrying helper container", podLog.PodName)
64+
65+
// Retry with empty container name (default / helper)
66+
mainLog, err = k8s.GetLogs(podLog.PodName, podLog.PodNamespace, "")
67+
if err != nil {
68+
logrus.Errorf("failed to fetch logs for pod %v: %v", podLog.PodName, err)
69+
logDetails.MainPod = "logs not found"
70+
// Continue to chaos-engine logs even if main logs failed
71+
} else {
72+
mainLog = strconv.Quote(strings.Replace(mainLog, `"`, `'`, -1))
73+
logDetails.MainPod = mainLog[1 : len(mainLog)-1]
74+
}
6375
} else {
64-
logDetails.MainPod = strconv.Quote(strings.Replace(mainLog, `"`, `'`, -1))
65-
logDetails.MainPod = logDetails.MainPod[1 : len(logDetails.MainPod)-1]
76+
mainLog = strconv.Quote(strings.Replace(mainLog, `"`, `'`, -1))
77+
logDetails.MainPod = mainLog[1 : len(mainLog)-1]
6678
}
67-
// try getting experiment pod logs if requested
79+
80+
// Try getting experiment pod logs if requested
6881
if strings.ToLower(podLog.PodType) == "chaosengine" && podLog.ChaosNamespace != nil {
6982
chaosLog := make(map[string]string)
83+
7084
if podLog.ExpPod != nil {
7185
expLog, err := k8s.GetLogs(*podLog.ExpPod, *podLog.ChaosNamespace, "")
7286
if err == nil {
73-
chaosLog[*podLog.ExpPod] = strconv.Quote(strings.Replace(expLog, `"`, `'`, -1))
74-
chaosLog[*podLog.ExpPod] = chaosLog[*podLog.ExpPod][1 : len(chaosLog[*podLog.ExpPod])-1]
87+
expLog = strconv.Quote(strings.Replace(expLog, `"`, `'`, -1))
88+
chaosLog[*podLog.ExpPod] = expLog[1 : len(expLog)-1]
7589
} else {
7690
logrus.Errorf("Failed to get experiment pod %v logs, err: %v", *podLog.ExpPod, err)
7791
}
7892
}
93+
7994
if podLog.RunnerPod != nil {
8095
runnerLog, err := k8s.GetLogs(*podLog.RunnerPod, *podLog.ChaosNamespace, "")
8196
if err == nil {
82-
chaosLog[*podLog.RunnerPod] = strconv.Quote(strings.Replace(runnerLog, `"`, `'`, -1))
83-
chaosLog[*podLog.RunnerPod] = chaosLog[*podLog.RunnerPod][1 : len(chaosLog[*podLog.RunnerPod])-1]
97+
runnerLog = strconv.Quote(strings.Replace(runnerLog, `"`, `'`, -1))
98+
chaosLog[*podLog.RunnerPod] = runnerLog[1 : len(runnerLog)-1]
8499
} else {
85100
logrus.Errorf("Failed to get runner pod %v logs, err: %v", *podLog.RunnerPod, err)
86101
}
87102
}
103+
88104
if podLog.ExpPod == nil && podLog.RunnerPod == nil {
89105
logDetails.ChaosPod = nil
90106
} else {
91107
logDetails.ChaosPod = chaosLog
92108
}
93109
}
110+
94111
return logDetails, nil
95112
}
96113

0 commit comments

Comments
 (0)