Skip to content
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

fix: Ensure Finalizer Patch #530

Merged
merged 4 commits into from
Jul 20, 2024
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
28 changes: 16 additions & 12 deletions pkg/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -40,7 +41,6 @@
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -68,20 +68,12 @@

klog.InfoS("Reconciling", "workspace", req.NamespacedName)

if err := c.ensureFinalizer(ctx, workspaceObj); err != nil {
return reconcile.Result{}, err

Check warning on line 72 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}
// Handle deleting workspace, garbage collect all the resources.
if !workspaceObj.DeletionTimestamp.IsZero() {
return c.deleteWorkspace(ctx, workspaceObj)
} else {
// Ensure finalizer
if !controllerutil.ContainsFinalizer(workspaceObj, consts.WorkspaceFinalizer) {
controllerutil.AddFinalizer(workspaceObj, consts.WorkspaceFinalizer)
updateCopy := workspaceObj.DeepCopy()
if updateErr := c.Update(ctx, updateCopy, &client.UpdateOptions{}); updateErr != nil {
klog.ErrorS(updateErr, "failed to ensure the finalizer to the workspace",
"workspace", klog.KObj(updateCopy))
return ctrl.Result{}, updateErr
}
}
}

if workspaceObj.Inference != nil && workspaceObj.Inference.Preset != nil {
Expand All @@ -94,6 +86,18 @@
return c.addOrUpdateWorkspace(ctx, workspaceObj)
}

func (c *WorkspaceReconciler) ensureFinalizer(ctx context.Context, workspaceObj *kaitov1alpha1.Workspace) error {
if !controllerutil.ContainsFinalizer(workspaceObj, consts.WorkspaceFinalizer) {
patch := client.MergeFrom(workspaceObj.DeepCopy())
controllerutil.AddFinalizer(workspaceObj, consts.WorkspaceFinalizer)
if err := c.Client.Patch(ctx, workspaceObj, patch); err != nil {
klog.ErrorS(err, "failed to ensure the finalizer to the workspace", "workspace", klog.KObj(workspaceObj))
return err

Check warning on line 95 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L89-L95

Added lines #L89 - L95 were not covered by tests
}
}
return nil

Check warning on line 98 in pkg/controllers/workspace_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/workspace_controller.go#L98

Added line #L98 was not covered by tests
}

func (c *WorkspaceReconciler) addOrUpdateWorkspace(ctx context.Context, wObj *kaitov1alpha1.Workspace) (reconcile.Result, error) {
// Read ResourceSpec
err := c.applyWorkspaceResource(ctx, wObj)
Expand Down
Loading