From 14026aadfdb60eb662a38f1437b938a32557f884 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Tue, 9 Feb 2021 03:32:34 +0000 Subject: [PATCH] Allow to get container logs asynchronously This will make sure that stderr of the generic container in gathered in a go routine, this way we won't have to wait to see debug messages from the container until it finishes. Change-Id: I929719c3369f4f5796d452b5f990bf5f28a4c0d5 --- pkg/container/api.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/container/api.go b/pkg/container/api.go index 7336f346b..d0457e310 100644 --- a/pkg/container/api.go +++ b/pkg/container/api.go @@ -157,6 +157,9 @@ func (c *clientV1Alpha1) runAirship() error { c.conf.Spec.Image, c.conf.Spec.Airship.Cmd) + // write logs asynchronously while waiting for for container to finish + go writeLogs(cont) + err = cont.WaitUntilFinished() if err != nil { return err @@ -168,20 +171,8 @@ func (c *clientV1Alpha1) runAirship() error { } defer rOut.Close() - rErr, err := cont.GetContainerLogs(GetLogOptions{Stderr: true}) - if err != nil { - return err - } - defer rOut.Close() - parsedOut := dlog.NewReader(rOut) - parsedErr := dlog.NewReader(rErr) - // write container stderr to airship log output - _, err = io.Copy(log.Writer(), parsedErr) - if err != nil { - return err - } return writeSink(c.resultsDir, parsedOut, c.output) } @@ -224,6 +215,22 @@ func (c *clientV1Alpha1) runKRM() error { return fns.Execute() } +func writeLogs(cont Container) { + stderr, err := cont.GetContainerLogs(GetLogOptions{ + Stderr: true, + Follow: true}) + if err != nil { + log.Fatalf("received an error trying to attach to container to retrieve logs %e", err) + return + } + defer stderr.Close() + parsedStdErr := dlog.NewReader(stderr) + _, err = io.Copy(log.Writer(), parsedStdErr) + if err != nil { + log.Fatalf("received an error while copying logs from container %e", err) + } +} + // writeSink output to directory on filesystem sink func writeSink(path string, rc io.Reader, out io.Writer) error { inputs := []kio.Reader{&kio.ByteReader{Reader: rc}}