Skip to content

Commit 2e74855

Browse files
committed
pvc: take snapshot even the workspace does not exist in ws-daemon
Signed-off-by: JenTing Hsiao <[email protected]>
1 parent 36beceb commit 2e74855

File tree

1 file changed

+64
-41
lines changed

1 file changed

+64
-41
lines changed

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

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ func (m *Monitor) finalizeWorkspaceContent(ctx context.Context, wso *workspaceOb
10541054
}
10551055
}
10561056

1057+
var snc wsdaemon.WorkspaceContentServiceClient
10571058
doBackup := wso.WasEverReady() && !wso.IsWorkspaceHeadless()
10581059
doBackupLogs := tpe == api.WorkspaceType_PREBUILD
10591060
doSnapshot := tpe == api.WorkspaceType_PREBUILD
@@ -1069,49 +1070,22 @@ func (m *Monitor) finalizeWorkspaceContent(ctx context.Context, wso *workspaceOb
10691070
return &csapi.GitStatus{}, nil
10701071
}
10711072

1072-
// we're not yet finalizing - start the process
1073-
snc, err := m.manager.connectToWorkspaceDaemon(ctx, *wso)
1074-
if err != nil {
1075-
tracing.LogError(span, err)
1076-
return nil, status.Errorf(codes.Unavailable, "cannot connect to workspace daemon: %q", err)
1077-
}
1078-
1079-
var workspaceExistsResult *wsdaemon.IsWorkspaceExistsResponse
1080-
workspaceExistsResult, err = snc.IsWorkspaceExists(ctx, &wsdaemon.IsWorkspaceExistsRequest{Id: workspaceID})
1081-
if err != nil {
1082-
tracing.LogError(span, err)
1083-
return nil, err
1084-
}
1085-
if !workspaceExistsResult.Exists {
1086-
// nothing to backup, workspace does not exist
1087-
return nil, status.Error(codes.NotFound, "workspace does not exist")
1088-
}
1089-
1090-
// make sure that workspace was ready, otherwise there is no need to backup anything
1091-
// as we might backup corrupted workspace state
1092-
// this also ensures that if INITIALIZING still going, that we will wait for it to finish before disposing the workspace
1093-
_, err = snc.WaitForInit(ctx, &wsdaemon.WaitForInitRequest{Id: workspaceID})
1094-
if err != nil {
1095-
tracing.LogError(span, err)
1096-
return nil, err
1097-
}
1098-
1099-
// only set status to started if we actually confirmed that workspace is ready and we are about to do actual disposal
1100-
// otherwise we risk overwriting previous disposal status
1101-
if !markedDisposalStatusStarted {
1102-
statusStarted := &workspaceDisposalStatus{
1103-
Status: DisposalStarted,
1104-
}
1105-
err = m.manager.markDisposalStatus(ctx, workspaceID, statusStarted)
1106-
if err != nil {
1107-
tracing.LogError(span, err)
1108-
log.WithError(err).Error("was unable to update pod's start disposal status - this might cause an incorrect disposal status")
1109-
} else {
1110-
markedDisposalStatusStarted = true
1073+
if pvcFeatureEnabled {
1074+
// only set status to started if we actually confirmed that workspace is ready and we are about to do actual disposal
1075+
// otherwise we risk overwriting previous disposal status
1076+
if !markedDisposalStatusStarted {
1077+
statusStarted := &workspaceDisposalStatus{
1078+
Status: DisposalStarted,
1079+
}
1080+
err = m.manager.markDisposalStatus(ctx, workspaceID, statusStarted)
1081+
if err != nil {
1082+
tracing.LogError(span, err)
1083+
log.WithError(err).Error("was unable to update pod's start disposal status - this might cause an incorrect disposal status")
1084+
} else {
1085+
markedDisposalStatusStarted = true
1086+
}
11111087
}
1112-
}
11131088

1114-
if pvcFeatureEnabled {
11151089
// pvc was created with the name of the pod. see createDefiniteWorkspacePod()
11161090
pvcName := wso.Pod.Name
11171091
if !createdVolumeSnapshot {
@@ -1267,6 +1241,48 @@ func (m *Monitor) finalizeWorkspaceContent(ctx context.Context, wso *workspaceOb
12671241
deletedPVC = true
12681242
}
12691243
} else if doSnapshot {
1244+
// we're not yet finalizing - start the process
1245+
snc, err = m.manager.connectToWorkspaceDaemon(ctx, *wso)
1246+
if err != nil {
1247+
tracing.LogError(span, err)
1248+
return nil, status.Errorf(codes.Unavailable, "cannot connect to workspace daemon: %q", err)
1249+
}
1250+
1251+
var workspaceExistsResult *wsdaemon.IsWorkspaceExistsResponse
1252+
workspaceExistsResult, err = snc.IsWorkspaceExists(ctx, &wsdaemon.IsWorkspaceExistsRequest{Id: workspaceID})
1253+
if err != nil {
1254+
tracing.LogError(span, err)
1255+
return nil, err
1256+
}
1257+
if !workspaceExistsResult.Exists {
1258+
// nothing to backup, workspace does not exist
1259+
return nil, status.Error(codes.NotFound, "workspace does not exist")
1260+
}
1261+
1262+
// make sure that workspace was ready, otherwise there is no need to backup anything
1263+
// as we might backup corrupted workspace state
1264+
// this also ensures that if INITIALIZING still going, that we will wait for it to finish before disposing the workspace
1265+
_, err = snc.WaitForInit(ctx, &wsdaemon.WaitForInitRequest{Id: workspaceID})
1266+
if err != nil {
1267+
tracing.LogError(span, err)
1268+
return nil, err
1269+
}
1270+
1271+
// only set status to started if we actually confirmed that workspace is ready and we are about to do actual disposal
1272+
// otherwise we risk overwriting previous disposal status
1273+
if !markedDisposalStatusStarted {
1274+
statusStarted := &workspaceDisposalStatus{
1275+
Status: DisposalStarted,
1276+
}
1277+
err = m.manager.markDisposalStatus(ctx, workspaceID, statusStarted)
1278+
if err != nil {
1279+
tracing.LogError(span, err)
1280+
log.WithError(err).Error("was unable to update pod's start disposal status - this might cause an incorrect disposal status")
1281+
} else {
1282+
markedDisposalStatusStarted = true
1283+
}
1284+
}
1285+
12701286
// if this is a prebuild take a snapshot and mark the workspace
12711287
var res *wsdaemon.TakeSnapshotResponse
12721288
res, err = snc.TakeSnapshot(ctx, &wsdaemon.TakeSnapshotRequest{Id: workspaceID})
@@ -1296,6 +1312,13 @@ func (m *Monitor) finalizeWorkspaceContent(ctx context.Context, wso *workspaceOb
12961312

12971313
// DiposeWorkspace will "degenerate" to a simple wait if the finalization/disposal process is already running.
12981314
// This is unlike the initialization process where we wait for things to finish in a later phase.
1315+
// we're not yet finalizing - start the process
1316+
snc, err = m.manager.connectToWorkspaceDaemon(ctx, *wso)
1317+
if err != nil {
1318+
tracing.LogError(span, err)
1319+
return nil, status.Errorf(codes.Unavailable, "cannot connect to workspace daemon: %q", err)
1320+
}
1321+
12991322
resp, err := snc.DisposeWorkspace(ctx, &wsdaemon.DisposeWorkspaceRequest{
13001323
Id: workspaceID,
13011324
Backup: doBackup && !pvcFeatureEnabled,

0 commit comments

Comments
 (0)