Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete leaked volume if driver don't know the volume status #771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions *
}

if err := c.waitForVolume(ctx, volumeID); err != nil {
// To avoid leaking volume, we should delete the volume just created
// TODO: Need to figure out how to handle DeleteDisk failed scenario instead of just log the error
if _, error := c.DeleteDisk(ctx, volumeID); error != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So delete works if the volume is not available when we make the call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I did a test on my account. One concern here is if the deleteVolume call failed as well then we can not prevent the volume get leaked anyway..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense.

klog.Errorf("%v failed to be deleted, this may cause volume leak", volumeID)
} else {
klog.V(5).Infof("%v is deleted because it is not in desired state within retry limit", volumeID)
}
return nil, fmt.Errorf("failed to get an available volume in EC2: %v", err)
}

Expand Down
35 changes: 20 additions & 15 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ const (

func TestCreateDisk(t *testing.T) {
testCases := []struct {
name string
volumeName string
volState string
diskOptions *DiskOptions
expDisk *Disk
expErr error
expCreateVolumeErr error
expDescVolumeErr error
name string
volumeName string
volState string
diskOptions *DiskOptions
expDisk *Disk
cleanUpFailedVolume bool
expErr error
expCreateVolumeErr error
expDescVolumeErr error
}{
{
name: "success: normal",
Expand Down Expand Up @@ -163,7 +164,7 @@ func TestCreateDisk(t *testing.T) {
expErr: nil,
},
{
name: "fail: CreateVolume returned CreateVolume error",
name: "fail: ec2.CreateVolume returned CreateVolume error",
volumeName: "vol-test-name-error",
diskOptions: &DiskOptions{
CapacityBytes: util.GiBToBytes(1),
Expand All @@ -174,27 +175,29 @@ func TestCreateDisk(t *testing.T) {
expCreateVolumeErr: fmt.Errorf("CreateVolume generic error"),
},
{
name: "fail: CreateVolume returned a DescribeVolumes error",
name: "fail: ec2.DescribeVolumes error after volume created",
volumeName: "vol-test-name-error",
volState: "creating",
diskOptions: &DiskOptions{
CapacityBytes: util.GiBToBytes(1),
Tags: map[string]string{VolumeNameTagKey: "vol-test"},
AvailabilityZone: "",
},
expErr: fmt.Errorf("could not create volume in EC2: DescribeVolumes generic error"),
expCreateVolumeErr: fmt.Errorf("DescribeVolumes generic error"),
expErr: fmt.Errorf("failed to get an available volume in EC2: DescribeVolumes generic error"),
expDescVolumeErr: fmt.Errorf("DescribeVolumes generic error"),
cleanUpFailedVolume: true,
},
{
name: "fail: CreateVolume returned a volume with wrong state",
name: "fail: Volume is not ready to use, volume stuck in creating status and controller timed out waiting for the condition",
volumeName: "vol-test-name-error",
volState: "creating",
diskOptions: &DiskOptions{
CapacityBytes: util.GiBToBytes(1),
Tags: map[string]string{VolumeNameTagKey: "vol-test"},
AvailabilityZone: "",
},
expErr: fmt.Errorf("failed to get an available volume in EC2: timed out waiting for the condition"),
cleanUpFailedVolume: true,
expErr: fmt.Errorf("failed to get an available volume in EC2: timed out waiting for the condition"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test case above (DescribeVolume error ) we should also expect to clean up.

(the test case names are a bit confusing since CreateVolume could mean ec2.createvolume or csi.createvolume. If it helps and you dont mind could you disambiguate them in the test names)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

},
{
name: "success: normal from snapshot",
Expand Down Expand Up @@ -243,7 +246,9 @@ func TestCreateDisk(t *testing.T) {
if len(tc.diskOptions.SnapshotID) > 0 {
mockEC2.EXPECT().DescribeSnapshotsWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.DescribeSnapshotsOutput{Snapshots: []*ec2.Snapshot{snapshot}}, nil).AnyTimes()
}

if tc.cleanUpFailedVolume == true {
mockEC2.EXPECT().DeleteVolumeWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.DeleteVolumeOutput{}, nil)
}
if len(tc.diskOptions.AvailabilityZone) == 0 {
mockEC2.EXPECT().DescribeAvailabilityZonesWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.DescribeAvailabilityZonesOutput{
AvailabilityZones: []*ec2.AvailabilityZone{
Expand Down