Skip to content

Commit

Permalink
storage: Handle unsupported response from drivers (From Incus) (#14007)
Browse files Browse the repository at this point in the history
This goes from a 500 resp to something that looks like this if the
driver reports `ErrNotSupported`:
```json
{
	"usage": null
}
```
For example, a btrfs volume [without a quota
set](https://github.com/canonical/lxd/blob/main/lxd/storage/drivers/driver_btrfs_volumes.go#L892).

It makes sense to me to pass through the lack of quota rather than just
failing the entire request/response.

Contains cherry-picks from lxc/incus#1143
  • Loading branch information
tomponline authored Aug 29, 2024
2 parents bd35d6d + 4018acd commit 861761c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lxd/storage_volumes_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/canonical/lxd/lxd/request"
"github.com/canonical/lxd/lxd/response"
storagePools "github.com/canonical/lxd/lxd/storage"
storageDrivers "github.com/canonical/lxd/lxd/storage/drivers"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/entity"
Expand Down Expand Up @@ -121,7 +122,7 @@ func storagePoolVolumeTypeStateGet(d *Daemon, r *http.Request) response.Response
if volumeType == cluster.StoragePoolVolumeTypeCustom {
// Custom volumes.
usage, err = pool.GetCustomVolumeUsage(projectName, volumeName)
if err != nil {
if err != nil && err != storageDrivers.ErrNotSupported {
return response.SmartError(err)
}
} else {
Expand All @@ -141,23 +142,26 @@ func storagePoolVolumeTypeStateGet(d *Daemon, r *http.Request) response.Response
}

usage, err = pool.GetInstanceUsage(inst)
if err != nil {
if err != nil && err != storageDrivers.ErrNotSupported {
return response.SmartError(err)
}
}

// Prepare the state struct.
state := api.StorageVolumeState{}
state.Usage = &api.StorageVolumeStateUsage{}

// Only fill 'used' field if receiving a valid value.
if usage.Used >= 0 {
state.Usage.Used = uint64(usage.Used)
}
if usage != nil {
state.Usage = &api.StorageVolumeStateUsage{}

// Only fill 'total' field if receiving a valid value.
if usage.Total >= 0 {
state.Usage.Total = usage.Total
// Only fill 'used' field if receiving a valid value.
if usage.Used >= 0 {
state.Usage.Used = uint64(usage.Used)
}

// Only fill 'total' field if receiving a valid value.
if usage.Total >= 0 {
state.Usage.Total = usage.Total
}
}

return response.SyncResponse(true, state)
Expand Down

0 comments on commit 861761c

Please sign in to comment.