From 1f17dcf680a7fe84b0023d2061bc42a1d7b02ce9 Mon Sep 17 00:00:00 2001 From: TangBin Date: Fri, 23 Oct 2020 12:04:22 +0800 Subject: [PATCH 1/3] add qcloud elb zone1 details --- pkg/apis/compute/loadbalancer.go | 1 + pkg/apis/compute/zone.go | 15 ++++++++++++- pkg/compute/models/loadbalancers.go | 34 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/pkg/apis/compute/loadbalancer.go b/pkg/apis/compute/loadbalancer.go index 406534da29..3c8dc73ebb 100644 --- a/pkg/apis/compute/loadbalancer.go +++ b/pkg/apis/compute/loadbalancer.go @@ -174,6 +174,7 @@ type LoadbalancerDetails struct { VpcResourceInfoBase ZoneResourceInfoBase + Zone1ResourceInfoBase NetworkResourceInfoBase SLoadbalancer diff --git a/pkg/apis/compute/zone.go b/pkg/apis/compute/zone.go index bc1ed0356a..575f0a741f 100644 --- a/pkg/apis/compute/zone.go +++ b/pkg/apis/compute/zone.go @@ -88,13 +88,26 @@ type ZoneDetails struct { type ZoneResourceInfoBase struct { // 可用区名称 - // example: zone1 + // example: 北京一区 Zone string `json:"zone"` // 纳管云的zoneId ZoneExtId string `json:"zone_ext_id"` } +type Zone1ResourceInfoBase struct { + // 可用区名称 + // example: 北京2区 + Zone1 string `json:"zone_1"` + + // 可用区ID + // example: cn-beijing-2 + Zone1Id string `json:"zone_1_id"` + + // 纳管云的zoneId + Zone1ExtId string `json:"zone_1_ext_id"` +} + type ZoneResourceInfo struct { ZoneResourceInfoBase diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 22ad7ed97d..d2148cb6cb 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -614,6 +614,7 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( regRows := man.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) vpcRows := man.SVpcResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) zoneRows := man.SZoneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) + zone1Rows := man.FetchZone1ResourceInfos(ctx, userCred, query, objs) netRows := man.SNetworkResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) for i := range rows { @@ -625,6 +626,7 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( CloudregionResourceInfo: regRows[i], VpcResourceInfoBase: vpcRows[i].VpcResourceInfoBase, ZoneResourceInfoBase: zoneRows[i].ZoneResourceInfoBase, + Zone1ResourceInfoBase: zone1Rows[i], NetworkResourceInfoBase: netRows[i].NetworkResourceInfoBase, } rows[i], _ = objs[i].(*SLoadbalancer).getMoreDetails(rows[i]) @@ -632,6 +634,38 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( return rows } + +func (lb *SLoadbalancerManager) FetchZone1ResourceInfos(ctx context.Context, + userCred mcclient.TokenCredential, + query jsonutils.JSONObject, + objs []interface{}) []api.Zone1ResourceInfoBase { + rows := make([]api.Zone1ResourceInfoBase, len(objs)) + zoneIds := []string{} + for i := range objs { + zone1 := objs[i].(*SLoadbalancer).Zone1 + if len(zone1) > 0 { + zoneIds = append(zoneIds, zone1) + } + } + + zones := make(map[string]SZone) + err := db.FetchStandaloneObjectsByIds(ZoneManager, zoneIds, &zones) + if err != nil { + log.Errorf("FetchStandaloneObjectsByIds fail %s", err) + return rows + } + + for i := range objs { + if zone, ok := zones[objs[i].(*SLoadbalancer).Zone1]; ok { + rows[i].Zone1 = zone.GetName() + rows[i].Zone1Id = zone.GetId() + rows[i].Zone1ExtId = zone.GetExternalId() + } + } + + return rows +} + func (lb *SLoadbalancer) GetExtraDetails( ctx context.Context, userCred mcclient.TokenCredential, From 94d3fa82af3e2655bb5879e4e1ab3d3b7a28e6ac Mon Sep 17 00:00:00 2001 From: TangBin Date: Fri, 23 Oct 2020 14:40:24 +0800 Subject: [PATCH 2/3] elb sync zone bugfix --- pkg/apis/compute/zone.go | 6 +-- pkg/cloudprovider/resources.go | 1 + pkg/compute/models/loadbalancers.go | 27 +++++++++++- pkg/multicloud/aliyun/loadbalancer.go | 4 ++ pkg/multicloud/aws/loadbalancer.go | 16 ++++++- pkg/multicloud/huawei/loadbalancer.go | 12 ++++- pkg/multicloud/openstack/loadbalbacer.go | 4 ++ pkg/multicloud/qcloud/loadbalancer.go | 49 +++++++++++++++++++++ pkg/multicloud/qcloud/region.go | 2 - pkg/multicloud/qcloud/shell/loadbalancer.go | 34 ++++++++++++++ 10 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 pkg/multicloud/qcloud/shell/loadbalancer.go diff --git a/pkg/apis/compute/zone.go b/pkg/apis/compute/zone.go index 575f0a741f..47b727858b 100644 --- a/pkg/apis/compute/zone.go +++ b/pkg/apis/compute/zone.go @@ -98,11 +98,7 @@ type ZoneResourceInfoBase struct { type Zone1ResourceInfoBase struct { // 可用区名称 // example: 北京2区 - Zone1 string `json:"zone_1"` - - // 可用区ID - // example: cn-beijing-2 - Zone1Id string `json:"zone_1_id"` + Zone1Name string `json:"zone_1_name"` // 纳管云的zoneId Zone1ExtId string `json:"zone_1_ext_id"` diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 43a5193612..d547846d8c 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -511,6 +511,7 @@ type ICloudLoadbalancer interface { GetNetworkIds() []string GetVpcId() string GetZoneId() string + GetZone1Id() string // first slave zone GetLoadbalancerSpec() string GetChargeType() string GetEgressMbps() int diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index d2148cb6cb..0d72550bf4 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -657,8 +657,7 @@ func (lb *SLoadbalancerManager) FetchZone1ResourceInfos(ctx context.Context, for i := range objs { if zone, ok := zones[objs[i].(*SLoadbalancer).Zone1]; ok { - rows[i].Zone1 = zone.GetName() - rows[i].Zone1Id = zone.GetId() + rows[i].Zone1Name = zone.GetName() rows[i].Zone1ExtId = zone.GetExternalId() } } @@ -901,6 +900,12 @@ func (man *SLoadbalancerManager) newFromCloudLoadbalancer(ctx context.Context, u } } + if zoneId := extLb.GetZone1Id(); len(zoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, zoneId); err == nil && zone != nil { + lb.Zone1 = zone.GetId() + } + } + if extLb.GetMetadata() != nil { lb.LBInfo = extLb.GetMetadata() } @@ -1077,6 +1082,24 @@ func (lb *SLoadbalancer) SyncWithCloudLoadbalancer(ctx context.Context, userCred } } + if len(lb.ZoneId) == 0 { + extZoneId := extLb.GetZoneId() + if len(extZoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, extZoneId); err == nil && zone != nil { + lb.ZoneId = zone.GetId() + } + } + } + + if len(lb.Zone1) == 0 { + extZoneId := extLb.GetZone1Id() + if len(extZoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, extZoneId); err == nil && zone != nil { + lb.Zone1 = zone.GetId() + } + } + } + return nil }) diff --git a/pkg/multicloud/aliyun/loadbalancer.go b/pkg/multicloud/aliyun/loadbalancer.go index 3484ebb51d..c393e829e0 100644 --- a/pkg/multicloud/aliyun/loadbalancer.go +++ b/pkg/multicloud/aliyun/loadbalancer.go @@ -141,6 +141,10 @@ func (lb *SLoadbalancer) GetZoneId() string { return zone.GetGlobalId() } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (lb *SLoadbalancer) IsEmulated() bool { return false } diff --git a/pkg/multicloud/aws/loadbalancer.go b/pkg/multicloud/aws/loadbalancer.go index 386669a085..16b6ef8126 100644 --- a/pkg/multicloud/aws/loadbalancer.go +++ b/pkg/multicloud/aws/loadbalancer.go @@ -170,7 +170,21 @@ func (self *SElb) GetZoneId() string { } sort.Strings(zones) - return zones[0] + if len(zones) > 0 { + z, err := self.region.getZoneById(zones[0]) + if err != nil { + log.Infof("getZoneById %s %s", zones[0], err) + return "" + } + + return z.GetGlobalId() + } + + return "" +} + +func (self *SElb) GetZone1Id() string { + return "" } func (self *SElb) GetLoadbalancerSpec() string { diff --git a/pkg/multicloud/huawei/loadbalancer.go b/pkg/multicloud/huawei/loadbalancer.go index 3c2bfcfbae..bb4cf03b06 100644 --- a/pkg/multicloud/huawei/loadbalancer.go +++ b/pkg/multicloud/huawei/loadbalancer.go @@ -201,12 +201,22 @@ func (self *SLoadbalancer) GetVpcId() string { func (self *SLoadbalancer) GetZoneId() string { net := self.GetNetwork() if net != nil { - return net.AvailabilityZone + z, err := self.region.getZoneById(net.AvailabilityZone) + if err != nil { + log.Infof("getZoneById %s %s", net.AvailabilityZone, err) + return "" + } + + return z.GetGlobalId() } return "" } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (self *SLoadbalancer) GetLoadbalancerSpec() string { return "" } diff --git a/pkg/multicloud/openstack/loadbalbacer.go b/pkg/multicloud/openstack/loadbalbacer.go index 5b7c0f1a8f..5719ad1e66 100644 --- a/pkg/multicloud/openstack/loadbalbacer.go +++ b/pkg/multicloud/openstack/loadbalbacer.go @@ -200,6 +200,10 @@ func (lb *SLoadbalancer) GetZoneId() string { return lb.AvailabilityZone } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (lb *SLoadbalancer) IsEmulated() bool { return false } diff --git a/pkg/multicloud/qcloud/loadbalancer.go b/pkg/multicloud/qcloud/loadbalancer.go index c4c09d74a9..b9048b3e4f 100644 --- a/pkg/multicloud/qcloud/loadbalancer.go +++ b/pkg/multicloud/qcloud/loadbalancer.go @@ -71,6 +71,14 @@ type SLoadbalancer struct { CreateTime time.Time `json:"CreateTime"` Isolation int64 `json:"Isolation"` // 0:表示未被隔离,1:表示被隔离。 SubnetId string `json:"SubnetId"` + BackupZoneSet []ZoneSet `json:"BackupZoneSet"` + MasterZone ZoneSet `json:"MasterZone"` +} + +type ZoneSet struct { + Zone string `json:"Zone"` + ZoneID int64 `json:"ZoneId"` + ZoneName string `json:"ZoneName"` } func (self *SLoadbalancer) GetLoadbalancerSpec() string { @@ -316,6 +324,47 @@ func (self *SLoadbalancer) GetVpcId() string { } func (self *SLoadbalancer) GetZoneId() string { + zoneId := "" + if len(self.MasterZone.Zone) > 0 { + zoneId = self.MasterZone.Zone + } else if len(self.SubnetId) > 0 { + net, err := self.region.GetNetwork(self.SubnetId) + if err != nil { + log.Warningf("GetNetwork %s %s", self.SubnetId, err) + return "" + } + + zoneId = net.Zone + } + + if len(zoneId) > 0 { + z, err := self.region.getZoneById(zoneId) + if err != nil { + log.Warningf("getZoneById %s %s", zoneId, err) + return "" + } + + return z.GetGlobalId() + } + + return "" +} + +func (self *SLoadbalancer) GetZone1Id() string { + if self.BackupZoneSet == nil { + return "" + } + + if len(self.BackupZoneSet) > 0 { + z, err := self.region.getZoneById(self.BackupZoneSet[0].Zone) + if err != nil { + log.Warningf("getZoneById %s %s", self.BackupZoneSet[0].Zone, err) + return "" + } + + return z.GetGlobalId() + } + return "" } diff --git a/pkg/multicloud/qcloud/region.go b/pkg/multicloud/qcloud/region.go index 2ae14b9293..f375aaba55 100644 --- a/pkg/multicloud/qcloud/region.go +++ b/pkg/multicloud/qcloud/region.go @@ -195,8 +195,6 @@ func (self *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalanc if len(loadbalancer.ZoneID) > 0 { if len(loadbalancer.SlaveZoneID) > 0 { params["MasterZoneId"] = loadbalancer.ZoneID - } else { - params["ZoneId"] = loadbalancer.ZoneID } } } diff --git a/pkg/multicloud/qcloud/shell/loadbalancer.go b/pkg/multicloud/qcloud/shell/loadbalancer.go new file mode 100644 index 0000000000..2e3510c903 --- /dev/null +++ b/pkg/multicloud/qcloud/shell/loadbalancer.go @@ -0,0 +1,34 @@ +// 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 shell + +import ( + "yunion.io/x/onecloud/pkg/multicloud/qcloud" + "yunion.io/x/onecloud/pkg/util/shellutils" +) + +func init() { + type LbListOptions struct { + } + shellutils.R(&LbListOptions{}, "lb-list", "List loadbalancers", func(cli *qcloud.SRegion, args *LbListOptions) error { + lbs, err := cli.GetILoadBalancers() + if err != nil { + return err + } + + printList(lbs, 0, 0, 0, []string{}) + return nil + }) +} From 6f2a9d8b7c1106077757d669318a3e5b907f7f16 Mon Sep 17 00:00:00 2001 From: TangBin Date: Fri, 23 Oct 2020 16:22:42 +0800 Subject: [PATCH 3/3] elb salve zone 1 external id bugfix --- pkg/compute/models/loadbalancers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 0d72550bf4..834012671c 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -658,7 +658,7 @@ func (lb *SLoadbalancerManager) FetchZone1ResourceInfos(ctx context.Context, for i := range objs { if zone, ok := zones[objs[i].(*SLoadbalancer).Zone1]; ok { rows[i].Zone1Name = zone.GetName() - rows[i].Zone1ExtId = zone.GetExternalId() + rows[i].Zone1ExtId = fetchExternalId(zone.GetExternalId()) } }