From e3a1c78f565afba2395190940c782b5fc872931a Mon Sep 17 00:00:00 2001 From: Hao Liu <44379968+TheRealHaoLiu@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:59:03 -0500 Subject: [PATCH] Fix index out of range error when no timestamp in log PR https://github.com/ansible/receptor/pull/1187 introduced a situation that causes ``` panic: runtime error: index out of range [1] with length 1 goroutine 534519 [running]: github.com/ansible/receptor/pkg/workceptor.(*KubeUnit).kubeLoggingWithReconnect(0xc001506b00, 0xc004a97400?, 0xc009cb9fd0?, 0xc00cd1d510, 0xc00cd1d520) /Users/haoli/projects/src/github.com/TheRealHaoLiu/receptor/pkg/workceptor/kubernetes.go:395 +0x108b created by github.com/ansible/receptor/pkg/workceptor.(*KubeUnit).runWorkUsingLogger /Users/haoli/projects/src/github.com/TheRealHaoLiu/receptor/pkg/workceptor/kubernetes.go:834 +0xc5e ``` This PR will write the whole line into stdout if first word in the log is not an timestamp This can both prevent the index out of range error and allow us to record the line in stdout to aid further debugging of this issue. Co-Authored-By: Matthew Sandoval --- pkg/workceptor/kubernetes.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/workceptor/kubernetes.go b/pkg/workceptor/kubernetes.go index 063e750bb..4b208245f 100644 --- a/pkg/workceptor/kubernetes.go +++ b/pkg/workceptor/kubernetes.go @@ -385,14 +385,16 @@ func (kw *KubeUnit) kubeLoggingWithReconnect(streamWait *sync.WaitGroup, stdout } split := strings.SplitN(line, " ", 2) - timeStamp := ParseTime(split[0]) - if timeStamp != nil { - if !timeStamp.After(sinceTime) && !successfulWrite { + msg := line + timestamp := ParseTime(split[0]) + if timestamp != nil { + kw.GetWorkceptor().nc.GetLogger().Debug("No timestamp received, log line: '%s'", line) + if !timestamp.After(sinceTime) && !successfulWrite { continue } - sinceTime = *timeStamp + sinceTime = *timestamp + msg = split[1] } - msg := split[1] _, err = stdout.Write([]byte(msg)) if err != nil {