From 47f7357caee526a703b097a94097cea0e79d3094 Mon Sep 17 00:00:00 2001 From: ioito Date: Wed, 7 Aug 2019 17:55:58 +0800 Subject: [PATCH] hotfix: sync openstack cup and memory cmtbound --- pkg/cloudprovider/resources.go | 2 + pkg/compute/models/hosts.go | 14 +++++ pkg/multicloud/aliyun/host.go | 2 + pkg/multicloud/aws/host.go | 2 + pkg/multicloud/azure/classic_host.go | 2 + pkg/multicloud/azure/host.go | 2 + pkg/multicloud/esxi/host.go | 2 + pkg/multicloud/host_base.go | 26 +++++++++ pkg/multicloud/huawei/host.go | 2 + pkg/multicloud/openstack/host.go | 73 ++++++++++++++++++++++++++ pkg/multicloud/openstack/shell/host.go | 12 +++++ pkg/multicloud/qcloud/host.go | 2 + pkg/multicloud/ucloud/host.go | 2 + pkg/multicloud/zstack/host.go | 2 + 14 files changed, 145 insertions(+) create mode 100644 pkg/multicloud/host_base.go diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 71621d732d..8002c18169 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -218,7 +218,9 @@ type ICloudHost interface { GetNodeCount() int8 GetCpuDesc() string GetCpuMhz() int + GetCpuCmtbound() float32 GetMemSizeMB() int + GetMemCmtbound() float32 GetStorageSizeMB() int GetStorageType() string GetHostType() string diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 38db522e23..4644eead81 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -1394,6 +1394,14 @@ func (self *SHost) syncWithCloudHost(ctx context.Context, userCred mcclient.Toke self.StorageType = extHost.GetStorageType() self.HostType = extHost.GetHostType() + if cpuCmt := extHost.GetCpuCmtbound(); cpuCmt > 0 { + self.CpuCmtbound = cpuCmt + } + + if memCmt := extHost.GetMemCmtbound(); memCmt > 0 { + self.MemCmtbound = memCmt + } + self.IsEmulated = extHost.IsEmulated() self.Enabled = extHost.GetEnabled() @@ -1481,7 +1489,13 @@ func (manager *SHostManager) newFromCloudHost(ctx context.Context, userCred mccl host.StorageSize = extHost.GetStorageSizeMB() host.StorageType = extHost.GetStorageType() host.CpuCmtbound = 8.0 + if cpuCmt := extHost.GetCpuCmtbound(); cpuCmt > 0 { + host.CpuCmtbound = cpuCmt + } host.MemCmtbound = 1.0 + if memCmt := extHost.GetMemCmtbound(); memCmt > 0 { + host.MemCmtbound = memCmt + } host.ManagerId = provider.Id host.IsEmulated = extHost.IsEmulated() diff --git a/pkg/multicloud/aliyun/host.go b/pkg/multicloud/aliyun/host.go index 8b559243e5..a1dd8ce734 100644 --- a/pkg/multicloud/aliyun/host.go +++ b/pkg/multicloud/aliyun/host.go @@ -19,6 +19,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/multicloud" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -26,6 +27,7 @@ import ( ) type SHost struct { + multicloud.SHostBase zone *SZone } diff --git a/pkg/multicloud/aws/host.go b/pkg/multicloud/aws/host.go index 98badbac01..208f5d1d48 100644 --- a/pkg/multicloud/aws/host.go +++ b/pkg/multicloud/aws/host.go @@ -22,9 +22,11 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" ) type SHost struct { + multicloud.SHostBase zone *SZone } diff --git a/pkg/multicloud/azure/classic_host.go b/pkg/multicloud/azure/classic_host.go index 0c355afc07..26b742246b 100644 --- a/pkg/multicloud/azure/classic_host.go +++ b/pkg/multicloud/azure/classic_host.go @@ -22,9 +22,11 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" ) type SClassicHost struct { + multicloud.SHostBase zone *SZone } diff --git a/pkg/multicloud/azure/host.go b/pkg/multicloud/azure/host.go index 9f66855627..5f54fcfa25 100644 --- a/pkg/multicloud/azure/host.go +++ b/pkg/multicloud/azure/host.go @@ -27,10 +27,12 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/onecloud/pkg/util/seclib2" ) type SHost struct { + multicloud.SHostBase zone *SZone } diff --git a/pkg/multicloud/esxi/host.go b/pkg/multicloud/esxi/host.go index a727d1600b..cd33f8b89a 100644 --- a/pkg/multicloud/esxi/host.go +++ b/pkg/multicloud/esxi/host.go @@ -31,6 +31,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" ) var HOST_SYSTEM_PROPS = []string{"name", "parent", "summary", "config", "hardware", "vm", "datastore"} @@ -77,6 +78,7 @@ type SHostStorageInfo struct { } type SHost struct { + multicloud.SHostBase SManagedObject nicInfo []SHostNicInfo diff --git a/pkg/multicloud/host_base.go b/pkg/multicloud/host_base.go new file mode 100644 index 0000000000..7dce562985 --- /dev/null +++ b/pkg/multicloud/host_base.go @@ -0,0 +1,26 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package multicloud + +type SHostBase struct { +} + +func (self *SHostBase) GetCpuCmtbound() float32 { + return 0.0 +} + +func (self *SHostBase) GetMemCmtbound() float32 { + return 0.0 +} diff --git a/pkg/multicloud/huawei/host.go b/pkg/multicloud/huawei/host.go index 8e537f5976..c8ec251dc2 100644 --- a/pkg/multicloud/huawei/host.go +++ b/pkg/multicloud/huawei/host.go @@ -22,10 +22,12 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/onecloud/pkg/util/billing" ) type SHost struct { + multicloud.SHostBase zone *SZone projectId string diff --git a/pkg/multicloud/openstack/host.go b/pkg/multicloud/openstack/host.go index 82c20dbb8d..1c00ad5855 100644 --- a/pkg/multicloud/openstack/host.go +++ b/pkg/multicloud/openstack/host.go @@ -16,7 +16,11 @@ package openstack import ( "fmt" + "strconv" "strings" + "time" + + "github.com/pkg/errors" "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" @@ -56,6 +60,7 @@ type SHost struct { CpuInfo string + Aggregates []string CurrentWorkload int Status string State string @@ -316,6 +321,50 @@ func (host *SHost) GetSN() string { return "" } +func (host *SHost) GetCpuCmtbound() float32 { + aggregates, err := host.zone.region.GetAggregates() + if err != nil || len(aggregates) == 0 { + return 16.0 + } + CpuCmtbound := 1000000.0 + for _, aggregate := range aggregates { + if utils.IsInStringArray(host.GetName(), aggregate.Hosts) { + if _cmtbound, ok := aggregate.Metadata["cpu_allocation_ratio"]; ok { + cmtbound, err := strconv.ParseFloat(_cmtbound, 32) + if err == nil && CpuCmtbound > cmtbound { + CpuCmtbound = cmtbound + } + } + } + } + if CpuCmtbound >= 1000000.0 { + return 16.0 + } + return float32(CpuCmtbound) +} + +func (host *SHost) GetMemCmtbound() float32 { + aggregates, err := host.zone.region.GetAggregates() + if err != nil || len(aggregates) == 0 { + return 1.5 + } + MemCmtbound := 1000000.0 + for _, aggregate := range aggregates { + if utils.IsInStringArray(host.GetName(), aggregate.Hosts) { + if _cmtbound, ok := aggregate.Metadata["ram_allocation_ratio"]; ok { + cmtbound, err := strconv.ParseFloat(_cmtbound, 32) + if err == nil && MemCmtbound > cmtbound { + MemCmtbound = cmtbound + } + } + } + } + if MemCmtbound >= 1000000.0 { + return 1.5 + } + return float32(MemCmtbound) +} + func (host *SHost) GetCpuCount() int { if host.Vcpus > 0 { return host.Vcpus @@ -428,3 +477,27 @@ func (host *SHost) Refresh() error { } return nil } + +type SAggregate struct { + AvailabilityZone string + CreatedAt time.Time + Deleted bool + Hosts []string + Id string + Metadata map[string]string + Name string + Uuid string +} + +func (region *SRegion) GetAggregates() ([]SAggregate, error) { + _, resp, err := region.List("compute", "/os-aggregates", "", nil) + if err != nil { + return nil, err + } + aggregates := []SAggregate{} + err = resp.Unmarshal(&aggregates, "aggregates") + if err != nil { + return nil, errors.Wrap(err, `resp.Unmarshal(&aggregates, "aggregates")`) + } + return aggregates, nil +} diff --git a/pkg/multicloud/openstack/shell/host.go b/pkg/multicloud/openstack/shell/host.go index 117c0bd218..d8c6be2f06 100644 --- a/pkg/multicloud/openstack/shell/host.go +++ b/pkg/multicloud/openstack/shell/host.go @@ -51,4 +51,16 @@ func init() { return nil }) + type AggregateListOption struct { + } + + shellutils.R(&AggregateListOption{}, "aggregate-list", "List os-aggregates", func(cli *openstack.SRegion, args *AggregateListOption) error { + aggregates, err := cli.GetAggregates() + if err != nil { + return err + } + printList(aggregates, 0, 0, 0, []string{}) + return nil + }) + } diff --git a/pkg/multicloud/qcloud/host.go b/pkg/multicloud/qcloud/host.go index 042622615b..c8c8fe9991 100644 --- a/pkg/multicloud/qcloud/host.go +++ b/pkg/multicloud/qcloud/host.go @@ -23,10 +23,12 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" "yunion.io/x/onecloud/pkg/util/billing" ) type SHost struct { + multicloud.SHostBase zone *SZone } diff --git a/pkg/multicloud/ucloud/host.go b/pkg/multicloud/ucloud/host.go index b555cbd4ec..6343b553a2 100644 --- a/pkg/multicloud/ucloud/host.go +++ b/pkg/multicloud/ucloud/host.go @@ -27,9 +27,11 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" ) type SHost struct { + multicloud.SHostBase zone *SZone projectId string diff --git a/pkg/multicloud/zstack/host.go b/pkg/multicloud/zstack/host.go index b5ba3201e9..4cedda9584 100644 --- a/pkg/multicloud/zstack/host.go +++ b/pkg/multicloud/zstack/host.go @@ -22,11 +22,13 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" api "yunion.io/x/onecloud/pkg/apis/compute" ) type SHost struct { + multicloud.SHostBase zone *SZone ZStackBasic