Skip to content

Commit

Permalink
Fix index out of range error when no timestamp in log
Browse files Browse the repository at this point in the history
PR #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 <[email protected]>
  • Loading branch information
TheRealHaoLiu and matoval committed Nov 19, 2024
1 parent ded8059 commit e95b01c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/workceptor/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e95b01c

Please sign in to comment.