summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-06-08 21:17:26 +0200
committerGitHub <noreply@github.com>2021-06-08 21:17:26 +0200
commitb5e5730f0c2459546a72129f0908618b38f0a975 (patch)
tree503b7c51fd88de06c57c8a07f4a1ebf3738055a6 /libpod
parent9edd08c3f16d5730ac783fde3706bda49401b79f (diff)
parent84b55eec27967d1a528c4f49f100393668638e1a (diff)
downloadpodman-b5e5730f0c2459546a72129f0908618b38f0a975.tar.gz
podman-b5e5730f0c2459546a72129f0908618b38f0a975.tar.bz2
podman-b5e5730f0c2459546a72129f0908618b38f0a975.zip
Merge pull request #10600 from vrothberg/fix-10596
logs: k8s-file: fix race
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container_log.go54
1 files changed, 33 insertions, 21 deletions
diff --git a/libpod/container_log.go b/libpod/container_log.go
index c207df819..a30e4f5cc 100644
--- a/libpod/container_log.go
+++ b/libpod/container_log.go
@@ -4,11 +4,10 @@ import (
"context"
"fmt"
"os"
- "time"
"github.com/containers/podman/v3/libpod/define"
+ "github.com/containers/podman/v3/libpod/events"
"github.com/containers/podman/v3/libpod/logs"
- "github.com/hpcloud/tail/watch"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -94,27 +93,40 @@ func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOption
}()
// Check if container is still running or paused
if options.Follow {
+ state, err := c.State()
+ if err != nil || state != define.ContainerStateRunning {
+ // If the container isn't running or if we encountered
+ // an error getting its state, instruct the logger to
+ // read the file until EOF.
+ tailError := t.StopAtEOF()
+ if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
+ logrus.Error(tailError)
+ }
+ if errors.Cause(err) != define.ErrNoSuchCtr {
+ logrus.Error(err)
+ }
+ return nil
+ }
+
+ // The container is running, so we need to wait until the container exited
go func() {
- for {
- state, err := c.State()
- time.Sleep(watch.POLL_DURATION)
- if err != nil {
- tailError := t.StopAtEOF()
- if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
- logrus.Error(tailError)
- }
- if errors.Cause(err) != define.ErrNoSuchCtr {
- logrus.Error(err)
- }
- break
- }
- if state != define.ContainerStateRunning && state != define.ContainerStatePaused {
- tailError := t.StopAtEOF()
- if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
- logrus.Error(tailError)
- }
- break
+ eventChannel := make(chan *events.Event)
+ eventOptions := events.ReadOptions{
+ EventChannel: eventChannel,
+ Filters: []string{"event=died", "container=" + c.ID()},
+ Stream: true,
+ }
+ go func() {
+ if err := c.runtime.Events(ctx, eventOptions); err != nil {
+ logrus.Errorf("Error waiting for container to exit: %v", err)
}
+ }()
+ // Now wait for the died event and signal to finish
+ // reading the log until EOF.
+ <-eventChannel
+ tailError := t.StopAtEOF()
+ if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
+ logrus.Error(tailError)
}
}()
}