@@ -164,6 +164,8 @@ var (
164
164
removeFinalizerFromPVC = k8s .RemoveFinalizerFromPVC
165
165
)
166
166
167
+ // TODO: find a way to optimise the logger usage in this file.
168
+
167
169
// Reconcile reads that state of the cluster for a Reconciler object
168
170
// and makes changes based on the state read and what is in the
169
171
// Reconciler.Spec.
@@ -194,6 +196,9 @@ func (r *Reconciler) Reconcile(ctx context.Context,
194
196
log .Info ("finished reconciling instance" )
195
197
}()
196
198
199
+ // Handle deletion of the instance.
200
+ // The finalizer will be removed only if the volume is unregistered
201
+ // or if the input parameters are invalid or if the volume is in use.
197
202
if instance .DeletionTimestamp != nil {
198
203
log .Info ("instance is marked for deletion" )
199
204
err = r .reconcileDelete (ctx , * instance , request )
@@ -220,12 +225,6 @@ func (r *Reconciler) Reconcile(ctx context.Context,
220
225
return reconcile.Result {}, nil
221
226
}
222
227
223
- err = protectInstance (ctx , r .client , instance )
224
- if err != nil {
225
- log .Error ("failed to protect instance with error " , err )
226
- return reconcile.Result {}, err
227
- }
228
-
229
228
err = r .reconcile (ctx , instance , request )
230
229
if err != nil {
231
230
log .Error ("failed to reconcile with error " , err )
@@ -256,6 +255,14 @@ func (r *Reconciler) reconcile(ctx context.Context,
256
255
return err
257
256
}
258
257
258
+ // Only protect the instance if input parameters are valid.
259
+ // This ensures faster deletion of instances with invalid parameters.
260
+ err = protectInstance (ctx , r .client , instance )
261
+ if err != nil {
262
+ log .Error ("failed to protect instance with error " , err )
263
+ return err
264
+ }
265
+
259
266
usageInfo , err := getVolumeUsageInfo (ctx , params .pvcName , params .namespace ,
260
267
instance .Spec .ForceUnregister )
261
268
if err != nil {
@@ -269,7 +276,7 @@ func (r *Reconciler) reconcile(ctx context.Context,
269
276
return errors .New (msg )
270
277
}
271
278
272
- err = unregisterVolume (ctx , * r , request , * params )
279
+ err = unregisterVolume (ctx , r . volumeManager , request , * params )
273
280
if err != nil {
274
281
log .Error ("failed to unregister volume with error " , err )
275
282
return err
@@ -315,7 +322,11 @@ func (r *Reconciler) reconcileDelete(ctx context.Context,
315
322
return nil
316
323
}
317
324
318
- err = unregisterVolume (ctx , * r , request , * params )
325
+ // Try to unregister the volume. This ensures that the system remains
326
+ // consistent and the volume is not left in an unusable state.
327
+ // If unregistration fails, the instance will be re-queued for
328
+ // reconciliation.
329
+ err = unregisterVolume (ctx , r .volumeManager , request , * params )
319
330
if err != nil {
320
331
log .Error ("failed to unregister volume with error " , err )
321
332
return err
@@ -325,7 +336,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context,
325
336
return nil
326
337
}
327
338
328
- func unregisterVolume (ctx context.Context , r Reconciler ,
339
+ func unregisterVolume (ctx context.Context , volMgr volumes. Manager ,
329
340
request reconcile.Request , params params ) error {
330
341
log := logger .GetLogger (ctx ).With ("name" , request .NamespacedName )
331
342
@@ -355,7 +366,7 @@ func unregisterVolume(ctx context.Context, r Reconciler,
355
366
}
356
367
357
368
unregDisk := ! params .retainFCD // If retainFCD is false, unregister the FCD too.
358
- err = r . volumeManager .UnregisterVolume (ctx , params .volumeID , unregDisk )
369
+ err = volMgr .UnregisterVolume (ctx , params .volumeID , unregDisk )
359
370
if err != nil {
360
371
log .Error ("failed to unregister volume with error " , err )
361
372
return errors .New ("failed to unregister volume" )
0 commit comments