Skip to content

[content-service] add prestop hook to extract git status #9807

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

Merged
merged 1 commit into from
May 10, 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
48 changes: 48 additions & 0 deletions components/content-service/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,54 @@ func (c *Client) Git(ctx context.Context, subcommand string, args ...string) (er
return nil
}

// GitStatusFromFiles same as Status but reads git output from preexisting files that were generated by prestop hook
func GitStatusFromFiles(ctx context.Context, loc string) (res *Status, err error) {
gitout, err := os.ReadFile(filepath.Join(loc, "git_status.txt"))
if err != nil {
return nil, err
}
porcelain, err := parsePorcelain(bytes.NewReader(gitout))
if err != nil {
return nil, err
}

unpushedCommits := make([]string, 0)
gitout, err = os.ReadFile(filepath.Join(loc, "git_log_1.txt"))
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
return nil, err
}
if gitout != nil {
out, err := io.ReadAll(bytes.NewReader(gitout))
if err != nil {
return nil, xerrors.Errorf("cannot determine unpushed commits: %w", err)
}
for _, l := range strings.Split(string(out), "\n") {
tl := strings.TrimSpace(l)
if tl != "" {
unpushedCommits = append(unpushedCommits, tl)
}
}
}
if len(unpushedCommits) == 0 {
unpushedCommits = nil
}

latestCommit := ""
gitout, err = os.ReadFile(filepath.Join(loc, "git_log_2.txt"))
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
return nil, err
}
if len(gitout) > 0 {
latestCommit = strings.TrimSpace(string(gitout))
}

return &Status{
porcelainStatus: *porcelain,
UnpushedCommits: unpushedCommits,
LatestCommit: latestCommit,
}, nil
}

// Status runs git status
func (c *Client) Status(ctx context.Context) (res *Status, err error) {
gitout, err := c.GitWithOutput(ctx, "status", "--porcelain=v2", "--branch", "-uall")
Expand Down
241 changes: 241 additions & 0 deletions components/content-service/pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -228,6 +229,246 @@ func TestGitStatus(t *testing.T) {
}
}

func TestGitStatusFromFiles(t *testing.T) {
tests := []struct {
Name string
Prep func(context.Context, *Client) error
Result *Status
Error error
}{
{
"no commits",
func(ctx context.Context, c *Client) error {
if err := c.Git(ctx, "init"); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchOID: "(initial)",
BranchHead: "master",
},
},
nil,
},
{
"clean copy",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "master",
BranchOID: notEmpty,
},
LatestCommit: notEmpty,
},
nil,
},
{
"untracked files",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(c.Location, "another-file"), []byte{}, 0755); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "master",
BranchOID: notEmpty,
UntrackedFiles: []string{"another-file"},
},
LatestCommit: notEmpty,
},
nil,
},
{
"uncommitted files",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "master",
BranchOID: notEmpty,
UncommitedFiles: []string{"first-file"},
},
LatestCommit: notEmpty,
},
nil,
},
{
"unpushed commits",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
return err
}
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "master",
BranchOID: notEmpty,
},
UnpushedCommits: []string{notEmpty},
LatestCommit: notEmpty,
},
nil,
},
{
"unpushed commits in new branch",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
if err := c.Git(ctx, "checkout", "-b", "otherbranch"); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
return err
}
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "otherbranch",
BranchOID: notEmpty,
},
UnpushedCommits: []string{notEmpty},
LatestCommit: notEmpty,
},
nil,
},

{
"pending in sub-dir files",
func(ctx context.Context, c *Client) error {
if err := initFromRemote(ctx, c); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(c.Location, "this/is/a/nested/test"), 0755); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(c.Location, "this/is/a/nested/test/first-file"), []byte("foobar"), 0755); err != nil {
return err
}
return nil
},
&Status{
porcelainStatus: porcelainStatus{
BranchHead: "master",
BranchOID: notEmpty,
UntrackedFiles: []string{"this/is/a/nested/test/first-file"},
},
LatestCommit: notEmpty,
},
nil,
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

client, err := newGitClient(ctx)
if err != nil {
t.Errorf("cannot prep %s: %v", test.Name, err)
return
}

err = test.Prep(ctx, client)
if err != nil {
t.Errorf("cannot prep %s: %v", test.Name, err)
return
}

gitout, err := client.GitWithOutput(ctx, "status", "--porcelain=v2", "--branch", "-uall")
if err != nil {
t.Errorf("error calling GitWithOutput: %v", err)
return
}
if err := os.WriteFile(filepath.Join("/tmp", "git_status.txt"), gitout, 0755); err != nil {
t.Errorf("error creating file: %v", err)
return
}

gitout, err = client.GitWithOutput(ctx, "log", "--pretty=%h: %s", "--branches", "--not", "--remotes")
if err != nil {
t.Errorf("error calling GitWithOutput: %v", err)
return
}
if err := os.WriteFile(filepath.Join("/tmp", "git_log_1.txt"), gitout, 0755); err != nil {
t.Errorf("error creating file: %v", err)
return
}

gitout, err = client.GitWithOutput(ctx, "log", "--pretty=%H", "-n", "1")
if err != nil && !strings.Contains(err.Error(), "fatal: your current branch 'master' does not have any commits yet") {
t.Errorf("error calling GitWithOutput: %v", err)
return
}
if err := os.WriteFile(filepath.Join("/tmp", "git_log_2.txt"), gitout, 0755); err != nil {
t.Errorf("error creating file: %v", err)
return
}

