From 3ab8beee03d4b0f824036f413b483ee84b3b9a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 1 Mar 2019 11:35:06 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E8=BF=87=E6=BB=A4zone=E5=BA=95=E4=B8=8B?= =?UTF-8?q?=E7=9A=84host?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/openstack/region.go | 11 ++++++++++- pkg/util/openstack/zone.go | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index 844bc24e73..67a2215c25 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -168,7 +168,7 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) } func (region *SRegion) fetchZones() error { - _, resp, err := region.Get("compute", "/os-availability-zone", "", jsonutils.NewDict()) + _, resp, err := region.Get("compute", "/os-availability-zone/detail", "", jsonutils.NewDict()) if err != nil { return err } @@ -181,6 +181,15 @@ func (region *SRegion) fetchZones() error { if zones[i].ZoneName == "internal" { continue } + zones[i].hosts = []string{} + for hostname, hostInfo := range zones[i].Hosts { + for k := range hostInfo { + if k == "nova-compute" { + zones[i].hosts = append(zones[i].hosts, hostname) + } + } + } + log.Errorf("hosts: %s", zones[i].hosts) zones[i].region = region region.izones = append(region.izones, &zones[i]) } diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index 1be4c99fe6..788c5a8976 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -2,6 +2,7 @@ package openstack import ( "fmt" + "time" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -18,6 +19,12 @@ const ( HYPERVISORS_VERSION = "2.28" ) +type HostState struct { + Available bool + Active bool + UpdatedAt time.Time +} + type SZone struct { region *SRegion @@ -26,6 +33,10 @@ type SZone struct { ZoneName string ZoneState ZoneState + + hosts []string + + Hosts map[string]map[string]HostState } func (zone *SZone) GetMetadata() *jsonutils.JSONDict { From 7475aa0fb1fcc48f71f2b02f8fc136c314479e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 1 Mar 2019 11:40:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84host?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/openstack/region.go | 1 - pkg/util/openstack/zone.go | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index 67a2215c25..b299562a84 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -189,7 +189,6 @@ func (region *SRegion) fetchZones() error { } } } - log.Errorf("hosts: %s", zones[i].hosts) zones[i].region = region region.izones = append(region.izones, &zones[i]) } diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index 788c5a8976..202f4e4f74 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -2,6 +2,7 @@ package openstack import ( "fmt" + "strings" "time" "yunion.io/x/jsonutils" @@ -9,6 +10,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/version" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/pkg/utils" ) type ZoneState struct { @@ -160,6 +162,15 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { return nil, err } for i := 0; i < len(hosts); i++ { + //过滤非此zone底下的host + if !utils.IsInStringArray(hosts[i].HypervisorHostname, zone.hosts) { + continue + } + // 过滤vmware的机器 + hypervisor := strings.ToLower(hosts[i].HypervisorType) + if strings.Index(hypervisor, "vmware") != -1 { + continue + } hosts[i].zone = zone ihosts = append(ihosts, &hosts[i]) } From 41404e19b1e4fe5baf9fb43a1e5e25004ff67572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 1 Mar 2019 12:01:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=89=80=E6=9C=89?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/openstack/instance.go | 2 +- pkg/util/openstack/network.go | 4 ++-- pkg/util/openstack/region.go | 8 ++++---- pkg/util/openstack/vpc.go | 2 +- pkg/util/openstack/wire.go | 11 ++++++----- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/util/openstack/instance.go b/pkg/util/openstack/instance.go index 523c7b8583..06d8233fe2 100644 --- a/pkg/util/openstack/instance.go +++ b/pkg/util/openstack/instance.go @@ -137,7 +137,7 @@ func (region *SRegion) GetSecurityGroupsByInstance(instanceId string) ([]Securit func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstance, error) { _, maxVersion, _ := region.GetVersion("compute") - _, resp, err := region.Get("compute", "/servers/detail", maxVersion, nil) + _, resp, err := region.Get("compute", "/servers/detail?all_tenants=True", maxVersion, nil) if err != nil { return nil, err } diff --git a/pkg/util/openstack/network.go b/pkg/util/openstack/network.go index fd2180c2f3..3b63e57444 100644 --- a/pkg/util/openstack/network.go +++ b/pkg/util/openstack/network.go @@ -70,7 +70,7 @@ func (network *SNetwork) GetStatus() string { } func (network *SNetwork) Delete() error { - return network.wire.zone.region.DeleteNetwork(network.ID) + return network.wire.region.DeleteNetwork(network.ID) } func (network *SRegion) DeleteNetwork(networkId string) error { @@ -145,7 +145,7 @@ func (region *SRegion) GetNetworks(vpcId string) ([]SNetwork, error) { func (network *SNetwork) Refresh() error { log.Debugf("network refresh %s", network.Name) - new, err := network.wire.zone.region.GetNetwork(network.ID) + new, err := network.wire.region.GetNetwork(network.ID) if err != nil { return err } diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index b299562a84..8360903101 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -218,12 +218,12 @@ func (region *SRegion) fetchInfrastructure() error { return err } for i := 0; i < len(region.ivpcs); i++ { + vpc := region.ivpcs[i].(*SVpc) + wire := &SWire{region: region, vpc: vpc} + vpc.addWire(wire) for j := 0; j < len(region.izones); j++ { zone := region.izones[j].(*SZone) - vpc := region.ivpcs[i].(*SVpc) - wire := SWire{zone: zone, vpc: vpc} - zone.addWire(&wire) - vpc.addWire(&wire) + zone.addWire(wire) } } return nil diff --git a/pkg/util/openstack/vpc.go b/pkg/util/openstack/vpc.go index 43e739de86..f295732254 100644 --- a/pkg/util/openstack/vpc.go +++ b/pkg/util/openstack/vpc.go @@ -123,7 +123,7 @@ func (vpc *SVpc) fetchWires() error { return err } } - wire := SWire{zone: vpc.region.izones[0].(*SZone), vpc: vpc} + wire := SWire{region: vpc.region, vpc: vpc} vpc.iwires = []cloudprovider.ICloudWire{&wire} return nil } diff --git a/pkg/util/openstack/wire.go b/pkg/util/openstack/wire.go index 8673f2d7dc..b800bc77ab 100644 --- a/pkg/util/openstack/wire.go +++ b/pkg/util/openstack/wire.go @@ -9,8 +9,9 @@ import ( ) type SWire struct { - zone *SZone - vpc *SVpc + region *SRegion + + vpc *SVpc inetworks []cloudprovider.ICloudNetwork } @@ -20,7 +21,7 @@ func (wire *SWire) GetMetadata() *jsonutils.JSONDict { } func (wire *SWire) GetId() string { - return fmt.Sprintf("%s-%s", wire.vpc.GetId(), wire.zone.GetId()) + return fmt.Sprintf("%s-%s", wire.vpc.GetId(), wire.region.GetId()) } func (wire *SWire) GetName() string { @@ -40,7 +41,7 @@ func (wire *SWire) Refresh() error { } func (wire *SWire) GetGlobalId() string { - return fmt.Sprintf("%s-%s", wire.vpc.GetGlobalId(), wire.zone.GetGlobalId()) + return fmt.Sprintf("%s-%s", wire.vpc.GetGlobalId(), wire.region.GetGlobalId()) } func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc { @@ -48,7 +49,7 @@ func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc { } func (wire *SWire) GetIZone() cloudprovider.ICloudZone { - return wire.zone + return nil } func (wire *SWire) GetBandwidth() int { From 6063545a072bfac2b38a798096ba3ab3c4c09741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 1 Mar 2019 16:22:04 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=99=9A=E6=8B=9Fzone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/openstack/host.go | 2 +- pkg/util/openstack/instance.go | 8 +++----- pkg/util/openstack/network.go | 4 ++-- pkg/util/openstack/region.go | 17 ++++++++--------- pkg/util/openstack/shell/instance.go | 5 ++--- pkg/util/openstack/vpc.go | 2 +- pkg/util/openstack/wire.go | 11 +++++------ pkg/util/openstack/zone.go | 19 +++++-------------- 8 files changed, 27 insertions(+), 41 deletions(-) diff --git a/pkg/util/openstack/host.go b/pkg/util/openstack/host.go index 45816ea990..c7938367c6 100644 --- a/pkg/util/openstack/host.go +++ b/pkg/util/openstack/host.go @@ -95,7 +95,7 @@ func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, erro } func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) { - instances, err := host.zone.region.GetInstances(host.zone.ZoneName, host.GetName()) + instances, err := host.zone.region.GetInstances(host.GetName()) if err != nil { return nil, err } diff --git a/pkg/util/openstack/instance.go b/pkg/util/openstack/instance.go index 06d8233fe2..a5a18b96b1 100644 --- a/pkg/util/openstack/instance.go +++ b/pkg/util/openstack/instance.go @@ -135,7 +135,7 @@ func (region *SRegion) GetSecurityGroupsByInstance(instanceId string) ([]Securit return secgroups, resp.Unmarshal(&secgroups, "security_groups") } -func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstance, error) { +func (region *SRegion) GetInstances(hostName string) ([]SInstance, error) { _, maxVersion, _ := region.GetVersion("compute") _, resp, err := region.Get("compute", "/servers/detail?all_tenants=True", maxVersion, nil) if err != nil { @@ -146,10 +146,8 @@ func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstan return nil, err } for i := 0; i < len(instances); i++ { - if len(zoneName) == 0 || instances[i].AvailabilityZone == zoneName { - if len(hostName) == 0 || hostName == instances[i].Host { - result = append(result, instances[i]) - } + if len(hostName) == 0 || hostName == instances[i].Host { + result = append(result, instances[i]) } } return result, nil diff --git a/pkg/util/openstack/network.go b/pkg/util/openstack/network.go index 3b63e57444..fd2180c2f3 100644 --- a/pkg/util/openstack/network.go +++ b/pkg/util/openstack/network.go @@ -70,7 +70,7 @@ func (network *SNetwork) GetStatus() string { } func (network *SNetwork) Delete() error { - return network.wire.region.DeleteNetwork(network.ID) + return network.wire.zone.region.DeleteNetwork(network.ID) } func (network *SRegion) DeleteNetwork(networkId string) error { @@ -145,7 +145,7 @@ func (region *SRegion) GetNetworks(vpcId string) ([]SNetwork, error) { func (network *SNetwork) Refresh() error { log.Debugf("network refresh %s", network.Name) - new, err := network.wire.region.GetNetwork(network.ID) + new, err := network.wire.zone.region.GetNetwork(network.ID) if err != nil { return err } diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index 8360903101..72d7738e21 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -168,6 +168,7 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) } func (region *SRegion) fetchZones() error { + zone := &SZone{region: region, ZoneName: region.Name, hosts: map[string][]string{}} _, resp, err := region.Get("compute", "/os-availability-zone/detail", "", jsonutils.NewDict()) if err != nil { return err @@ -176,22 +177,20 @@ func (region *SRegion) fetchZones() error { if err := resp.Unmarshal(&zones, "availabilityZoneInfo"); err != nil { return err } - region.izones = []cloudprovider.ICloudZone{} for i := 0; i < len(zones); i++ { if zones[i].ZoneName == "internal" { continue } - zones[i].hosts = []string{} + zone.hosts[zones[i].ZoneName] = []string{} for hostname, hostInfo := range zones[i].Hosts { for k := range hostInfo { if k == "nova-compute" { - zones[i].hosts = append(zones[i].hosts, hostname) + zone.hosts[zones[i].ZoneName] = append(zone.hosts[zones[i].ZoneName], hostname) } } } - zones[i].region = region - region.izones = append(region.izones, &zones[i]) } + region.izones = []cloudprovider.ICloudZone{zone} return nil } @@ -218,12 +217,12 @@ func (region *SRegion) fetchInfrastructure() error { return err } for i := 0; i < len(region.ivpcs); i++ { - vpc := region.ivpcs[i].(*SVpc) - wire := &SWire{region: region, vpc: vpc} - vpc.addWire(wire) for j := 0; j < len(region.izones); j++ { zone := region.izones[j].(*SZone) - zone.addWire(wire) + vpc := region.ivpcs[i].(*SVpc) + wire := SWire{zone: zone, vpc: vpc} + zone.addWire(&wire) + vpc.addWire(&wire) } } return nil diff --git a/pkg/util/openstack/shell/instance.go b/pkg/util/openstack/shell/instance.go index fa00647313..e14fe563a9 100644 --- a/pkg/util/openstack/shell/instance.go +++ b/pkg/util/openstack/shell/instance.go @@ -9,11 +9,10 @@ import ( func init() { type InstanceListOptions struct { - ZoneID string `help:"Zone ID for filter instance list"` - Host string `help:"Host name for filter instance list"` + Host string `help:"Host name for filter instance list"` } shellutils.R(&InstanceListOptions{}, "instance-list", "List instances", func(cli *openstack.SRegion, args *InstanceListOptions) error { - instances, err := cli.GetInstances(args.ZoneID, args.Host) + instances, err := cli.GetInstances(args.Host) if err != nil { return err } diff --git a/pkg/util/openstack/vpc.go b/pkg/util/openstack/vpc.go index f295732254..43e739de86 100644 --- a/pkg/util/openstack/vpc.go +++ b/pkg/util/openstack/vpc.go @@ -123,7 +123,7 @@ func (vpc *SVpc) fetchWires() error { return err } } - wire := SWire{region: vpc.region, vpc: vpc} + wire := SWire{zone: vpc.region.izones[0].(*SZone), vpc: vpc} vpc.iwires = []cloudprovider.ICloudWire{&wire} return nil } diff --git a/pkg/util/openstack/wire.go b/pkg/util/openstack/wire.go index b800bc77ab..8673f2d7dc 100644 --- a/pkg/util/openstack/wire.go +++ b/pkg/util/openstack/wire.go @@ -9,9 +9,8 @@ import ( ) type SWire struct { - region *SRegion - - vpc *SVpc + zone *SZone + vpc *SVpc inetworks []cloudprovider.ICloudNetwork } @@ -21,7 +20,7 @@ func (wire *SWire) GetMetadata() *jsonutils.JSONDict { } func (wire *SWire) GetId() string { - return fmt.Sprintf("%s-%s", wire.vpc.GetId(), wire.region.GetId()) + return fmt.Sprintf("%s-%s", wire.vpc.GetId(), wire.zone.GetId()) } func (wire *SWire) GetName() string { @@ -41,7 +40,7 @@ func (wire *SWire) Refresh() error { } func (wire *SWire) GetGlobalId() string { - return fmt.Sprintf("%s-%s", wire.vpc.GetGlobalId(), wire.region.GetGlobalId()) + return fmt.Sprintf("%s-%s", wire.vpc.GetGlobalId(), wire.zone.GetGlobalId()) } func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc { @@ -49,7 +48,7 @@ func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc { } func (wire *SWire) GetIZone() cloudprovider.ICloudZone { - return nil + return wire.zone } func (wire *SWire) GetBandwidth() int { diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index 202f4e4f74..b033c31d6b 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -10,7 +10,6 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/version" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" - "yunion.io/x/pkg/utils" ) type ZoneState struct { @@ -33,10 +32,9 @@ type SZone struct { iwires []cloudprovider.ICloudWire istorages []cloudprovider.ICloudStorage - ZoneName string - ZoneState ZoneState + ZoneName string - hosts []string + hosts map[string][]string Hosts map[string]map[string]HostState } @@ -58,14 +56,11 @@ func (zone *SZone) GetGlobalId() string { } func (zone *SZone) IsEmulated() bool { - return false + return true } func (zone *SZone) GetStatus() string { - if zone.ZoneState.Available { - return models.ZONE_ENABLE - } - return models.ZONE_SOLDOUT + return models.ZONE_ENABLE } func (zone *SZone) Refresh() error { @@ -162,10 +157,6 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { return nil, err } for i := 0; i < len(hosts); i++ { - //过滤非此zone底下的host - if !utils.IsInStringArray(hosts[i].HypervisorHostname, zone.hosts) { - continue - } // 过滤vmware的机器 hypervisor := strings.ToLower(hosts[i].HypervisorType) if strings.Index(hypervisor, "vmware") != -1 { @@ -188,7 +179,7 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { return nil, err } for i := 0; i < len(_hosts); i++ { - if _hosts[i].Zone == zone.ZoneName && _hosts[i].Service == "compute" { + if _hosts[i].Service == "compute" { host := SHost{HostName: _hosts[i].HostName, Zone: _hosts[i].Zone, zone: zone} ihosts = append(ihosts, &host) } From dc799abbc9ae099bc65829849d097a9d460e3ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 1 Mar 2019 17:45:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/openstack/region.go | 6 +++--- pkg/util/openstack/zone.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/util/openstack/region.go b/pkg/util/openstack/region.go index 72d7738e21..c0e8c333aa 100644 --- a/pkg/util/openstack/region.go +++ b/pkg/util/openstack/region.go @@ -168,7 +168,7 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) } func (region *SRegion) fetchZones() error { - zone := &SZone{region: region, ZoneName: region.Name, hosts: map[string][]string{}} + zone := &SZone{region: region, ZoneName: region.Name, cachedHosts: map[string][]string{}} _, resp, err := region.Get("compute", "/os-availability-zone/detail", "", jsonutils.NewDict()) if err != nil { return err @@ -181,11 +181,11 @@ func (region *SRegion) fetchZones() error { if zones[i].ZoneName == "internal" { continue } - zone.hosts[zones[i].ZoneName] = []string{} + zone.cachedHosts[zones[i].ZoneName] = []string{} for hostname, hostInfo := range zones[i].Hosts { for k := range hostInfo { if k == "nova-compute" { - zone.hosts[zones[i].ZoneName] = append(zone.hosts[zones[i].ZoneName], hostname) + zone.cachedHosts[zones[i].ZoneName] = append(zone.cachedHosts[zones[i].ZoneName], hostname) } } } diff --git a/pkg/util/openstack/zone.go b/pkg/util/openstack/zone.go index b033c31d6b..a724046327 100644 --- a/pkg/util/openstack/zone.go +++ b/pkg/util/openstack/zone.go @@ -34,7 +34,7 @@ type SZone struct { ZoneName string - hosts map[string][]string + cachedHosts map[string][]string Hosts map[string]map[string]HostState }