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

Commit

Permalink
Merge pull request #939 from wongma7/api-calls
Browse files Browse the repository at this point in the history
Avoid double deletion attempts by accounting for (newish) pv protection feature
  • Loading branch information
wongma7 authored Aug 17, 2018
2 parents 31e9872 + 093ae2f commit bfc72ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,14 @@ func (ctrl *ProvisionController) shouldProvision(claim *v1.PersistentVolumeClaim
// shouldDelete returns whether a volume should have its backing volume
// deleted, i.e. whether a Delete is "desired"
func (ctrl *ProvisionController) shouldDelete(volume *v1.PersistentVolume) bool {
// In 1.9+ PV protection means the object will exist briefly with a
// deletion timestamp even after our successful Delete. Ignore it.
if ctrl.kubeVersion.AtLeast(utilversion.MustParseSemantic("v1.9.0")) {
if volume.ObjectMeta.DeletionTimestamp != nil {
return false
}
}

// In 1.5+ we delete only if the volume is in state Released. In 1.4 we must
// delete if the volume is in state Failed too.
if ctrl.kubeVersion.AtLeast(utilversion.MustParseSemantic("v1.5.0")) {
Expand Down
28 changes: 23 additions & 5 deletions lib/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,14 @@ func TestShouldProvision(t *testing.T) {
}

func TestShouldDelete(t *testing.T) {
timestamp := metav1.NewTime(time.Now())
tests := []struct {
name string
provisionerName string
volume *v1.PersistentVolume
serverGitVersion string
expectedShould bool
name string
provisionerName string
volume *v1.PersistentVolume
deletionTimestamp *metav1.Time
serverGitVersion string
expectedShould bool
}{
{
name: "should delete",
Expand Down Expand Up @@ -504,11 +506,27 @@ func TestShouldDelete(t *testing.T) {
serverGitVersion: "v1.5.0",
expectedShould: false,
},
{
name: "1.9 non-nil deletion timestamp",
provisionerName: "foo.bar/baz",
volume: newVolume("volume-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, map[string]string{annDynamicallyProvisioned: "foo.bar/baz"}),
deletionTimestamp: &timestamp,
serverGitVersion: "v1.9.0",
expectedShould: false,
},
{
name: "1.9 nil deletion timestamp",
provisionerName: "foo.bar/baz",
volume: newVolume("volume-1", v1.VolumeReleased, v1.PersistentVolumeReclaimDelete, map[string]string{annDynamicallyProvisioned: "foo.bar/baz"}),
serverGitVersion: "v1.9.0",
expectedShould: true,
},
}
for _, test := range tests {
client := fake.NewSimpleClientset()
provisioner := newTestProvisioner()
ctrl := newTestProvisionController(client, test.provisionerName, provisioner, test.serverGitVersion)
test.volume.ObjectMeta.DeletionTimestamp = test.deletionTimestamp

should := ctrl.shouldDelete(test.volume)
if test.expectedShould != should {
Expand Down

0 comments on commit bfc72ae

Please sign in to comment.