Skip to content

Commit eb1709c

Browse files
committed
minor refactoring
1 parent 2f27e7b commit eb1709c

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

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

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -223,62 +223,68 @@ func (wsc *WorkspaceController) handleWorkspaceRunning(ctx context.Context, ws *
223223
}
224224

225225
func (wsc *WorkspaceController) handleWorkspaceStop(ctx context.Context, ws *workspacev1.Workspace, req ctrl.Request) (result ctrl.Result, err error) {
226-
log := log.FromContext(ctx)
227226
span, ctx := opentracing.StartSpanFromContext(ctx, "handleWorkspaceStop")
228227
defer tracing.FinishSpan(span, &err)
229228

230229
if ws.IsConditionTrue(workspacev1.WorkspaceConditionPodRejected) {
230+
// edge case only exercised for rejected workspace pods
231231
if ws.IsConditionPresent(workspacev1.WorkspaceConditionStateWiped) {
232232
// we are done here
233233
return ctrl.Result{}, nil
234234
}
235235

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

244-
if wsc.latestWorkspace(ctx, ws) != nil {
245-
return ctrl.Result{Requeue: true, RequeueAfter: 100 * time.Millisecond}, nil
246-
}
239+
// regular case
240+
return wsc.doWorkspaceContentBackup(ctx, span, ws, req)
241+
}
247242

248-
setStateWipedCondition := func(s bool) {
249-
err := retry.RetryOnConflict(retryParams, func() error {
250-
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
251-
return err
252-
}
253-
254-
if s {
255-
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionTrue))
256-
} else {
257-
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionFalse))
258-
}
259-
return wsc.Client.Status().Update(ctx, ws)
260-
})
261-
if err != nil {
262-
log.Error(err, "failed to set StateWiped condition")
243+
func (wsc *WorkspaceController) doWipeWorkspace(ctx context.Context, ws *workspacev1.Workspace, req ctrl.Request) (result ctrl.Result, err error) {
244+
log := log.FromContext(ctx)
245+
246+
// in this case we are not interested in any backups, but instead are concerned with completely wiping all state that might be dangling somewhere
247+
if ws.IsConditionTrue(workspacev1.WorkspaceConditionContainerRunning) {
248+
// Container is still running, we need to wait for it to stop.
249+
// We should get an event when the condition changes, but requeue
250+
// anyways to make sure we act on it in time.
251+
return ctrl.Result{RequeueAfter: 500 * time.Millisecond}, nil
252+
}
253+
254+
if wsc.latestWorkspace(ctx, ws) != nil {
255+
return ctrl.Result{Requeue: true, RequeueAfter: 100 * time.Millisecond}, nil
256+
}
257+
258+
setStateWipedCondition := func(s bool) {
259+
err := retry.RetryOnConflict(retryParams, func() error {
260+
if err := wsc.Get(ctx, req.NamespacedName, ws); err != nil {
261+
return err
263262
}
264-
}
265-
log.Info("handling workspace stop - wiping mode")
266263

267-
err = wsc.operations.WipeWorkspace(ctx, ws.Name)
264+
if s {
265+
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionTrue))
266+
} else {
267+
ws.Status.SetCondition(workspacev1.NewWorkspaceConditionStateWiped("", metav1.ConditionFalse))
268+
}
269+
return wsc.Client.Status().Update(ctx, ws)
270+
})
268271
if err != nil {
269-
setStateWipedCondition(false)
270-
wsc.emitEvent(ws, "Wiping", fmt.Errorf("failed to wipe workspace: %w", err))
271-
return ctrl.Result{}, fmt.Errorf("failed to wipe workspace: %w", err)
272+
log.Error(err, "failed to set StateWiped condition")
272273
}
274+
}
275+
log.Info("handling workspace stop - wiping mode")
276+
defer log.Info("handling workspace stop - wiping done.")
273277

274-
setStateWipedCondition(true)
275-
276-
log.Info("handling workspace stop - wiping done.")
277-
return ctrl.Result{}, nil
278+
err = wsc.operations.WipeWorkspace(ctx, ws.Name)
279+
if err != nil {
280+
setStateWipedCondition(false)
281+
wsc.emitEvent(ws, "Wiping", fmt.Errorf("failed to wipe workspace: %w", err))
282+
return ctrl.Result{}, fmt.Errorf("failed to wipe workspace: %w", err)
278283
}
279284

280-
// regular case
281-
return wsc.doWorkspaceContentBackup(ctx, span, ws, req)
285+
setStateWipedCondition(true)
286+
287+
return ctrl.Result{}, nil
282288
}
283289

284290
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)