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