Skip to content

Commit da9f47e

Browse files
authored
Merge pull request kubernetes#88146 from gnufied/avoid-multiple-pv-delete
Prevent deletion of PVs that are already deleted
2 parents d4c5637 + d9f7a1f commit da9f47e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

pkg/controller/volume/persistentvolume/framework_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ func newVolumeArray(name, capacity, boundToClaimUID, boundToClaimName string, ph
348348
}
349349
}
350350

351+
func withVolumeDeletionTimestamp(pvs []*v1.PersistentVolume) []*v1.PersistentVolume {
352+
result := []*v1.PersistentVolume{}
353+
for _, pv := range pvs {
354+
// Using time.Now() here will cause mismatching deletion timestamps in tests
355+
deleteTime := metav1.Date(2020, time.February, 18, 10, 30, 30, 10, time.UTC)
356+
pv.SetDeletionTimestamp(&deleteTime)
357+
result = append(result, pv)
358+
}
359+
return result
360+
}
361+
351362
// newClaim returns a new claim with given attributes
352363
func newClaim(name, claimUID, capacity, boundToVolume string, phase v1.PersistentVolumeClaimPhase, class *string, annotations ...string) *v1.PersistentVolumeClaim {
353364
fs := v1.PersistentVolumeFilesystem

pkg/controller/volume/persistentvolume/pv_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,11 @@ func (ctrl *PersistentVolumeController) deleteVolumeOperation(volume *v1.Persist
11831183
klog.V(3).Infof("error reading persistent volume %q: %v", volume.Name, err)
11841184
return "", nil
11851185
}
1186+
1187+
if newVolume.GetDeletionTimestamp() != nil {
1188+
klog.V(3).Infof("Volume %q is already being deleted", volume.Name)
1189+
return "", nil
1190+
}
11861191
needsReclaim, err := ctrl.isVolumeReleased(newVolume)
11871192
if err != nil {
11881193
klog.V(3).Infof("error reading claim for volume %q: %v", volume.Name, err)

pkg/controller/volume/persistentvolume/pv_controller_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ func TestControllerSync(t *testing.T) {
233233
return nil
234234
},
235235
},
236+
{
237+
// delete success(?) - volume has deletion timestamp before doDelete() starts
238+
"8-13 - volume is has deletion timestamp and is not processed",
239+
withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeBound, v1.PersistentVolumeReclaimDelete, classEmpty)),
240+
withVolumeDeletionTimestamp(newVolumeArray("volume8-13", "1Gi", "uid8-13", "claim8-13", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, classEmpty)),
241+
noclaims,
242+
noclaims,
243+
noevents, noerrors,
244+
// We don't need to do anything in test function because deletion will be noticed automatically and synced.
245+
// Attempting to use testSyncVolume here will cause an error because of race condition between manually
246+
// calling testSyncVolume and volume loop running.
247+
func(ctrl *PersistentVolumeController, reactor *pvtesting.VolumeReactor, test controllerTest) error {
248+
return nil
249+
},
250+
},
236251
}
237252

238253
for _, test := range tests {

0 commit comments

Comments
 (0)