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 e3a1c78
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 {

Check warning on line 392 in pkg/workceptor/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pkg/workceptor/kubernetes.go#L388-L392

Added lines #L388 - L392 were not covered by tests
continue
}
sinceTime = *timeStamp
sinceTime = *timestamp
msg = split[1]

Check warning on line 396 in pkg/workceptor/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pkg/workceptor/kubernetes.go#L395-L396

Added lines #L395 - L396 were not covered by tests
}
msg := split[1]

_, err = stdout.Write([]byte(msg))
if err != nil {
Expand Down

0 comments on commit e3a1c78

Please sign in to comment.