status, err := GitStatusFromFiles(ctx, "/tmp")
if err != test.Error {
t.Errorf("expected error does not match for %s: %v != %v", test.Name, err, test.Error)
return
}

if status != nil {
if test.Result.BranchOID == notEmpty && status.LatestCommit != "" {
test.Result.BranchOID = status.LatestCommit
}
if test.Result.LatestCommit == notEmpty && status.LatestCommit != "" {
test.Result.LatestCommit = status.LatestCommit
}
for _, c := range test.Result.UnpushedCommits {
if c == notEmpty {
if len(status.UnpushedCommits) == 0 {
t.Errorf("expected unpushed commits")
}

test.Result.UnpushedCommits = status.UnpushedCommits
break
}
}
}

if diff := cmp.Diff(test.Result, status, cmp.AllowUnexported(Status{})); diff != "" {
t.Errorf("unexpected status (-want +got):\n%s", diff)
}

})
}
}

func newGitClient(ctx context.Context) (*Client, error) {
loc, err := os.MkdirTemp("", "gittest")
if err != nil {
Expand Down
22 changes: 17 additions & 5 deletions components/content-service/pkg/layer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,27 @@ func contentDescriptorToLayer(cdesc []byte) (*Layer, error) {
)
}

var prestophookScript = `#!/bin/bash
cd ${GITPOD_REPO_ROOT}
git config --global --add safe.directory ${GITPOD_REPO_ROOT}
git status --porcelain=v2 --branch -uall > /.workspace/prestophookdata/git_status.txt
git log --pretty='%h: %s' --branches --not --remotes > /.workspace/prestophookdata/git_log_1.txt
git log --pretty=%H -n 1 > /.workspace/prestophookdata/git_log_2.txt
`

// version of this function for persistent volume claim feature
// we cannot use /workspace folder as when mounting /workspace folder through PVC
// it will mask anything that was in container layer, hence we are using /.workspace instead here
func contentDescriptorToLayerPVC(cdesc []byte) (*Layer, error) {
return layerFromContent(
fileInLayer{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
fileInLayer{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace/.gitpod", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
fileInLayer{&tar.Header{Typeflag: tar.TypeReg, Name: "/.workspace/.gitpod/content.json", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755, Size: int64(len(cdesc))}, cdesc},
)
layers := []fileInLayer{
{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace/.gitpod", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
{&tar.Header{Typeflag: tar.TypeReg, Name: "/.supervisor/prestophook.sh", Uid: 0, Gid: 0, Mode: 0775, Size: int64(len(prestophookScript))}, []byte(prestophookScript)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why we are setting Uid and Gid to 0 but then permission to 0775?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uid/Gid to 0 to make sure no one else can edit\modify this file from inside the workspace (otherwise you can execute things from ring1).
775 to allow gitpod user to execute it as part of prestop hook.

}
if len(cdesc) > 0 {
layers = append(layers, fileInLayer{&tar.Header{Typeflag: tar.TypeReg, Name: "/.workspace/.gitpod/content.json", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755, Size: int64(len(cdesc))}, cdesc})
}
return layerFromContent(layers...)
}

func workspaceReadyLayer(src csapi.WorkspaceInitSource) (*Layer, error) {
Expand Down
17 changes: 17 additions & 0 deletions components/ws-daemon/pkg/content/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
Expand Down Expand Up @@ -240,6 +241,22 @@ func (s *WorkspaceService) InitWorkspace(ctx context.Context, req *api.InitWorks
}
}

if req.PersistentVolumeClaim {
// create a folder that is used to store data from running prestophook
deamonDir := fmt.Sprintf("%s-daemon", req.Id)
prestophookDir := filepath.Join(s.config.WorkingArea, deamonDir, "prestophookdata")
err = os.MkdirAll(prestophookDir, 0755)
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot create prestophookdata folder")
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot create prestophookdata: %v", err))
}
_, err = exec.CommandContext(ctx, "chown", "-R", fmt.Sprintf("%d:%d", wsinit.GitpodUID, wsinit.GitpodGID), prestophookDir).CombinedOutput()
if err != nil {
log.WithError(err).WithField("workspaceId", req.Id).Error("cannot chown prestophookdata folder")
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("cannot chown prestophookdata: %v", err))
}
}

// Tell the world we're done
err = workspace.MarkInitDone(ctx)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions components/ws-manager/pkg/manager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ func (m *Manager) createDefiniteWorkspacePod(startContext *startWorkspaceContext
// not needed, since it is using dedicated disk
pod.Spec.Containers[0].VolumeMounts[0].MountPropagation = nil

// add prestop hook to capture git status
pod.Spec.Containers[0].Lifecycle = &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"/bin/sh", "-c", "/.supervisor/workspacekit lift /.supervisor/prestophook.sh"},
},
},
}

// pavel: 133332 is the Gitpod UID (33333) shifted by 99999. The shift happens inside the workspace container due to the user namespace use.
// We set this magical ID to make sure that gitpod user inside the workspace can write into /workspace folder mounted by PVC
gitpodGUID := int64(133332)
Expand Down