Skip to content

Commit da04d25

Browse files
committed
refactor logging.
1 parent 8436dc3 commit da04d25

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Client/kaniko/cmd.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,11 @@ func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath stri
119119
for _,stage := range stages{
120120
kanikoExecutor.Destination[0] = stage
121121
stdout, stderr, err := kanikoExecutor.Execute()
122+
fmt.Println(stdout)
123+
fmt.Println(stderr)
122124
if err !=nil{
123125
panic(err)
124126
}
125-
if len(stdout)==0{
126-
panic("No output from docker build.")
127-
}
128-
if len(stderr)>0{
129-
panic(stderr)
130-
}
131127
fmt.Println(stdout)
132128
}
133129
} else {

Client/kaniko/kaniko.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,34 @@ func (ke *KanikoExecutor) buildArgs() []string {
8282
func (ke *KanikoExecutor) Execute() (string, string, error) {
8383
args := ke.buildArgs()
8484
//ke.Registry.RecordImage(ke.Destination[0], "/path/to/local/image/or/remote/repository")
85-
// Change root to the new directory
85+
86+
// Change root to the new directory and handle errors
8687
if err := syscall.Chroot(ke.RootDir); err != nil {
87-
fmt.Println("Change root to the new directory failed")
88-
return "","",err
88+
return "", "", fmt.Errorf("failed to change root: %w", err)
8989
}
9090

91-
// Changing directory to "/"
91+
// Changing directory to "/" and handle errors
9292
if err := os.Chdir("/"); err != nil {
93-
fmt.Println("Change directory to / failed")
94-
return "","",err
93+
return "", "", fmt.Errorf("failed to change directory to '/': %w", err)
9594
}
9695

9796
fmt.Println(args)
9897
cmd := exec.Command("/kaniko/executor", args...)
9998
var stdout, stderr bytes.Buffer
10099
cmd.Stdout = &stdout
101100
cmd.Stderr = &stderr
101+
102102
err := cmd.Run()
103-
return stdout.String(), stderr.String(), err
103+
104+
if err != nil {
105+
// If error is an ExitError, add exit code to the returned error
106+
if exitErr, ok := err.(*exec.ExitError); ok {
107+
err = fmt.Errorf("command exited with code %d: %w", exitErr.ExitCode(), err)
108+
}
109+
return stdout.String(), stderr.String(), err
110+
}
111+
112+
return stdout.String(), stderr.String(), nil
104113
}
105114

106115
func New(registry shared.Registry,envProvider *environment.EnvProvider) (shared.DockerBuilder, shared.ExecutorInterface) {

0 commit comments

Comments
 (0)