From 125ec1bc195d5fe313866a7a3648934be97f6a0c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?=
 <lukas.kaemmerling@hetzner-cloud.de>
Date: Wed, 10 Jun 2020 07:10:32 +0200
Subject: [PATCH] Fix Detach deleted volume should not return NOT FOUND (#123)

---
 CHANGES.md                | 4 ++++
 api/volume.go             | 8 ++++++++
 driver/controller.go      | 5 +++--
 driver/controller_test.go | 5 -----
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index e526e35..c396e6a 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,5 +1,9 @@
 # Changes
 
+## master
+
+- Fix stuck volume terminating when the volume was already deleted
+
 ## v1.3.1
 
 - Add correct deployment file for latest version
diff --git a/api/volume.go b/api/volume.go
index 4ee3eab..74dba08 100644
--- a/api/volume.go
+++ b/api/volume.go
@@ -241,6 +241,14 @@ func (s *VolumeService) Detach(ctx context.Context, volume *csi.Volume, server *
 
 	hcloudVolume, _, err := s.client.Volume.GetByID(ctx, int(volume.ID))
 	if err != nil {
+		if hcloud.IsError(err, hcloud.ErrorCodeNotFound) {
+			level.Info(s.logger).Log(
+				"msg", "volume to detach not found",
+				"volume-id", volume.ID,
+				"err", err,
+			)
+			return volumes.ErrVolumeNotFound
+		}
 		level.Info(s.logger).Log(
 			"msg", "failed to get volume to detach",
 			"volume-id", volume.ID,
diff --git a/driver/controller.go b/driver/controller.go
index 74d57ad..a8aab07 100644
--- a/driver/controller.go
+++ b/driver/controller.go
@@ -196,8 +196,9 @@ func (s *ControllerService) ControllerUnpublishVolume(ctx context.Context, req *
 	if err := s.volumeService.Detach(ctx, volume, server); err != nil {
 		code := codes.Internal
 		switch err {
-		case volumes.ErrVolumeNotFound:
-			code = codes.NotFound
+		case volumes.ErrVolumeNotFound: // Based on the spec it is save to assume that the call was successful if the volume is not found
+			resp := &proto.ControllerUnpublishVolumeResponse{}
+			return resp, nil
 		case volumes.ErrServerNotFound:
 			code = codes.NotFound
 		case volumes.ErrLockedServer:
diff --git a/driver/controller_test.go b/driver/controller_test.go
index a3a90c6..e374b50 100644
--- a/driver/controller_test.go
+++ b/driver/controller_test.go
@@ -682,11 +682,6 @@ func TestControllerServiceUnpublishVolumeDetachErrors(t *testing.T) {
 		DetachError error
 		Code        codes.Code
 	}{
-		{
-			Name:        "volume not found",
-			DetachError: volumes.ErrVolumeNotFound,
-			Code:        codes.NotFound,
-		},
 		{
 			Name:        "server not found",
 			DetachError: volumes.ErrServerNotFound,
-- 
GitLab