Skip to content

Commit b8df71f

Browse files
committed
wip
1 parent f4b227a commit b8df71f

27 files changed

+328
-44
lines changed

components/content-service/pkg/git/git.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,54 @@ func (c *Client) Git(ctx context.Context, subcommand string, args ...string) (er
212212
return nil
213213
}
214214

215+
// GitStatusFromFiles same as Status but reads git output from preexisting files that were generated by prestop hook
216+
func GitStatusFromFiles(ctx context.Context, loc string) (res *Status, err error) {
217+
gitout, err := os.ReadFile(filepath.Join(loc, "git1.txt"))
218+
if err != nil {
219+
return nil, err
220+
}
221+
porcelain, err := parsePorcelain(bytes.NewReader(gitout))
222+
if err != nil {
223+
return nil, err
224+
}
225+
226+
unpushedCommits := make([]string, 0)
227+
gitout, err = os.ReadFile(filepath.Join(loc, "git2.txt"))
228+
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
229+
return nil, err
230+
}
231+
if gitout != nil {
232+
out, err := io.ReadAll(bytes.NewReader(gitout))
233+
if err != nil {
234+
return nil, xerrors.Errorf("cannot determine unpushed commits: %w", err)
235+
}
236+
for _, l := range strings.Split(string(out), "\n") {
237+
tl := strings.TrimSpace(l)
238+
if tl != "" {
239+
unpushedCommits = append(unpushedCommits, tl)
240+
}
241+
}
242+
}
243+
if len(unpushedCommits) == 0 {
244+
unpushedCommits = nil
245+
}
246+
247+
latestCommit := ""
248+
gitout, err = os.ReadFile(filepath.Join(loc, "git3.txt"))
249+
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
250+
return nil, err
251+
}
252+
if len(gitout) > 0 {
253+
latestCommit = strings.TrimSpace(string(gitout))
254+
}
255+
256+
return &Status{
257+
porcelainStatus: *porcelain,
258+
UnpushedCommits: unpushedCommits,
259+
LatestCommit: latestCommit,
260+
}, nil
261+
}
262+
215263
// Status runs git status
216264
func (c *Client) Status(ctx context.Context) (res *Status, err error) {
217265
gitout, err := c.GitWithOutput(ctx, "status", "--porcelain=v2", "--branch", "-uall")

components/content-service/pkg/layer/provider.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,14 @@ func contentDescriptorToLayer(cdesc []byte) (*Layer, error) {
467467
)
468468
}
469469

470+
var prestophookScript = `
471+
#!/bin/bash
472+
cd ${GITPOD_REPO_ROOT}
473+
git status --porcelain=v2 --branch -uall > /.workspace/mark/.workspace/git1.txt
474+
git log --pretty='%h: %s' --branches --not --remotes > /.workspace/mark/.workspace/git2.txt
475+
git log --pretty=%H -n 1 > /.workspace/mark/.workspace/git3.txt
476+
`
477+
470478
// version of this function for persistent volume claim feature
471479
// we cannot use /workspace folder as when mounting /workspace folder through PVC
472480
// it will mask anything that was in container layer, hence we are using /.workspace instead here
@@ -475,6 +483,7 @@ func contentDescriptorToLayerPVC(cdesc []byte) (*Layer, error) {
475483
fileInLayer{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
476484
fileInLayer{&tar.Header{Typeflag: tar.TypeDir, Name: "/.workspace/.gitpod", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755}, nil},
477485
fileInLayer{&tar.Header{Typeflag: tar.TypeReg, Name: "/.workspace/.gitpod/content.json", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0755, Size: int64(len(cdesc))}, cdesc},
486+
fileInLayer{&tar.Header{Typeflag: tar.TypeReg, Name: "/.workspace/.gitpod/prestophook.sh", Uid: initializer.GitpodUID, Gid: initializer.GitpodGID, Mode: 0770, Size: int64(len(prestophookScript))}, []byte(prestophookScript)},
478487
)
479488
}
480489

