Skip to content

Commit

Permalink
fix(pkg): fixed docker multiplexed output function.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Dec 12, 2023
1 parent ba598bc commit e2a4d60
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions pkg/driverbuilder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os"
"runtime"
"strconv"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -286,7 +287,7 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error {
return err
}

hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{Tty: false})
hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{})
if err != nil {
return err
}
Expand Down Expand Up @@ -396,20 +397,43 @@ func forwardLogs(logPipe io.Reader) {
func multiplexedForwardLogs(logPipe io.Reader) {
hdr := make([]byte, 8)
for {
// Load size of message
_, err := logPipe.Read(hdr)
if err == io.EOF {
break
}
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
return
}
count := binary.BigEndian.Uint32(hdr[4:])

// Read message
dat := make([]byte, count)
_, err = logPipe.Read(dat)
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
var readCnt int
for uint32(readCnt) < count {
readBytes, err := logPipe.Read(dat[readCnt:])
readCnt += readBytes
if err == io.EOF {
if uint32(readCnt) == count {
break
}
slog.With("err", err.Error()).Error("log pipe error")
return
}
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
return
}
}

// Print message line by line
lines := strings.Split(string(dat), "\n")
for _, line := range lines {
if line != "" {
slog.Debug(line)
}
}
slog.Debug(string(dat))
}
slog.Debug("log pipe close")
}

0 comments on commit e2a4d60

Please sign in to comment.