Skip to content

Commit

Permalink
refactor logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanHnter committed Feb 19, 2024
1 parent 8436dc3 commit da04d25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
8 changes: 2 additions & 6 deletions Client/kaniko/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,11 @@ func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath stri
for _,stage := range stages{
kanikoExecutor.Destination[0] = stage
stdout, stderr, err := kanikoExecutor.Execute()
fmt.Println(stdout)
fmt.Println(stderr)
if err !=nil{
panic(err)
}
if len(stdout)==0{
panic("No output from docker build.")
}
if len(stderr)>0{
panic(stderr)
}
fmt.Println(stdout)
}
} else {
Expand Down
23 changes: 16 additions & 7 deletions Client/kaniko/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,34 @@ func (ke *KanikoExecutor) buildArgs() []string {
func (ke *KanikoExecutor) Execute() (string, string, error) {
args := ke.buildArgs()
//ke.Registry.RecordImage(ke.Destination[0], "/path/to/local/image/or/remote/repository")
// Change root to the new directory

// Change root to the new directory and handle errors
if err := syscall.Chroot(ke.RootDir); err != nil {
fmt.Println("Change root to the new directory failed")
return "","",err
return "", "", fmt.Errorf("failed to change root: %w", err)
}

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

fmt.Println(args)
cmd := exec.Command("/kaniko/executor", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
return stdout.String(), stderr.String(), err

if err != nil {
// If error is an ExitError, add exit code to the returned error
if exitErr, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("command exited with code %d: %w", exitErr.ExitCode(), err)
}
return stdout.String(), stderr.String(), err
}

return stdout.String(), stderr.String(), nil
}

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

0 comments on commit da04d25

Please sign in to comment.