Skip to content

Commit cd53088

Browse files
ahg-gcoolkp
authored andcommitted
Delete InferenceModels from the datastore when deletionTimestamp is set (kubernetes-sigs#319)
* Delete InferenceModels from the datastore when deletionTimestamp is set * Update pkg/ext-proc/backend/inferencemodel_reconciler_test.go
1 parent ee58f89 commit cd53088

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/ext-proc/backend/inferencemodel_reconciler.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (c *InferenceModelReconciler) Reconcile(ctx context.Context, req ctrl.Reque
3737
}
3838
klog.Error(err, "Unable to get InferenceModel")
3939
return ctrl.Result{}, err
40+
} else if !infModel.DeletionTimestamp.IsZero() {
41+
klog.V(1).Infof("InferenceModel %v is marked for deletion. Removing from datastore", req.NamespacedName)
42+
c.Datastore.InferenceModels.Delete(infModel.Spec.ModelName)
43+
return ctrl.Result{}, nil
4044
}
4145

4246
c.updateDatastore(infModel)

pkg/ext-proc/backend/inferencemodel_reconciler_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,66 @@ func TestReconcile_ResourceNotFound(t *testing.T) {
184184
}
185185
}
186186

187+
func TestReconcile_ModelMarkedForDeletion(t *testing.T) {
188+
// Set up the scheme.
189+
scheme := runtime.NewScheme()
190+
_ = v1alpha1.AddToScheme(scheme)
191+
192+
// Create an InferenceModel object.
193+
now := metav1.Now()
194+
existingModel := &v1alpha1.InferenceModel{
195+
ObjectMeta: metav1.ObjectMeta{
196+
Name: "existing-model",
197+
Namespace: "default",
198+
DeletionTimestamp: &now,
199+
Finalizers: []string{"finalizer"},
200+
},
201+
Spec: v1alpha1.InferenceModelSpec{
202+
ModelName: "fake-model",
203+
PoolRef: v1alpha1.PoolObjectReference{Name: "test-pool"},
204+
},
205+
}
206+
207+
// Create a fake client with the existing model.
208+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(existingModel).Build()
209+
210+
// Create a minimal datastore.
211+
datastore := &K8sDatastore{
212+
InferenceModels: &sync.Map{},
213+
inferencePool: &v1alpha1.InferencePool{
214+
ObjectMeta: metav1.ObjectMeta{Name: "test-pool"},
215+
},
216+
}
217+
218+
// Create the reconciler.
219+
reconciler := &InferenceModelReconciler{
220+
Client: fakeClient,
221+
Scheme: scheme,
222+
Record: record.NewFakeRecorder(10),
223+
Datastore: datastore,
224+
PoolNamespacedName: types.NamespacedName{Name: "test-pool", Namespace: "default"},
225+
}
226+
227+
// Create a request for the existing resource.
228+
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "existing-model", Namespace: "default"}}
229+
230+
// Call Reconcile.
231+
result, err := reconciler.Reconcile(context.Background(), req)
232+
if err != nil {
233+
t.Fatalf("expected no error when resource exists, got %v", err)
234+
}
235+
236+
// Check that no requeue is requested.
237+
if result.Requeue || result.RequeueAfter != 0 {
238+
t.Errorf("expected no requeue, got %+v", result)
239+
}
240+
241+
// Verify that the datastore was not updated.
242+
if _, ok := datastore.InferenceModels.Load(existingModel.Spec.ModelName); ok {
243+
t.Errorf("expected datastore to not contain model %q", existingModel.Spec.ModelName)
244+
}
245+
}
246+
187247
func TestReconcile_ResourceExists(t *testing.T) {
188248
// Set up the scheme.
189249
scheme := runtime.NewScheme()

0 commit comments

Comments
 (0)