logs: consume all file until EOF on exited container

If the container is not found, do not stop reading the log file
immediately but wait until we reach again EOF.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2019-01-21 22:19:21 +01:00
parent 8f68b281e4
commit a561196bfe
No known key found for this signature in database
GPG Key ID: E4730F97F60286ED

View File

@ -292,6 +292,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
var watcher *fsnotify.Watcher
var parse parseFunc
var stop bool
found := true
writer := newLogWriter(stdout, stderr, opts)
msg := &logMessage{}
for {
@ -305,6 +306,10 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
return fmt.Errorf("failed to read log file %q: %v", path, err)
}
if opts.follow {
// The container is not running, we got to the end of the log.
if !found {
return nil
}
// Reset seek so that if this is an incomplete line,
// it will be read again.
if _, err := f.Seek(-int64(len(l)), io.SeekCurrent); err != nil {
@ -323,8 +328,12 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
// the event.
continue
}
var recreated bool
// Wait until the next log change.
found, recreated, err := waitLogs(ctx, containerID, watcher, runtimeService)
found, recreated, err = waitLogs(ctx, containerID, watcher, runtimeService)
if err != nil {
return err
}
if recreated {
newF, err := os.Open(path)
if err != nil {
@ -343,9 +352,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
}
r = bufio.NewReader(f)
}
if !found {
return err
}
// If the container exited consume data until the next EOF
continue
}
// Should stop after writing the remaining content.