@@ -50,6 +50,8 @@ import (
5050 csifault "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/fault"
5151 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/prometheus"
5252 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/utils"
53+ "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
54+ "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco"
5355 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
5456 k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
5557 cnsoptypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/types"
@@ -69,6 +71,7 @@ const (
6971var (
7072 backOffDuration map [k8stypes.NamespacedName ]time.Duration
7173 backOffDurationMapMutex = sync.Mutex {}
74+ isSharedDiskEnabled bool
7275)
7376
7477// Mockable function variables for testing
@@ -135,6 +138,9 @@ func Add(mgr manager.Manager, clusterFlavor cnstypes.CnsClusterFlavor,
135138 return err
136139 }
137140
141+ // If the capability gets enabled at a later point, then container will be restarted and this value will be reinitialized.
142+ isSharedDiskEnabled = commonco .ContainerOrchestratorUtility .IsFSSEnabled (ctx , common .SharedDiskFss )
143+
138144 recorder := eventBroadcaster .NewRecorder (scheme .Scheme , v1.EventSource {Component : cnsoperatorapis .GroupName })
139145 return add (mgr , newReconciler (mgr , configInfo , volumeManager , vmOperatorClient , recorder ))
140146}
@@ -347,6 +353,17 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
347353 }
348354 nodeUUID := instance .Spec .NodeUUID
349355 if ! instance .Status .Attached && instance .DeletionTimestamp == nil {
356+
357+ if isSharedDiskEnabled {
358+ // If isSharedDiskEnabled is enabled, then all new attach operations should happen via the
359+ // new CnsNodeVMBatchAttachment CRD.
360+ err := r .updateErrorOnInstanceToDisallowAttach (ctx , instance )
361+ if err != nil {
362+ return reconcile.Result {RequeueAfter : timeout }, csifault .CSIInternalFault , nil
363+ }
364+ return reconcile.Result {}, "" , nil
365+ }
366+
350367 nodeVM , err := getVMByUUIDFromVCenter (internalCtx , dc , nodeUUID )
351368 if err != nil {
352369 msg := fmt .Sprintf ("failed to find the VM with UUID: %q for CnsNodeVmAttachment " +
@@ -817,6 +834,24 @@ func isVmCrPresent(ctx context.Context, vmOperatorClient client.Client,
817834 return nil , nil
818835}
819836
837+ // updateErrorOnInstanceToDisallowAttach sets error on the CnsNodeVMAttachment CR so that devops user can detach this volume
838+ // from the VM and re-attach it so that the new Batch Attach flow is triggered.
839+ func (r * ReconcileCnsNodeVMAttachment ) updateErrorOnInstanceToDisallowAttach (ctx context.Context ,
840+ instance * v1a1.CnsNodeVmAttachment ) error {
841+ log := logger .GetLogger (ctx )
842+
843+ log .Infof ("Attach should happen via the new CnsNodeVMBatchAttachment CRD. Skipping attach." )
844+ msg := "CnsNodeVMAttachment CR is deprecated. Please detach this PVC from the VM and then reattach it."
845+ instance .Status .Error = msg
846+ err := k8s .UpdateStatus (ctx , r .client , instance )
847+ if err != nil {
848+ log .Errorf ("updateCnsNodeVMAttachment failed. err: %v" , err )
849+ return err
850+ }
851+ recordEvent (ctx , r , instance , v1 .EventTypeWarning , msg )
852+ return nil
853+ }
854+
820855// getVCDatacenterFromConfig returns datacenter registered for each vCenter
821856func getVCDatacentersFromConfig (cfg * config.Config ) (map [string ][]string , error ) {
822857 var err error
0 commit comments