@@ -132,7 +132,7 @@ func NewResizeController(
132
132
}
133
133
134
134
func (ctrl * resizeController ) addPVC (obj interface {}) {
135
- objKey , err := getObjectKey (obj )
135
+ objKey , err := util . GetObjectKey (obj )
136
136
if err != nil {
137
137
return
138
138
}
@@ -228,25 +228,13 @@ func (ctrl *resizeController) updatePVC(oldObj, newObj interface{}) {
228
228
}
229
229
230
230
func (ctrl * resizeController ) deletePVC (obj interface {}) {
231
- objKey , err := getObjectKey (obj )
231
+ objKey , err := util . GetObjectKey (obj )
232
232
if err != nil {
233
233
return
234
234
}
235
235
ctrl .claimQueue .Forget (objKey )
236
236
}
237
237
238
- func getObjectKey (obj interface {}) (string , error ) {
239
- if unknown , ok := obj .(cache.DeletedFinalStateUnknown ); ok && unknown .Obj != nil {
240
- obj = unknown .Obj
241
- }
242
- objKey , err := cache .DeletionHandlingMetaNamespaceKeyFunc (obj )
243
- if err != nil {
244
- klog .ErrorS (err , "Failed to get key from object" )
245
- return "" , err
246
- }
247
- return objKey , nil
248
- }
249
-
250
238
// Run starts the controller.
251
239
func (ctrl * resizeController ) Run (
252
240
workers int , ctx context.Context ) {
@@ -292,7 +280,7 @@ func (ctrl *resizeController) syncPVCs() {
292
280
293
281
// syncPVC checks if a pvc requests resizing, and execute the resize operation if requested.
294
282
func (ctrl * resizeController ) syncPVC (key string ) error {
295
- klog .V (4 ).InfoS ("Started PVC processing" , "key" , key )
283
+ klog .V (4 ).InfoS ("Started PVC processing for resize controller " , "key" , key )
296
284
297
285
namespace , name , err := cache .SplitMetaNamespaceKey (key )
298
286
if err != nil {
@@ -502,12 +490,16 @@ func (ctrl *resizeController) markPVCAsFSResizeRequired(pvc *v1.PersistentVolume
502
490
newPVC .Status .Conditions = util .MergeResizeConditionsOfPVC (newPVC .Status .Conditions ,
503
491
[]v1.PersistentVolumeClaimCondition {pvcCondition })
504
492
505
- _ , err := ctrl .patchClaim (pvc , newPVC , true /* addResourceVersionCheck */ )
506
-
493
+ updatedPVC , err := util .PatchClaim (ctrl .kubeClient , pvc , newPVC , true /* addResourceVersionCheck */ )
507
494
if err != nil {
508
495
return fmt .Errorf ("Mark PVC %q as file system resize required failed: %v" , klog .KObj (pvc ), err )
509
496
}
510
497
498
+ err = ctrl .claims .Update (updatedPVC )
499
+ if err != nil {
500
+ return fmt .Errorf ("error updating PVC %s in local cache: %v" , klog .KObj (newPVC ), err )
501
+ }
502
+
511
503
klog .V (4 ).InfoS ("Mark PVC as file system resize required" , "PVC" , klog .KObj (pvc ))
512
504
ctrl .eventRecorder .Eventf (pvc , v1 .EventTypeNormal ,
513
505
util .FileSystemResizeRequired , "Require file system resize of volume on node" )
@@ -527,7 +519,14 @@ func (ctrl *resizeController) markPVCResizeInProgress(pvc *v1.PersistentVolumeCl
527
519
newPVC .Status .Conditions = util .MergeResizeConditionsOfPVC (newPVC .Status .Conditions ,
528
520
[]v1.PersistentVolumeClaimCondition {progressCondition })
529
521
530
- updatedPVC , err := ctrl .patchClaim (pvc , newPVC , true /* addResourceVersionCheck */ )
522
+ updatedPVC , err := util .PatchClaim (ctrl .kubeClient , pvc , newPVC , true /* addResourceVersionCheck */ )
523
+ if err != nil {
524
+ return updatedPVC , fmt .Errorf ("Mark PVC %q as resize as in progress failed: %v" , klog .KObj (pvc ), err )
525
+ }
526
+ err = ctrl .claims .Update (updatedPVC )
527
+ if err != nil {
528
+ return updatedPVC , fmt .Errorf ("error updating PVC %s in local cache: %v" , klog .KObj (newPVC ), err )
529
+ }
531
530
return updatedPVC , err
532
531
}
533
532
@@ -538,38 +537,22 @@ func (ctrl *resizeController) markPVCResizeFinished(
538
537
newPVC .Status .Capacity [v1 .ResourceStorage ] = newSize
539
538
newPVC .Status .Conditions = util .MergeResizeConditionsOfPVC (pvc .Status .Conditions , []v1.PersistentVolumeClaimCondition {})
540
539
541
- _ , err := ctrl . patchClaim ( pvc , newPVC , true /* addResourceVersionCheck */ )
540
+ updatedPVC , err := util . PatchClaim ( ctrl . kubeClient , pvc , newPVC , true /* addResourceVersionCheck */ )
542
541
if err != nil {
543
542
return fmt .Errorf ("Mark PVC %q as resize finished failed: %v" , klog .KObj (pvc ), err )
544
543
}
545
544
545
+ err = ctrl .claims .Update (updatedPVC )
546
+ if err != nil {
547
+ return fmt .Errorf ("error updating PVC %s in local cache: %v" , klog .KObj (newPVC ), err )
548
+ }
549
+
546
550
klog .V (4 ).InfoS ("Resize PVC finished" , "PVC" , klog .KObj (pvc ))
547
551
ctrl .eventRecorder .Eventf (pvc , v1 .EventTypeNormal , util .VolumeResizeSuccess , "Resize volume succeeded" )
548
552
549
553
return nil
550
554
}
551
555
552
- // Patches a given PVC with changes from newPVC. If addResourceVersionCheck is true
553
- // then a version check is added to the patch to ensure that we are not patching
554
- // old(and possibly outdated) PVC objects.
555
- func (ctrl * resizeController ) patchClaim (oldPVC , newPVC * v1.PersistentVolumeClaim , addResourceVersionCheck bool ) (* v1.PersistentVolumeClaim , error ) {
556
- patchBytes , err := util .GetPVCPatchData (oldPVC , newPVC , addResourceVersionCheck )
557
- if err != nil {
558
- return oldPVC , fmt .Errorf ("can't patch status of PVC %s as generate path data failed: %v" , klog .KObj (oldPVC ), err )
559
- }
560
- updatedClaim , updateErr := ctrl .kubeClient .CoreV1 ().PersistentVolumeClaims (oldPVC .Namespace ).
561
- Patch (context .TODO (), oldPVC .Name , types .StrategicMergePatchType , patchBytes , metav1.PatchOptions {}, "status" )
562
- if updateErr != nil {
563
- return oldPVC , fmt .Errorf ("can't patch status of PVC %s with %v" , klog .KObj (oldPVC ), updateErr )
564
- }
565
- err = ctrl .claims .Update (updatedClaim )
566
- if err != nil {
567
- return oldPVC , fmt .Errorf ("error updating PVC %s in local cache: %v" , klog .KObj (newPVC ), err )
568
- }
569
-
570
- return updatedClaim , nil
571
- }
572
-
573
556
func (ctrl * resizeController ) deletePreResizeCapAnnotation (pv * v1.PersistentVolume ) error {
574
557
// if the pv does not have a resize annotation skip the entire process
575
558
if ! metav1 .HasAnnotation (pv .ObjectMeta , util .AnnPreResizeCapacity ) {
@@ -608,22 +591,6 @@ func (ctrl *resizeController) updatePVCapacity(
608
591
return updatedPV , nil
609
592
}
610
593
611
- func (ctrl * resizeController ) patchPersistentVolume (oldPV , newPV * v1.PersistentVolume ) (* v1.PersistentVolume , error ) {
612
- patchBytes , err := util .GetPatchData (oldPV , newPV )
613
- if err != nil {
614
- return nil , fmt .Errorf ("can't update capacity of PV %s as generate path data failed: %v" , newPV .Name , err )
615
- }
616
- updatedPV , updateErr := ctrl .kubeClient .CoreV1 ().PersistentVolumes ().Patch (context .TODO (), newPV .Name , types .StrategicMergePatchType , patchBytes , metav1.PatchOptions {})
617
- if updateErr != nil {
618
- return nil , fmt .Errorf ("update capacity of PV %s failed: %v" , newPV .Name , updateErr )
619
- }
620
- err = ctrl .volumes .Update (updatedPV )
621
- if err != nil {
622
- return nil , fmt .Errorf ("error updating PV %s in local cache: %v" , newPV .Name , err )
623
- }
624
- return updatedPV , nil
625
- }
626
-
627
594
func parsePod (obj interface {}) * v1.Pod {
628
595
if obj == nil {
629
596
return nil
@@ -644,6 +611,22 @@ func parsePod(obj interface{}) *v1.Pod {
644
611
return pod
645
612
}
646
613
614
+ func (ctrl * resizeController ) patchPersistentVolume (oldPV , newPV * v1.PersistentVolume ) (* v1.PersistentVolume , error ) {
615
+ patchBytes , err := util .GetPatchData (oldPV , newPV )
616
+ if err != nil {
617
+ return nil , fmt .Errorf ("can't update capacity of PV %s as generate path data failed: %v" , newPV .Name , err )
618
+ }
619
+ updatedPV , updateErr := ctrl .kubeClient .CoreV1 ().PersistentVolumes ().Patch (context .TODO (), newPV .Name , types .StrategicMergePatchType , patchBytes , metav1.PatchOptions {})
620
+ if updateErr != nil {
621
+ return nil , fmt .Errorf ("update capacity of PV %s failed: %v" , newPV .Name , updateErr )
622
+ }
623
+ err = ctrl .volumes .Update (updatedPV )
624
+ if err != nil {
625
+ return nil , fmt .Errorf ("error updating PV %s in local cache: %v" , newPV .Name , err )
626
+ }
627
+ return updatedPV , nil
628
+ }
629
+
647
630
func inUseError (err error ) bool {
648
631
st , ok := status .FromError (err )
649
632
if ! ok {
0 commit comments