Skip to content

Commit e2a4d60

Browse files
FedeDPpoiana
authored andcommitted
fix(pkg): fixed docker multiplexed output function.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
1 parent ba598bc commit e2a4d60

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

pkg/driverbuilder/docker.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"os"
3030
"runtime"
3131
"strconv"
32+
"strings"
3233

3334
"github.com/docker/docker/api/types"
3435
"github.com/docker/docker/api/types/container"
@@ -286,7 +287,7 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error {
286287
return err
287288
}
288289

289-
hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{Tty: false})
290+
hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{})
290291
if err != nil {
291292
return err
292293
}
@@ -396,20 +397,43 @@ func forwardLogs(logPipe io.Reader) {
396397
func multiplexedForwardLogs(logPipe io.Reader) {
397398
hdr := make([]byte, 8)
398399
for {
400+
// Load size of message
399401
_, err := logPipe.Read(hdr)
400402
if err == io.EOF {
401403
break
402404
}
403405
if err != nil {
404406
slog.With("err", err.Error()).Error("log pipe error")
407+
return
405408
}
406409
count := binary.BigEndian.Uint32(hdr[4:])
410+
411+
// Read message
407412
dat := make([]byte, count)
408-
_, err = logPipe.Read(dat)
409-
if err != nil {
410-
slog.With("err", err.Error()).Error("log pipe error")
413+
var readCnt int
414+
for uint32(readCnt) < count {
415+
readBytes, err := logPipe.Read(dat[readCnt:])
416+
readCnt += readBytes
417+
if err == io.EOF {
418+
if uint32(readCnt) == count {
419+
break
420+
}
421+
slog.With("err", err.Error()).Error("log pipe error")
422+
return
423+
}
424+
if err != nil {
425+
slog.With("err", err.Error()).Error("log pipe error")
426+
return
427+
}
428+
}
429+
430+
// Print message line by line
431+
lines := strings.Split(string(dat), "\n")
432+
for _, line := range lines {
433+
if line != "" {
434+
slog.Debug(line)
435+
}
411436
}
412-
slog.Debug(string(dat))
413437
}
414438
slog.Debug("log pipe close")
415439
}

0 commit comments

Comments
 (0)