|
39 | 39 | GetVMFromVcenter = cnsoperatorutil.GetVMFromVcenter
|
40 | 40 | )
|
41 | 41 |
|
| 42 | +const ( |
| 43 | + detachSuffix = ":detaching" |
| 44 | +) |
| 45 | + |
42 | 46 | // removeFinalizerFromCRDInstance will remove the CNS Finalizer, cns.vmware.com,
|
43 | 47 | // from a given nodevmbatchattachment instance.
|
44 | 48 | func removeFinalizerFromCRDInstance(ctx context.Context,
|
@@ -164,11 +168,36 @@ func getVolumesToDetachForVmFromVC(ctx context.Context,
|
164 | 168 | }
|
165 | 169 | log.Debugf("Obtained volumes to detach %+v for instance %s", pvcsToDetach, instance.Name)
|
166 | 170 |
|
| 171 | + updatePvcStatusEntryName(ctx, instance, pvcsToDetach) |
| 172 | + |
167 | 173 | // Ensure that there are no extra entries in instance status from a previous detach call.
|
168 | 174 | removeStaleEntriesFromInstanceStatus(ctx, instance, pvcsToDetach, volumeNamesInSpec)
|
169 | 175 | return pvcsToDetach, nil
|
170 | 176 | }
|
171 | 177 |
|
| 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 | + |
172 | 201 | // updateInstanceStatus updates the given nodevmbatchattachment instance's status.
|
173 | 202 | func updateInstanceStatus(ctx context.Context, cnsoperatorclient client.Client,
|
174 | 203 | instance *v1alpha1.CnsNodeVmBatchAttachment) error {
|
|
0 commit comments