Skip to content

[ws-manager-mk2] Reduce duplicate delete pod calls #16579

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
Feb 27, 2023
Merged
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
46 changes: 20 additions & 26 deletions components/ws-manager-mk2/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,7 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp
switch {
// if there is a pod, and it's failed, delete it
case wsk8s.ConditionPresentAndTrue(workspace.Status.Conditions, string(workspacev1.WorkspaceConditionFailed)) && !isPodBeingDeleted(pod):
err := r.Client.Delete(ctx, pod)
if errors.IsNotFound(err) {
// pod is gone - nothing to do here
} else {
return ctrl.Result{Requeue: true}, err
}
return r.deleteWorkspacePod(ctx, pod, "workspace failed")

// if the pod was stopped by request, delete it
case wsk8s.ConditionPresentAndTrue(workspace.Status.Conditions, string(workspacev1.WorkspaceConditionStoppedByRequest)) && !isPodBeingDeleted(pod):
Expand All @@ -215,31 +210,14 @@ func (r *WorkspaceReconciler) actOnStatus(ctx context.Context, workspace *worksp

// if the workspace timed out, delete it
case wsk8s.ConditionPresentAndTrue(workspace.Status.Conditions, string(workspacev1.WorkspaceConditionTimeout)) && !isPodBeingDeleted(pod):
err := r.Client.Delete(ctx, pod)
if errors.IsNotFound(err) {
// pod is gone - nothing to do here
} else {
return ctrl.Result{Requeue: true}, err
}
return r.deleteWorkspacePod(ctx, pod, "timed out")

// if the content initialization failed, delete the pod
case wsk8s.ConditionWithStatusAndReason(workspace.Status.Conditions, string(workspacev1.WorkspaceConditionContentReady), false, "InitializationFailure") && !isPodBeingDeleted(pod):
err := r.Client.Delete(ctx, pod)
if errors.IsNotFound(err) {
// pod is gone - nothing to do here
} else {
return ctrl.Result{Requeue: true}, err
}
return r.deleteWorkspacePod(ctx, pod, "init failed")

case isWorkspaceBeingDeleted(workspace) && !isPodBeingDeleted(pod):
// Workspace was requested to be deleted, propagate by deleting the Pod.
// The Pod deletion will then trigger workspace disposal steps.
err := r.Client.Delete(ctx, pod)
if errors.IsNotFound(err) {
// pod is gone - nothing to do here
} else {
return ctrl.Result{Requeue: true}, err
}
return r.deleteWorkspacePod(ctx, pod, "workspace deleted")

case workspace.Status.Headless && workspace.Status.Phase == workspacev1.WorkspacePhaseStopped && !isPodBeingDeleted(pod):
// Workspace was requested to be deleted, propagate by deleting the Pod.
Expand Down Expand Up @@ -315,6 +293,22 @@ func (r *WorkspaceReconciler) updateMetrics(ctx context.Context, workspace *work
r.metrics.rememberWorkspace(workspace)
}

func (r *WorkspaceReconciler) deleteWorkspacePod(ctx context.Context, pod *corev1.Pod, reason string) (ctrl.Result, error) {
log := log.FromContext(ctx).WithValues("workspace", pod.Name, "reason", reason)
log.V(1).Info("deleting workspace pod")

// Workspace was requested to be deleted, propagate by deleting the Pod.
// The Pod deletion will then trigger workspace disposal steps.
err := r.Client.Delete(ctx, pod)
if errors.IsNotFound(err) {
// pod is gone - nothing to do here
} else {
return ctrl.Result{Requeue: true}, err
}

return ctrl.Result{}, nil
}

var (
wsOwnerKey = ".metadata.controller"
apiGVStr = workspacev1.GroupVersion.String()
Expand Down