Skip to content

Commit 0c95ee2

Browse files
authored
Ensure that during detach PVC has a unique entry in volumeStatus (#3630)
1 parent 743e814 commit 0c95ee2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pkg/syncer/cnsoperator/controller/cnsnodevmbatchattachment/cnsnodevmbatchattachment_helper.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ var (
3939
GetVMFromVcenter = cnsoperatorutil.GetVMFromVcenter
4040
)
4141

42+
const (
43+
detachSuffix = ":detaching"
44+
)
45+
4246
// removeFinalizerFromCRDInstance will remove the CNS Finalizer, cns.vmware.com,
4347
// from a given nodevmbatchattachment instance.
4448
func removeFinalizerFromCRDInstance(ctx context.Context,
@@ -164,11 +168,36 @@ func getVolumesToDetachForVmFromVC(ctx context.Context,
164168
}
165169
log.Debugf("Obtained volumes to detach %+v for instance %s", pvcsToDetach, instance.Name)
166170

171+
updatePvcStatusEntryName(ctx, instance, pvcsToDetach)
172+
167173
// Ensure that there are no extra entries in instance status from a previous detach call.
168174
removeStaleEntriesFromInstanceStatus(ctx, instance, pvcsToDetach, volumeNamesInSpec)
169175
return pvcsToDetach, nil
170176
}
171177

178+
// updatePvcStatusEntryName goes through each of the PVCs to detach and updates their
179+
// status to have the suffix ":detaching".
180+
// This is required to avoid the case where disk-1 was associated with pvc-1 and got attached.
181+
// disk-1 is then associated with pvc-2.
182+
// This means, PVC-1 should get detached and PVC-2 should get attached to the VM.
183+
// But they both have the same entry in the status which is wrong. By adding the suffix,
184+
// the volume name entry for the PVC getting detached becomes unique.
185+
func updatePvcStatusEntryName(ctx context.Context,
186+
instance *v1alpha1.CnsNodeVmBatchAttachment, pvcsToDetach map[string]string) {
187+
log := logger.GetLogger(ctx)
188+
189+
for i, volume := range instance.Status.VolumeStatus {
190+
if _, ok := pvcsToDetach[volume.PersistentVolumeClaim.ClaimName]; !ok {
191+
continue
192+
}
193+
newVolumeName := instance.Status.VolumeStatus[i].Name + detachSuffix
194+
instance.Status.VolumeStatus[i].Name = newVolumeName
195+
log.Infof("Updating status name entry to %s for detaching PVC %s",
196+
newVolumeName,
197+
volume.PersistentVolumeClaim.ClaimName)
198+
}
199+
}
200+
172201
// updateInstanceStatus updates the given nodevmbatchattachment instance's status.
173202
func updateInstanceStatus(ctx context.Context, cnsoperatorclient client.Client,
174203
instance *v1alpha1.CnsNodeVmBatchAttachment) error {

0 commit comments

Comments
 (0)