Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[content-init] Propagate sensible error messages #7753

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewFromRequest(ctx context.Context, loc string, rs storage.DirectDownloader
initializer = &EmptyInitializer{}
}
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot initialize workspace: %v", err))
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return initializer, nil
}
Expand Down
4 changes: 4 additions & 0 deletions components/ws-daemon/cmd/content-initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"fmt"
"os"

"github.com/gitpod-io/gitpod/common-go/log"
Expand All @@ -18,6 +19,9 @@ func main() {

err := content.RunInitializerChild()
if err != nil {
errfd := os.NewFile(uintptr(3), "errout")
_, _ = fmt.Fprintf(errfd, err.Error())

os.Exit(42)
}
}
27 changes: 25 additions & 2 deletions components/ws-daemon/pkg/content/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/google/uuid"
"github.com/opencontainers/runc/libcontainer/specconv"
Expand Down Expand Up @@ -237,22 +239,43 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
name = "init-ws-" + opts.OWI.InstanceID
}

args = append(args, "--log-format", "json", "run", name)
args = append(args, "--log-format", "json", "run")
args = append(args, "--preserve-fds", "1")
args = append(args, name)

errIn, errOut, err := os.Pipe()
if err != nil {
return err
}
errch := make(chan []byte, 1)
go func() {
errmsg, _ := ioutil.ReadAll(errIn)
errch <- errmsg
}()

var cmdOut bytes.Buffer
cmd := exec.Command("runc", args...)
cmd.Dir = tmpdir
cmd.Stdout = &cmdOut
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.ExtraFiles = []*os.File{errOut}
err = cmd.Run()
log.FromBuffer(&cmdOut, log.WithFields(opts.OWI.Fields()))
errOut.Close()

var errmsg []byte
select {
case errmsg = <-errch:
case <-time.After(1 * time.Second):
errmsg = []byte("failed to read content initializer response")
}
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0. If it's 42, it was deliberate.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 42 {
log.WithError(err).WithField("exitCode", status.ExitStatus()).WithField("args", args).Error("content init failed")
return xerrors.Errorf("content initializer failed")
return xerrors.Errorf(string(errmsg))
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/ws-daemon/pkg/content/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ func (s *WorkspaceService) InitWorkspace(ctx context.Context, req *api.InitWorks
err = RunInitializer(ctx, workspace.Location, req.Initializer, remoteContent, opts)
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot initialize workspace")
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot initialize workspace: %s", err.Error()))
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
}

// Tell the world we're done
err = workspace.MarkInitDone(ctx)
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot initialize workspace")
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot finish workspace init: %v", err))
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot finish workspace init: %v", err))
}

return &api.InitWorkspaceResponse{}, nil
Expand Down