Skip to content

Commit

Permalink
fix: Ensure Finalizer Patch (#530)
Browse files Browse the repository at this point in the history
**Reason for Change**:
This is some code cleanup on ensure finalizer. Main change is moving
code into seperate if statement and using a patch instead of update to
add finalizer.
  • Loading branch information
ishaansehgal99 authored Jul 20, 2024
1 parent 531207e commit 4fdecc2
Showing 1 changed file with 16 additions and 12 deletions.
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 @@ package controllers
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -40,7 +41,6 @@ import (
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 @@ func (c *WorkspaceReconciler) Reconcile(ctx context.Context, req reconcile.Reque

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

if err := c.ensureFinalizer(ctx, workspaceObj); err != nil {
return reconcile.Result{}, err
}
// 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 @@ func (c *WorkspaceReconciler) Reconcile(ctx context.Context, req reconcile.Reque
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
}
}
return nil
}

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

0 comments on commit 4fdecc2

Please sign in to comment.