Skip to content

Commit

Permalink
refactor: update volume size error
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jun 13, 2024
1 parent b1c8282 commit 0739b2f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions api/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ func (s *VolumeService) Resize(ctx context.Context, volume *csi.Volume, size int
logger = log.With(logger, "current-size", hcloudVolume.Size)

if hcloudVolume.Size >= size {
level.Info(logger).Log("msg", "volume already has a size larger or equal than the request")
return volumes.ErrVolumeAlreadyFulfillsSizeRequirement
level.Info(logger).Log("msg", "volume size is already larger or equal than the requested size")
return volumes.ErrVolumeSizeAlreadyReached
}

action, _, err := s.client.Volume.Resize(ctx, hcloudVolume, size)
Expand Down
6 changes: 3 additions & 3 deletions api/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func makeTestVolumeService(t *testing.T, requests []mocked.Request) (*VolumeServ
}

func TestResize(t *testing.T) {
t.Run("ErrVolumeAlreadyFulfillsSizeRequirement", func(t *testing.T) {
t.Run("ErrVolumeSizeAlreadyReached", func(t *testing.T) {
t.Run("happy with larger volume size", func(t *testing.T) {
volumeService, cleanup := makeTestVolumeService(t, []mocked.Request{
{
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestResize(t *testing.T) {
defer cleanup()

err := volumeService.Resize(context.Background(), &csi.Volume{ID: 1}, 15)
assert.Equal(t, volumes.ErrVolumeAlreadyFulfillsSizeRequirement, err)
assert.Equal(t, volumes.ErrVolumeSizeAlreadyReached, err)
})

t.Run("with smaller volume size", func(t *testing.T) {
Expand All @@ -88,7 +88,7 @@ func TestResize(t *testing.T) {
defer cleanup()

err := volumeService.Resize(context.Background(), &csi.Volume{ID: 1}, 10)
assert.Equal(t, volumes.ErrVolumeAlreadyFulfillsSizeRequirement, err)
assert.Equal(t, volumes.ErrVolumeSizeAlreadyReached, err)
})
})
}
2 changes: 1 addition & 1 deletion volumes/idempotency.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *IdempotentService) Detach(ctx context.Context, volume *csi.Volume, serv

func (s *IdempotentService) Resize(ctx context.Context, volume *csi.Volume, size int) error {
switch err := s.volumeService.Resize(ctx, volume, size); err {
case ErrVolumeAlreadyFulfillsSizeRequirement:
case ErrVolumeSizeAlreadyReached:
// If a previous rescale attempt failed (rate limit, network connectivity, ...), the volume might already have the target size.
// In the Hetzner Cloud API, a resize must always be larger than the current size, so this
// would manifest as a "volume size is too small (invalid_input)" error.
Expand Down
4 changes: 2 additions & 2 deletions volumes/idempotency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ func TestIdempotentServiceDetachAttachedToDifferentServer(t *testing.T) {
}

func TestIdempotentServiceExpand(t *testing.T) {
t.Run("ErrVolumeAlreadyFulfillsSizeRequirement", func(t *testing.T) {
t.Run("ErrVolumeSizeAlreadyReached", func(t *testing.T) {
volumeService := &mock.VolumeService{
ResizeFunc: func(ctx context.Context, volume *csi.Volume, size int) error {
return volumes.ErrVolumeAlreadyFulfillsSizeRequirement
return volumes.ErrVolumeSizeAlreadyReached
},
}

Expand Down
16 changes: 8 additions & 8 deletions volumes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

var (
ErrVolumeNotFound = errors.New("volume not found")
ErrVolumeAlreadyExists = errors.New("volume does already exist")
ErrServerNotFound = errors.New("server not found")
ErrAttached = errors.New("volume is attached")
ErrNotAttached = errors.New("volume is not attached")
ErrAttachLimitReached = errors.New("max number of attachments per server reached")
ErrLockedServer = errors.New("server is locked")
ErrVolumeAlreadyFulfillsSizeRequirement = errors.New("volume already has a size larger or equal than the request")
ErrVolumeNotFound = errors.New("volume not found")
ErrVolumeAlreadyExists = errors.New("volume does already exist")
ErrServerNotFound = errors.New("server not found")
ErrAttached = errors.New("volume is attached")
ErrNotAttached = errors.New("volume is not attached")
ErrAttachLimitReached = errors.New("max number of attachments per server reached")
ErrLockedServer = errors.New("server is locked")
ErrVolumeSizeAlreadyReached = errors.New("volume size is already larger or equal than the requested size")
)

type Service interface {
Expand Down

0 comments on commit 0739b2f

Please sign in to comment.