Skip to content

Commit 30ddbe5

Browse files
committed
feat: add options for capturing run output
1 parent efb9a14 commit 30ddbe5

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

pkg/commands/commands.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ type DockerCommand interface {
6464
IsArgsEnvsRequiredInCache() bool
6565
}
6666

67-
func GetCommand(cmd instructions.Command, fileContext util.FileContext, useNewRun bool, cacheCopy bool, cacheRun bool) (DockerCommand, error) {
67+
func GetCommand(cmd instructions.Command, fileContext util.FileContext, useNewRun bool, cacheCopy bool, cacheRun bool, output *RunOutput) (DockerCommand, error) {
6868
switch c := cmd.(type) {
6969
case *instructions.RunCommand:
7070
if useNewRun {
71-
return &RunMarkerCommand{cmd: c, shdCache: cacheRun}, nil
71+
return &RunMarkerCommand{cmd: c, shdCache: cacheRun, output: output}, nil
7272
}
73-
return &RunCommand{cmd: c, shdCache: cacheRun}, nil
73+
return &RunCommand{cmd: c, shdCache: cacheRun, output: output}, nil
7474
case *instructions.CopyCommand:
7575
return &CopyCommand{cmd: c, fileContext: fileContext, shdCache: cacheCopy}, nil
7676
case *instructions.ExposeCommand:

pkg/commands/run.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package commands
1818

1919
import (
2020
"fmt"
21+
"io"
2122
"os"
2223
"os/exec"
2324
"strings"
@@ -33,9 +34,15 @@ import (
3334
"github.com/sirupsen/logrus"
3435
)
3536

37+
type RunOutput struct {
38+
Stdout io.Writer
39+
Stderr io.Writer
40+
}
41+
3642
type RunCommand struct {
3743
BaseCommand
3844
cmd *instructions.RunCommand
45+
output *RunOutput
3946
shdCache bool
4047
}
4148

@@ -49,10 +56,19 @@ func (r *RunCommand) IsArgsEnvsRequiredInCache() bool {
4956
}
5057

5158
func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
52-
return runCommandInExec(config, buildArgs, r.cmd)
59+
return runCommandInExec(config, buildArgs, r.cmd, r.output)
5360
}
5461

55-
func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun *instructions.RunCommand) error {
62+
func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun *instructions.RunCommand, output *RunOutput) error {
63+
if output == nil {
64+
output = &RunOutput{}
65+
}
66+
if output.Stdout == nil {
67+
output.Stdout = os.Stdout
68+
}
69+
if output.Stderr == nil {
70+
output.Stderr = os.Stderr
71+
}
5672
var newCommand []string
5773
if cmdRun.PrependShell {
5874
// This is the default shell on Linux
@@ -89,8 +105,8 @@ func runCommandInExec(config *v1.Config, buildArgs *dockerfile.BuildArgs, cmdRun
89105
cmd := exec.Command(newCommand[0], newCommand[1:]...)
90106

91107
cmd.Dir = setWorkDirIfExists(config.WorkingDir)
92-
cmd.Stdout = os.Stdout
93-
cmd.Stderr = os.Stderr
108+
cmd.Stdout = output.Stdout
109+
cmd.Stderr = output.Stderr
94110
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
95111
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
96112

pkg/config/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package config
1919
import (
2020
"errors"
2121
"fmt"
22+
"io"
2223
"strconv"
2324
"strings"
2425
"time"
@@ -83,6 +84,8 @@ type KanikoOptions struct {
8384
IgnoreVarRun bool
8485
SkipUnusedStages bool
8586
RunV2 bool
87+
RunStdout io.Writer
88+
RunStderr io.Writer
8689
CacheCopyLayers bool
8790
CacheRunLayers bool
8891
ForceBuildMetadata bool

pkg/executor/build.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
133133
}
134134

135135
for _, cmd := range s.stage.Commands {
136-
command, err := commands.GetCommand(cmd, fileContext, opts.RunV2, opts.CacheCopyLayers, opts.CacheRunLayers)
136+
command, err := commands.GetCommand(cmd, fileContext, opts.RunV2, opts.CacheCopyLayers, opts.CacheRunLayers, &commands.RunOutput{
137+
Stdout: opts.RunStdout,
138+
Stderr: opts.RunStderr,
139+
})
137140
if err != nil {
138141
return nil, err
139142
}

pkg/executor/build_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ func Test_stageBuilder_populateCompositeKey(t *testing.T) {
848848
}
849849

850850
fc1 := util.FileContext{Root: "workspace"}
851-
dockerCommand1, err := commands.GetCommand(instructions1[0], fc1, false, true, true)
851+
dockerCommand1, err := commands.GetCommand(instructions1[0], fc1, false, true, true, nil)
852852
if err != nil {
853853
t.Fatal(err)
854854
}
@@ -859,7 +859,7 @@ func Test_stageBuilder_populateCompositeKey(t *testing.T) {
859859
}
860860

861861
fc2 := util.FileContext{Root: "workspace"}
862-
dockerCommand2, err := commands.GetCommand(instructions[0], fc2, false, true, true)
862+
dockerCommand2, err := commands.GetCommand(instructions[0], fc2, false, true, true, nil)
863863
if err != nil {
864864
t.Fatal(err)
865865
}
@@ -1515,6 +1515,7 @@ func getCommands(fileContext util.FileContext, cmds []instructions.Command, cach
15151515
false,
15161516
cacheCopy,
15171517
cacheRun,
1518+
nil,
15181519
)
15191520
if err != nil {
15201521
panic(err)
@@ -1610,7 +1611,7 @@ func Test_stageBuild_populateCompositeKeyForCopyCommand(t *testing.T) {
16101611
}
16111612

16121613
fc := util.FileContext{Root: "workspace"}
1613-
copyCommand, err := commands.GetCommand(instructions[0], fc, false, true, true)
1614+
copyCommand, err := commands.GetCommand(instructions[0], fc, false, true, true, nil)
16141615
if err != nil {
16151616
t.Fatal(err)
16161617
}

0 commit comments

Comments
 (0)