@@ -45,7 +45,7 @@ import (
45
45
"sigs.k8s.io/controller-runtime/pkg/reconcile"
46
46
"sigs.k8s.io/controller-runtime/pkg/source"
47
47
cnsoperatorapis "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator"
48
- cnsfileaccessconfigv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsfileaccessconfig/v1alpha1"
48
+ v1a1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsfileaccessconfig/v1alpha1"
49
49
volumes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/volume"
50
50
commonconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
51
51
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
@@ -191,8 +191,8 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
191
191
// Watch for changes to primary resource CnsFileAccessConfig.
192
192
err = c .Watch (source .Kind (
193
193
mgr .GetCache (),
194
- & cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig {},
195
- & handler.TypedEnqueueRequestForObject [* cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig ]{},
194
+ & v1a1 .CnsFileAccessConfig {},
195
+ & handler.TypedEnqueueRequestForObject [* v1a1 .CnsFileAccessConfig ]{},
196
196
))
197
197
if err != nil {
198
198
log .Errorf ("Failed to watch for changes to CnsFileAccessConfig resource with error: %+v" , err )
@@ -228,7 +228,7 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
228
228
request reconcile.Request ) (reconcile.Result , error ) {
229
229
log := logger .GetLogger (ctx )
230
230
// Fetch the CnsFileAccessConfig instance.
231
- instance := & cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig {}
231
+ instance := & v1a1 .CnsFileAccessConfig {}
232
232
err := r .client .Get (ctx , request .NamespacedName , instance )
233
233
if err != nil {
234
234
if apierrors .IsNotFound (err ) {
@@ -297,11 +297,10 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
297
297
}
298
298
299
299
// Remove finalizer from CnsFileAccessConfig CRD
300
- removeFinalizerFromCRDInstance (ctx , instance )
301
- err = updateCnsFileAccessConfig (ctx , r .client , instance )
300
+ err = k8s .RemoveFinalizer (ctx , r .client , instance , cnsoperatortypes .CNSFinalizer )
302
301
if err != nil {
303
- msg := fmt .Sprintf ("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v" ,
304
- instance .Name , instance .Namespace , err )
302
+ msg := fmt .Sprintf ("failed to remove finalizer from CnsFileAccessConfig " +
303
+ "instance: %q on namespace: %q. Error: %+v" , instance .Name , instance .Namespace , err )
305
304
recordEvent (ctx , r , instance , v1 .EventTypeWarning , msg )
306
305
return reconcile.Result {RequeueAfter : timeout }, nil
307
306
}
@@ -370,8 +369,7 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
370
369
return reconcile.Result {RequeueAfter : timeout }, nil
371
370
}
372
371
}
373
- removeFinalizerFromCRDInstance (ctx , instance )
374
- err = updateCnsFileAccessConfig (ctx , r .client , instance )
372
+ err = k8s .RemoveFinalizer (ctx , r .client , instance , cnsoperatortypes .CNSFinalizer )
375
373
if err != nil {
376
374
msg := fmt .Sprintf ("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v" ,
377
375
instance .Name , instance .Namespace , err )
@@ -396,24 +394,14 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
396
394
backOffDurationMapMutex .Unlock ()
397
395
return reconcile.Result {}, nil
398
396
}
399
- cnsFinalizerExists := false
400
- // Check if finalizer already exists.
401
- for _ , finalizer := range instance .Finalizers {
402
- if finalizer == cnsoperatortypes .CNSFinalizer {
403
- cnsFinalizerExists = true
404
- break
405
- }
406
- }
407
- if ! cnsFinalizerExists {
408
- // Add finalizer.
409
- instance .Finalizers = append (instance .Finalizers , cnsoperatortypes .CNSFinalizer )
410
- err = updateCnsFileAccessConfig (ctx , r .client , instance )
411
- if err != nil {
412
- msg := fmt .Sprintf ("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v" ,
413
- instance .Name , instance .Namespace , err )
414
- recordEvent (ctx , r , instance , v1 .EventTypeWarning , msg )
415
- return reconcile.Result {RequeueAfter : timeout }, nil
416
- }
397
+
398
+ // Add finalizer to CnsFileAccessConfig instance if it does not already exist.
399
+ err = k8s .AddFinalizer (ctx , r .client , instance , cnsoperatortypes .CNSFinalizer )
400
+ if err != nil {
401
+ msg := fmt .Sprintf ("failed to add finalizer on CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v" ,
402
+ instance .Name , instance .Namespace , err )
403
+ recordEvent (ctx , r , instance , v1 .EventTypeWarning , msg )
404
+ return reconcile.Result {RequeueAfter : timeout }, nil
417
405
}
418
406
419
407
vmOwnerRefExists := false
@@ -528,7 +516,7 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
528
516
// addPvcFinalizer checks if CnsPvcFinalizer exists on PVC.
529
517
// If it does not exist, it updates the PVC with it.
530
518
func addPvcFinalizer (ctx context.Context ,
531
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig , client client.Client ) error {
519
+ instance * v1a1 .CnsFileAccessConfig , client client.Client ) error {
532
520
log := logger .GetLogger (ctx )
533
521
534
522
if ! commonco .ContainerOrchestratorUtility .IsFSSEnabled (ctx , common .FileVolumesWithVmService ) {
@@ -566,7 +554,7 @@ func addPvcFinalizer(ctx context.Context,
566
554
567
555
// isPvcInUse returns true if there is at least 1 VM which is using the given PVC.
568
556
func isPvcInUse (ctx context.Context , pvcName string ,
569
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig ) (bool , error ) {
557
+ instance * v1a1 .CnsFileAccessConfig ) (bool , error ) {
570
558
log := logger .GetLogger (ctx )
571
559
572
560
cnsFileVolumeClientInstance , err := cnsfilevolumeclient .GetFileVolumeClientInstance (ctx )
@@ -581,7 +569,7 @@ func isPvcInUse(ctx context.Context, pvcName string,
581
569
// removeFinalizerFromPVC will remove the CNS Finalizer, cns.vmware.com/pvc-protection,
582
570
// from a given PersistentVolumeClaim.
583
571
func removeFinalizerFromPVC (ctx context.Context , client client.Client ,
584
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig ) error {
572
+ instance * v1a1 .CnsFileAccessConfig ) error {
585
573
log := logger .GetLogger (ctx )
586
574
587
575
if ! commonco .ContainerOrchestratorUtility .IsFSSEnabled (ctx , common .FileVolumesWithVmService ) {
@@ -640,7 +628,7 @@ func removeFinalizerFromPVC(ctx context.Context, client client.Client,
640
628
// This method is used when we don't have VM instance. It fetches the VM IP from CNSFileVolumeClient
641
629
// instance for the VM name associated with CnsFileAccessConfig.
642
630
func (r * ReconcileCnsFileAccessConfig ) removePermissionsForFileVolume (ctx context.Context , volumeID string ,
643
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig , skipConfigureVolumeACL bool ) error {
631
+ instance * v1a1 .CnsFileAccessConfig , skipConfigureVolumeACL bool ) error {
644
632
log := logger .GetLogger (ctx )
645
633
volumePermissionLock , _ := volumePermissionLockMap .LoadOrStore (volumeID , & sync.Mutex {})
646
634
instanceLock , _ := volumePermissionLock .(* sync.Mutex )
@@ -685,7 +673,7 @@ func (r *ReconcileCnsFileAccessConfig) removePermissionsForFileVolume(ctx contex
685
673
// permissions by setting the parameter removePermission to true or false
686
674
// respectively. Returns error if any operation fails.
687
675
func (r * ReconcileCnsFileAccessConfig ) configureNetPermissionsForFileVolume (ctx context.Context ,
688
- volumeID string , vm * vmoperatortypes.VirtualMachine , instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig ,
676
+ volumeID string , vm * vmoperatortypes.VirtualMachine , instance * v1a1 .CnsFileAccessConfig ,
689
677
removePermission bool ) error {
690
678
log := logger .GetLogger (ctx )
691
679
volumePermissionLock , _ := volumePermissionLockMap .LoadOrStore (volumeID , & sync.Mutex {})
@@ -865,10 +853,10 @@ func validateVmAndPvc(ctx context.Context, instanceLabels map[string]string, ins
865
853
// setInstanceSuccess sets instance to success and records an event on the
866
854
// CnsFileAccessConfig instance.
867
855
func setInstanceSuccess (ctx context.Context , r * ReconcileCnsFileAccessConfig ,
868
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig , msg string ) error {
856
+ instance * v1a1 .CnsFileAccessConfig , msg string ) error {
869
857
instance .Status .Done = true
870
858
instance .Status .Error = ""
871
- err := updateCnsFileAccessConfig (ctx , r .client , instance )
859
+ err := k8s . UpdateStatus (ctx , r .client , instance )
872
860
if err != nil {
873
861
return err
874
862
}
@@ -879,18 +867,18 @@ func setInstanceSuccess(ctx context.Context, r *ReconcileCnsFileAccessConfig,
879
867
// setInstanceError sets error and records an event on the CnsFileAccessConfig
880
868
// instance.
881
869
func setInstanceError (ctx context.Context , r * ReconcileCnsFileAccessConfig ,
882
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig , errMsg string ) {
870
+ instance * v1a1 .CnsFileAccessConfig , errMsg string ) {
883
871
log := logger .GetLogger (ctx )
884
872
instance .Status .Error = errMsg
885
- err := updateCnsFileAccessConfig (ctx , r .client , instance )
873
+ err := k8s . UpdateStatus (ctx , r .client , instance )
886
874
if err != nil {
887
875
log .Errorf ("updateCnsFileAccessConfig failed. err: %v" , err )
888
876
}
889
877
recordEvent (ctx , r , instance , v1 .EventTypeWarning , errMsg )
890
878
}
891
879
892
880
func updateCnsFileAccessConfig (ctx context.Context , client client.Client ,
893
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig ) error {
881
+ instance * v1a1 .CnsFileAccessConfig ) error {
894
882
log := logger .GetLogger (ctx )
895
883
err := client .Update (ctx , instance )
896
884
if err != nil {
@@ -904,7 +892,7 @@ func updateCnsFileAccessConfig(ctx context.Context, client client.Client,
904
892
// appropriately and logs the message.
905
893
// backOffDuration is reset to 1 second on success and doubled on failure.
906
894
func recordEvent (ctx context.Context , r * ReconcileCnsFileAccessConfig ,
907
- instance * cnsfileaccessconfigv1alpha1 .CnsFileAccessConfig , eventtype string , msg string ) {
895
+ instance * v1a1 .CnsFileAccessConfig , eventtype string , msg string ) {
908
896
log := logger .GetLogger (ctx )
909
897
log .Debugf ("Event type is %s" , eventtype )
910
898
namespacedName := types.NamespacedName {
@@ -927,17 +915,3 @@ func recordEvent(ctx context.Context, r *ReconcileCnsFileAccessConfig,
927
915
backOffDurationMapMutex .Unlock ()
928
916
}
929
917
}
930
-
931
- // removeFinalizerFromCRDInstance will remove the CNS Finalizer = cns.vmware.com,
932
- // from a given CnsFileAccessConfig instance.
933
- func removeFinalizerFromCRDInstance (ctx context.Context , instance * cnsfileaccessconfigv1alpha1.CnsFileAccessConfig ) {
934
- log := logger .GetLogger (ctx )
935
- for i , finalizer := range instance .Finalizers {
936
- if finalizer == cnsoperatortypes .CNSFinalizer {
937
- log .Debugf ("Removing %q finalizer from CnsFileAccessConfig instance with name: %q on namespace: %q" ,
938
- cnsoperatortypes .CNSFinalizer , instance .Name , instance .Namespace )
939
- instance .Finalizers = append (instance .Finalizers [:i ], instance .Finalizers [i + 1 :]... )
940
- break
941
- }
942
- }
943
- }
0 commit comments