diff --git a/go.mod b/go.mod index d3b1bfa..10a56e8 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/cuongpiger/joat v1.0.12 github.com/spf13/pflag v1.0.5 github.com/vngcloud/vngcloud-csi-volume-modifier v1.0.2 - github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.26 + github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.28 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 go.opentelemetry.io/otel v1.26.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 diff --git a/go.sum b/go.sum index e0d6cfb..164e14d 100644 --- a/go.sum +++ b/go.sum @@ -126,8 +126,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/vngcloud/vngcloud-csi-volume-modifier v1.0.2 h1:LBSW1T0W4HxJKtpgSYbL5ZL1XiyOTF2nHIs8uqG5uoo= github.com/vngcloud/vngcloud-csi-volume-modifier v1.0.2/go.mod h1:wkhxk+x3ILNOk0aUxGivvhdiOXxPOVNXeRDHUx/IR50= -github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.26 h1:76CDH/n8d8C3fCv+htmFXil0Dhh1oN07mO/GlumpnM4= -github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.26/go.mod h1:TN4rl3ifgmGoFCBOEdzr0lV4raBDmo13XKbRvxtlktw= +github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.28 h1:iOv30U4uRRBTiPHwT27WLnKbCLEImJLz/JdpQ0tM8+w= +github.com/vngcloud/vngcloud-go-sdk/v2 v2.2.28/go.mod h1:TN4rl3ifgmGoFCBOEdzr0lV4raBDmo13XKbRvxtlktw= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 h1:A3SayB3rNyt+1S6qpI9mHPkeHTZbD7XILEqWnYZb2l0= diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 89cdddc..0c722c5 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -138,30 +138,57 @@ func (s *cloud) GetVolume(volumeID string) (*lsentity.Volume, lserr.IError) { }, nil } -func (s *cloud) DeleteVolume(volID string) error { - vol, ierr := s.GetVolume(volID) - if ierr != nil && ierr.IsError(lsdkErrs.EcVServerVolumeNotFound) { - return nil - } +func (s *cloud) DeleteVolume(volID string) lserr.IError { + llog.InfoS("[INFO] - DeleteVolume: Start deleting the volume", "volumeId", volID) - if vol.CanDelete() { - _, err := s.waitVolumeAchieveStatus(volID, availableDeleteStatus) - if err != nil { - return err - } + var ( + ierr lserr.IError + ) + + _ = ljwait.ExponentialBackoff(ljwait.NewBackOff(5, 10, true, ljtime.Minute(10)), func() (bool, error) { + vol, sdkErr := s.client.VServerGateway().V2().VolumeService(). + GetBlockVolumeById(lsdkVolumeV2.NewGetBlockVolumeByIdRequest(volID)) - opt := lsdkVolumeV2.NewDeleteBlockVolumeByIdRequest(volID) - sdkErr := s.client.VServerGateway().V2().VolumeService().DeleteBlockVolumeById(opt) if sdkErr != nil { if sdkErr.IsError(lsdkErrs.EcVServerVolumeNotFound) { - return nil + ierr = nil // reset + llog.InfoS("[INFO] - DeleteVolume: The volume was deleted before", "volumeId", volID) + return true, nil } - return sdkErr.GetError() + ierr = lserr.ErrVServerVolumeFailedToGet(volID, sdkErr) + return false, nil } + + // Check can delete this volume + if vol.CanDelete() { + llog.InfoS("[INFO] - DeleteVolume: Deleting the volume", "volumeId", volID) + if sdkErr = s.client.VServerGateway().V2().VolumeService(). + DeleteBlockVolumeById(lsdkVolumeV2.NewDeleteBlockVolumeByIdRequest(volID)); sdkErr != nil { + if sdkErr.IsError(lsdkErrs.EcVServerVolumeNotFound) { + ierr = nil // reset + llog.InfoS("[INFO] - DeleteVolume: The volume was deleted before", "volumeId", volID) + return true, nil + } + + ierr = lserr.ErrVServerVolumeFailedToDelete(volID, sdkErr) + llog.ErrorS(ierr.GetError(), "[ERROR] - DeleteVolume: Failed to delete the volume", ierr.GetListParameters()...) + return false, nil + } + + ierr = nil // reset + return true, nil + } + + return false, nil + }) + + if ierr == nil { + llog.InfoS("[INFO] - DeleteVolume: Deleted the volume successfully", "volumeId", volID) + return nil } - return nil + return ierr } func (s *cloud) AttachVolume(pinstanceId, pvolumeId string) (*lsentity.Volume, error) { diff --git a/pkg/cloud/errors/cloud.go b/pkg/cloud/errors/cloud.go index a686651..351be5e 100644 --- a/pkg/cloud/errors/cloud.go +++ b/pkg/cloud/errors/cloud.go @@ -31,4 +31,13 @@ var ( WithKVparameters("volumeId", pvolId). WithParameters(psdkErr.GetParameters())) } + + ErrVServerVolumeFailedToDelete = func(pvolId string, psdkErr lsdkErr.ISdkError) IError { + return NewError(new(lsdkErr.SdkError). + WithErrorCode(EcVServerVolumeFailedToDelete). + WithErrors(psdkErr.GetError()). + WithMessage(lfmt.Sprintf("Failed to delete volume %s", pvolId)). + WithKVparameters("volumeId", pvolId). + WithParameters(psdkErr.GetParameters())) + } ) diff --git a/pkg/cloud/errors/error_code.go b/pkg/cloud/errors/error_code.go index 7993af2..6122148 100644 --- a/pkg/cloud/errors/error_code.go +++ b/pkg/cloud/errors/error_code.go @@ -19,4 +19,5 @@ const ( EcVServerVolumeIsInErrorState = lsdkErrs.ErrorCode("VServerVolumeIsInErrorState") EcVServerVolumeFailedToDetach = lsdkErrs.ErrorCode("VServerVolumeFailedToDetach") EcVServerVolumeFailedToGet = lsdkErrs.ErrorCode("VServerVolumeFailedToGet") + EcVServerVolumeFailedToDelete = lsdkErrs.ErrorCode("VServerVolumeFailedToDelete") ) diff --git a/pkg/cloud/icloud.go b/pkg/cloud/icloud.go index 1f973d4..fa9abfd 100644 --- a/pkg/cloud/icloud.go +++ b/pkg/cloud/icloud.go @@ -11,7 +11,7 @@ type Cloud interface { EitherCreateResizeVolume(preq lsdkVolumeV2.ICreateBlockVolumeRequest) (*lsentity.Volume, lserr.IError) GetVolumeByName(pvolName string) (*lsentity.Volume, lserr.IError) GetVolume(volumeID string) (*lsentity.Volume, lserr.IError) - DeleteVolume(volID string) error + DeleteVolume(volID string) lserr.IError AttachVolume(instanceID, volumeID string) (*lsentity.Volume, error) DetachVolume(instanceID, volumeID string) lserr.IError ModifyVolumeType(pvolumeId, pvolumeType string, psize int) lserr.IError diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 9160e03..302eb29 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -248,7 +248,7 @@ func (s *controllerService) DeleteVolume(pctx lctx.Context, preq *lcsi.DeleteVol if err := s.cloud.DeleteVolume(volumeID); err != nil { if err != nil { - llog.ErrorS(err, "[ERROR] - DeleteVolume: failed to delete volume", "volumeID", volumeID) + llog.ErrorS(err.GetError(), "[ERROR] - DeleteVolume: failed to delete volume", "volumeID", volumeID) return nil, ErrFailedToDeleteVolume(volumeID) } }