components/ws-daemon/pkg/internal/session/workspace.go

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,14 @@ func (s *Workspace) UpdateGitStatus(ctx context.Context, podUid string) (res *cs
261261
var loc string
262262
log.Infof("UpdateGitStatus: loc: %s, checkoutLoc: %s, uid: %s", s.Location, s.CheckoutLocation, podUid)
263263
if podUid != "" {
264-
loc = filepath.Join("/mnt/pods", podUid)
265-
loc = filepath.Join(loc, "volumes/kubernetes.io~csi")
266-
log.Infof("readDir: %s", loc)
267-
dirs, err := os.ReadDir(loc)
264+
loc = filepath.Join(s.ServiceLocDaemon, "mark/.workspace")
265+
log.Infof("git status location: %s", loc)
266+
stat, err := git.GitStatusFromFiles(ctx, loc)
268267
if err != nil {
269268
return nil, err
270269
}
271-
if len(dirs) == 0 {
272-
return nil, xerrors.Errorf("cannot locate workspace pvc mount to update git status")
273-
}
274-
pvcName := ""
275-
// each workspace pod should only have one PVC attached to it
276-
for _, d := range dirs {
277-
if d.IsDir() {
278-
pvcName = d.Name()
279-
break
280-
}
281-
}
282-
loc = filepath.Join(loc, pvcName)
283-
loc = filepath.Join(loc, "mount/workspace")
284-
log.Infof("final path: %s", loc)
270+
271+
s.LastGitStatus = toGitStatus(stat)
285272
} else {
286273
loc = s.Location
287274
if loc == "" {
@@ -296,28 +283,28 @@ func (s *Workspace) UpdateGitStatus(ctx context.Context, podUid string) (res *cs
296283
log.WithField("loc", loc).WithFields(s.OWI()).Debug("not updating Git status of FWB workspace")
297284
return
298285
}
299-
}
300286

301-
loc = filepath.Join(loc, s.CheckoutLocation)
302-
if !git.IsWorkingCopy(loc) {
303-
log.WithField("loc", loc).WithField("checkout location", s.CheckoutLocation).WithFields(s.OWI()).Debug("did not find a Git working copy - not updating Git status")
304-
return nil, nil
305-
}
287+
loc = filepath.Join(loc, s.CheckoutLocation)
288+
if !git.IsWorkingCopy(loc) {
289+
log.WithField("loc", loc).WithField("checkout location", s.CheckoutLocation).WithFields(s.OWI()).Debug("did not find a Git working copy - not updating Git status")
290+
return nil, nil
291+
}
306292

307-
c := git.Client{Location: loc}
293+
c := git.Client{Location: loc}
308294

309-
err = c.Git(ctx, "config", "--global", "--add", "safe.directory", loc)
310-
if err != nil {
311-
log.WithError(err).WithFields(s.OWI()).Warn("cannot persist latest Git status")
312-
err = nil
313-
}
295+
err = c.Git(ctx, "config", "--global", "--add", "safe.directory", loc)
296+
if err != nil {
297+
log.WithError(err).WithFields(s.OWI()).Warn("cannot persist latest Git status")
298+
err = nil
299+
}
314300

315-
stat, err := c.Status(ctx)
316-
if err != nil {
317-
return nil, err
318-
}
301+
stat, err := c.Status(ctx)
302+
if err != nil {
303+
return nil, err
304+
}
319305

320-
s.LastGitStatus = toGitStatus(stat)
306+
s.LastGitStatus = toGitStatus(stat)
307+
}
321308

322309
err = s.persist()
323310
if err != nil {

components/ws-manager/pkg/manager/create.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ func (m *Manager) createWorkspaceContainer(startContext *startWorkspaceContext)
631631
Ports: []corev1.ContainerPort{
632632
{ContainerPort: startContext.IDEPort},
633633
},
634+
Lifecycle: &corev1.Lifecycle{
635+
PreStop: &corev1.LifecycleHandler{
636+
Exec: &corev1.ExecAction{
637+
Command: []string{"sh", "-c", "/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"},
638+
},
639+
},
640+
},
634641
Resources: corev1.ResourceRequirements{
635642
Limits: limits,
636643
Requests: requests,

components/ws-manager/pkg/manager/testdata/cdwp_admission.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@
175175
"successThreshold": 1,
176176
"failureThreshold": 600
177177
},
178+
"lifecycle": {
179+
"preStop": {
180+
"exec": {
181+
"command": [
182+
"sh",
183+
"-c",
184+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
185+
]
186+
}
187+
}
188+
},
178189
"terminationMessagePolicy": "File",
179190
"imagePullPolicy": "IfNotPresent",
180191
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_affinity.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@
167167
"successThreshold": 1,
168168
"failureThreshold": 600
169169
},
170+
"lifecycle": {
171+
"preStop": {
172+
"exec": {
173+
"command": [
174+
"sh",
175+
"-c",
176+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
177+
]
178+
}
179+
}
180+
},
170181
"terminationMessagePolicy": "File",
171182
"imagePullPolicy": "IfNotPresent",
172183
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_customcerts.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@
189189
"successThreshold": 1,
190190
"failureThreshold": 600
191191
},
192+
"lifecycle": {
193+
"preStop": {
194+
"exec": {
195+
"command": [
196+
"sh",
197+
"-c",
198+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
199+
]
200+
}
201+
}
202+
},
192203
"terminationMessagePolicy": "File",
193204
"imagePullPolicy": "IfNotPresent",
194205
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_empty_resource_req.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@
169169
"successThreshold": 1,
170170
"failureThreshold": 600
171171
},
172+
"lifecycle": {
173+
"preStop": {
174+
"exec": {
175+
"command": [
176+
"sh",
177+
"-c",
178+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
179+
]
180+
}
181+
}
182+
},
172183
"terminationMessagePolicy": "File",
173184
"imagePullPolicy": "IfNotPresent",
174185
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_envvars.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@
204204
"successThreshold": 1,
205205
"failureThreshold": 600
206206
},
207+
"lifecycle": {
208+
"preStop": {
209+
"exec": {
210+
"command": [
211+
"sh",
212+
"-c",
213+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
214+
]
215+
}
216+
}
217+
},
207218
"terminationMessagePolicy": "File",
208219
"imagePullPolicy": "IfNotPresent",
209220
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_fixedresources.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@
172172
"successThreshold": 1,
173173
"failureThreshold": 600
174174
},
175+
"lifecycle": {
176+
"preStop": {
177+
"exec": {
178+
"command": [
179+
"sh",
180+
"-c",
181+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
182+
]
183+
}
184+
}
185+
},
175186
"terminationMessagePolicy": "File",
176187
"imagePullPolicy": "IfNotPresent",
177188
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_fullworkspacebackup.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@
161161
"successThreshold": 1,
162162
"failureThreshold": 600
163163
},
164+
"lifecycle": {
165+
"preStop": {
166+
"exec": {
167+
"command": [
168+
"sh",
169+
"-c",
170+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
171+
]
172+
}
173+
}
174+
},
164175
"terminationMessagePolicy": "File",
165176
"imagePullPolicy": "IfNotPresent",
166177
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_imagebuild.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@
193193
"successThreshold": 1,
194194
"failureThreshold": 600
195195
},
196+
"lifecycle": {
197+
"preStop": {
198+
"exec": {
199+
"command": [
200+
"sh",
201+
"-c",
202+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
203+
]
204+
}
205+
}
206+
},
196207
"terminationMessagePolicy": "File",
197208
"imagePullPolicy": "IfNotPresent",
198209
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_imagebuild_template.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@
197197
"successThreshold": 1,
198198
"failureThreshold": 600
199199
},
200+
"lifecycle": {
201+
"preStop": {
202+
"exec": {
203+
"command": [
204+
"sh",
205+
"-c",
206+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
207+
]
208+
}
209+
}
210+
},
200211
"terminationMessagePolicy": "File",
201212
"imagePullPolicy": "IfNotPresent",
202213
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_no_ideimage.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@
169169
"successThreshold": 1,
170170
"failureThreshold": 600
171171
},
172+
"lifecycle": {
173+
"preStop": {
174+
"exec": {
175+
"command": [
176+
"sh",
177+
"-c",
178+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
179+
]
180+
}
181+
}
182+
},
172183
"terminationMessagePolicy": "File",
173184
"imagePullPolicy": "IfNotPresent",
174185
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_prebuild.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@
175175
"successThreshold": 1,
176176
"failureThreshold": 600
177177
},
178+
"lifecycle": {
179+
"preStop": {
180+
"exec": {
181+
"command": [
182+
"sh",
183+
"-c",
184+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
185+
]
186+
}
187+
}
188+
},
178189
"terminationMessagePolicy": "File",
179190
"imagePullPolicy": "IfNotPresent",
180191
"securityContext": {

components/ws-manager/pkg/manager/testdata/cdwp_prebuild_template.golden

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@
179179
"successThreshold": 1,
180180
"failureThreshold": 600
181181
},
182+
"lifecycle": {
183+
"preStop": {
184+
"exec": {
185+
"command": [
186+
"sh",
187+
"-c",
188+
"/.supervisor/workspacekit lift /.workspace/mark/.workspace/.gitpod/prestophook.sh"
189+
]
190+
}
191+
}
192+
},
182193
"terminationMessagePolicy": "File",
183194
"imagePullPolicy": "IfNotPresent",
184195
"securityContext": {

0 commit comments

Comments
 (0)