diff --git a/pkg/util/openstack/host.go b/pkg/util/openstack/host.go index 6b5e936bc5..82c20dbb8d 100644 --- a/pkg/util/openstack/host.go +++ b/pkg/util/openstack/host.go @@ -16,6 +16,7 @@ package openstack import ( "fmt" + "strings" "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" @@ -108,7 +109,29 @@ func (host *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) { } func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) { - return host.zone.GetIStorages() + istorages, err := host.zone.GetIStorages() + if err != nil { + return nil, err + } + + schedulerPools, err := host.zone.getSchedulerStatsPool() + if err != nil { + return nil, err + } + + result := []cloudprovider.ICloudStorage{} + + for _, istorage := range istorages { + if storage := istorage.(*SStorage); len(storage.ExtraSpecs.VolumeBackendName) > 0 { + for _, pool := range schedulerPools { + if strings.HasPrefix(pool.Name, fmt.Sprintf("%s@%s", host.GetName(), storage.ExtraSpecs.VolumeBackendName)) { + result = append(result, istorage) + break + } + } + } + } + return result, nil } func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index 6fc4350c01..cc510ae066 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -34,6 +34,14 @@ const ( HYPERVISORS_VERSION = "2.28" ) +type SCapabilities struct { +} + +type SPool struct { + Name string + Capabilities SCapabilities +} + type HostState struct { Available bool Active bool @@ -50,6 +58,8 @@ type SZone struct { cachedHosts map[string][]string + schedulerPools []SPool + Hosts map[string]map[string]HostState } @@ -90,6 +100,28 @@ func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { return zone.iwires, nil } +func (zone *SZone) fetchSchedulerStatsPool() error { + zone.schedulerPools = []SPool{} + for _, service := range []string{"volumev3", "volumev2", "volume"} { + _, resp, err := zone.region.List(service, "/scheduler-stats/get_pools", "", nil) + if err == nil { + if err := resp.Unmarshal(&zone.schedulerPools, "pools"); err != nil { + return err + } + return nil + } + log.Warningf("failed to get scheduler-stats pool by service %s error: %v, try another", service, err) + } + return fmt.Errorf("failed to find scheduler-stats pool by cinder service") +} + +func (zone *SZone) getSchedulerStatsPool() ([]SPool, error) { + if len(zone.schedulerPools) == 0 { + return zone.schedulerPools, zone.fetchSchedulerStatsPool() + } + return zone.schedulerPools, nil +} + func (zone *SZone) getStorageByCategory(category string) (*SStorage, error) { storages, err := zone.GetIStorages() if err != nil {