Skip to content

Commit 791efa6

Browse files
committed
minor refactoring
1 parent 5eac463 commit 791efa6

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

components/ws-daemon/pkg/controller/workspace_controller.go

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -258,62 +258,68 @@ func (wsc *WorkspaceController) handleWorkspaceRunning(ctx context.Context, ws *
258258
}
259259

260260
func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *workspacev1.Workspace, req ctrl.Request) (result ctrl.Result, err error) {
261-
log := log.FromContext(ctx)
262261
span, ctx := opentracing.StartSpanFromContext(ctx, "handleWorkspaceStop")
263262
defer tracing.FinishSpan(span, &err)
264263

265264
if ws.IsConditionTrue(workspacev1.WorkspaceConditionPodRejected) {
265+
// edge case only exercised for rejected workspace pods
266266
if ws.IsConditionPresent(workspacev1.WorkspaceConditionStateWiped) {
267267
// we are done here
268268
return ctrl.Result{}, nil
269269
}
270270

271-
// in this case we are not interested in any backups, but instead are concerned with completely wiping all state that might be dangling somewhere
272-
if ws.IsConditionTrue(workspacev1.WorkspaceConditionContainerRunning) {
273-
// Container is still running, we need to wait for it to stop.
274-
// We should get an event when the condition changes, but requeue
275-
// anyways to make sure we act on it in time.
276-
return ctrl.Result{RequeueAfter: 500 * time.Millisecond}, nil
277-
}
271+
return wsc.doWipeWorkspace(ctx, ws, req)
272+
}
278273

279-
if wsc.latestWorkspace(ctx, ws) != nil {
280-
return ctrl.Result{Requeue: true, RequeueAfter: 100 * time.Millisecond}, nil
281-
}
274+
// regular case
275+
return wsc.doWorkspaceContentBackup(ctx, span, ws, req)
276+
}
282277

283-
setStateWipedCondition := func(s bool) {
284-
err := retry.RetryOnConflict(retryParams, func() error {
285-
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
286-
return err
287-
}
278+
func (wsc *WorkspaceController) doWipeWorkspace(ctx context.Context, ws *workspacev1.Workspace, req ctrl.Request) (result ctrl.Result, err error) {
279+
log := log.FromContext(ctx)
288280

289-
if s {
290-
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionTrue))
291-
} else {
292-
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionFalse))
293-
}
294-
return wsc.Client.Status().Update(ctx, ws)
295-
})
296-
if err != nil {
297-
log.Error(err, "failed to set StateWiped condition")
281+
// in this case we are not interested in any backups, but instead are concerned with completely wiping all state that might be dangling somewhere
282+
if ws.IsConditionTrue(workspacev1.WorkspaceConditionContainerRunning) {
283+
// Container is still running, we need to wait for it to stop.
284+
// We should get an event when the condition changes, but requeue
285+
// anyways to make sure we act on it in time.
286+
return ctrl.Result{RequeueAfter: 500 * time.Millisecond}, nil
287+
}
288+
289+
if wsc.latestWorkspace(ctx, ws) != nil {
290+
return ctrl.Result{Requeue: true, RequeueAfter: 100 * time.Millisecond}, nil
291+
}
292+
293+
setStateWipedCondition := func(s bool) {
294+
err := retry.RetryOnConflict(retryParams, func() error {
295+
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
296+
return err
298297
}
299-
}
300-
log.Info("handling workspace stop - wiping mode")
301298

302-
err = wsc.operations.WipeWorkspace(ctx, ws.Name)
299+
if s {
300+
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionTrue))
301+
} else {
302+
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionFalse))
303+
}
304+
return wsc.Client.Status().Update(ctx, ws)
305+
})
303306
if err != nil {
304-
setStateWipedCondition(false)
305-
wsc.emitEvent(ws, "Wiping", fmt.Errorf("failed to wipe workspace: %w", err))
306-
return ctrl.Result{}, fmt.Errorf("failed to wipe workspace: %w", err)
307+
log.Error(err, "failed to set StateWiped condition")
307308
}
309+
}
310+
log.Info("handling workspace stop - wiping mode")
311+
defer log.Info("handling workspace stop - wiping done.")
308312

309-
setStateWipedCondition(true)
310-
311-
log.Info("handling workspace stop - wiping done.")
312-
return ctrl.Result{}, nil
313+
err = wsc.operations.WipeWorkspace(ctx, ws.Name)
314+
if err != nil {
315+
setStateWipedCondition(false)
316+
wsc.emitEvent(ws, "Wiping", fmt.Errorf("failed to wipe workspace: %w", err))
317+
return ctrl.Result{}, fmt.Errorf("failed to wipe workspace: %w", err)
313318
}
314319

315-
// regular case
316-
return wsc.doWorkspaceContentBackup(ctx, span, ws, req)
320+
setStateWipedCondition(true)
321+
322+
return ctrl.Result{}, nil
317323
}
318324

319325
func (wsc *WorkspaceController) doWorkspaceContentBackup(ctx context.Context, span opentracing.Span, ws *workspacev1.Workspace, req ctrl.Request) (result ctrl.Result, err error) {

0 commit comments

Comments
 (0)