Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ws-daemon] Collect logs from runc command #5117

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions components/common-go/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package log

import (
"fmt"
"io"
"os"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -129,3 +130,29 @@ func (f *gcpFormatter) Format(entry *log.Entry) ([]byte, error) {

return f.JSONFormatter.Format(entry)
}

// Writer produces a writer that wraps everything that's written to it in a log message.
// Callers are expected to close the returned writer.
//
// Beware: due to logEntry not being synchronised, writes to the returned writer must not
// concurrent.
func Writer(logEntry *logrus.Entry) io.WriteCloser {
rd, rw := io.Pipe()
go func() {
b := make([]byte, 4096)
for {
n, err := rd.Read(b)
if err == io.EOF {
break
}
if err != nil {
log.WithError(err).Error("cannot read from content initializer output")
return
}

logEntry.Debug(string(b[:n]))
}
}()

return rw
}
6 changes: 4 additions & 2 deletions components/ws-daemon/pkg/content/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
withDebug = "--debug"
}

rw := log.Writer(log.WithFields(opts.OWI))
defer rw.Close()
cmd = exec.Command("runc", "--root", "state", withDebug, "--log-format", "json", "run", "gogogo")
cmd.Dir = tmpdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = rw
cmd.Stderr = rw
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions components/ws-daemon/pkg/iws/iws.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,11 @@ func nsinsider(instanceID string, targetPid int, mod func(*exec.Cmd), opts ...ns
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
rw := log.Writer(log.WithFields(log.OWI("", "", instanceID)))
defer rw.Close()

cmd.Stdout = rw
cmd.Stderr = rw
err = cmd.Run()
if err != nil {
return fmt.Errorf("cannot run nsinsider: %w", err)
Expand Down