From 29b6c1a650595893a51ceaac6831bcf6ba92870a Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Wed, 23 Aug 2023 17:25:45 +0300 Subject: [PATCH 1/3] feat(logtail): Also send the end of file when found Signed-off-by: Cezar Craciunoiu --- internal/logtail/logtail.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/logtail/logtail.go b/internal/logtail/logtail.go index dcb6e7b61..7753786a6 100644 --- a/internal/logtail/logtail.go +++ b/internal/logtail/logtail.go @@ -112,6 +112,7 @@ func peekAndRead(file *os.File, reader *bufio.Reader, logs *chan string, errs *c } reader.Reset(file) + *errs <- io.EOF return true } From bc4b5f3724c2d77e4f9f2a958a27ada320b3f23d Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Wed, 23 Aug 2023 17:29:29 +0300 Subject: [PATCH 2/3] feat(run): Shutdown only after logging finishes Signed-off-by: Cezar Craciunoiu --- cmd/kraft/run/run.go | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/cmd/kraft/run/run.go b/cmd/kraft/run/run.go index 42a78821b..471044f74 100644 --- a/cmd/kraft/run/run.go +++ b/cmd/kraft/run/run.go @@ -7,10 +7,12 @@ package run import ( "errors" "fmt" + "io" "io/fs" "os" "path/filepath" "strings" + "time" "github.com/MakeNowJust/heredoc" "github.com/rancher/wrangler/pkg/signals" @@ -357,6 +359,8 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error { } var exitErr error + requestShutdown := false + logsFinished := make(chan bool, 1) // Tail the logs if -d|--detach is not provided if !opts.Detach { @@ -372,6 +376,12 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error { loop: for { + if requestShutdown { + <-logsFinished + signals.RequestShutdown() + break loop + } + // Wait on either channel select { case update := <-events: @@ -379,11 +389,10 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error { case machineapi.MachineStateErrored: signals.RequestShutdown() exitErr = fmt.Errorf("machine fatally exited") - break loop + requestShutdown = true case machineapi.MachineStateExited, machineapi.MachineStateFailed: - signals.RequestShutdown() - break loop + requestShutdown = true } case err := <-errs: @@ -392,7 +401,7 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error { break loop case <-ctx.Done(): - break loop + requestShutdown = true } } }() @@ -412,22 +421,35 @@ func (opts *Run) Run(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not listen for machine logs: %v", err) } + var line string loop: for { // Wait on either channel select { - case line := <-logs: + case <-time.After(10 * time.Millisecond): + if requestShutdown && line == "" { + break loop + } else if line != "" { + line = "" + } + + case line = <-logs: fmt.Fprint(iostreams.G(ctx).Out, line) case err := <-errs: - log.G(ctx).Errorf("received event error: %v", err) - signals.RequestShutdown() - break loop + if errors.Is(err, io.EOF) && requestShutdown { + break loop + } else if !errors.Is(err, io.EOF) { + log.G(ctx).Errorf("received log error: %v", err) + signals.RequestShutdown() + break loop + } case <-ctx.Done(): break loop } } + logsFinished <- true // Remove the instance on Ctrl+C if the --rm flag is passed if opts.Remove { From 0e91e47ade81012a20606cb0c4867d5da77852ed Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Wed, 23 Aug 2023 17:30:27 +0300 Subject: [PATCH 3/3] fix(qemu): Ignore the first EOF received Signed-off-by: Cezar Craciunoiu --- machine/qemu/v1alpha1.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine/qemu/v1alpha1.go b/machine/qemu/v1alpha1.go index 706ae191c..53f18c082 100644 --- a/machine/qemu/v1alpha1.go +++ b/machine/qemu/v1alpha1.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "io" "io/fs" "net" "os" @@ -752,7 +753,9 @@ func (service *machineV1alpha1Service) Logs(ctx context.Context, machine *machin return out, errOut, nil case err := <-errOut: - return nil, nil, err + if err != io.EOF { + return nil, nil, err + } case <-ctx.Done(): return out, errOut, nil