Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit bfc72ae

Browse files
authored
Merge pull request #939 from wongma7/api-calls
Avoid double deletion attempts by accounting for (newish) pv protection feature
2 parents 31e9872 + 093ae2f commit bfc72ae

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/controller/controller.go

+8
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,14 @@ func (ctrl *ProvisionController) shouldProvision(claim *v1.PersistentVolumeClaim
870870
// shouldDelete returns whether a volume should have its backing volume
871871
// deleted, i.e. whether a Delete is "desired"
872872
func (ctrl *ProvisionController) shouldDelete(volume *v1.PersistentVolume) bool {
873+
// In 1.9+ PV protection means the object will exist briefly with a
874+
// deletion timestamp even after our successful Delete. Ignore it.
875+
if ctrl.kubeVersion.AtLeast(utilversion.MustParseSemantic("v1.9.0")) {
876+
if volume.ObjectMeta.DeletionTimestamp != nil {
877+
return false
878+
}
879+
}
880+
873881
// In 1.5+ we delete only if the volume is in state Released. In 1.4 we must
874882
// delete if the volume is in state Failed too.
875883
if ctrl.kubeVersion.AtLeast(utilversion.MustParseSemantic("v1.5.0")) {

lib/controller/controller_test.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,14 @@ func TestShouldProvision(t *testing.T) {
455455
}
456456

457457
func TestShouldDelete(t *testing.T) {
458+
timestamp := metav1.NewTime(time.Now())
458459
tests := []struct {
459-
name string
460-
provisionerName string
461-
volume *v1.PersistentVolume
462-
serverGitVersion string
463-
expectedShould bool
460+
name string
461+
provisionerName string
462+
volume *v1.PersistentVolume
463+
deletionTimestamp *metav1.Time
464+
serverGitVersion string
465+
expectedShould bool
464466
}{
465467
{
466468
name: "should delete",
@@ -504,11 +506,27 @@ func TestShouldDelete(t *testing.T) {
504506
serverGitVersion: "v1.5.0",
505507
expectedShould: false,
506508
},
509+
{
510+
name: "1.9 non-nil deletion timestamp",
511+
provisionerName: "foo.bar/baz",
512+
volume: newVolume("volume-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, map[string]string{annDynamicallyProvisioned: "foo.bar/baz"}),
513+
deletionTimestamp: &timestamp,
514+
serverGitVersion: "v1.9.0",
515+
expectedShould: false,
516+
},
517+
{
518+
name: "1.9 nil deletion timestamp",
519+
provisionerName: "foo.bar/baz",
520+
volume: newVolume("volume-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, map[string]string{annDynamicallyProvisioned: "foo.bar/baz"}),
521+
serverGitVersion: "v1.9.0",
522+
expectedShould: true,
523+
},
507524
}
508525
for _, test := range tests {
509526
client := fake.NewSimpleClientset()
510527
provisioner := newTestProvisioner()
511528
ctrl := newTestProvisionController(client, test.provisionerName, provisioner, test.serverGitVersion)
529+
test.volume.ObjectMeta.DeletionTimestamp = test.deletionTimestamp
512530

513531
should := ctrl.shouldDelete(test.volume)
514532
if test.expectedShould != should {

0 commit comments

Comments
 (0)