Skip to content

Commit

Permalink
fix(region): support baidu cloud (#21652)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Nov 21, 2024
1 parent 7c6fe30 commit 6dbe908
Show file tree
Hide file tree
Showing 45 changed files with 4,449 additions and 153 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ require (
k8s.io/cri-api v0.22.17
k8s.io/klog/v2 v2.20.0
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241120113602-163eeb5fbfbe
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241121082754-4ea6426f262a
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
yunion.io/x/jsonutils v1.0.1-0.20240930100528-1671a2d0d22f
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241120113602-163eeb5fbfbe h1:jQ6fI+DxmfgXpVx3u1uqqC3Bbfm+ogUhpeMUWGY/UTM=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241120113602-163eeb5fbfbe/go.mod h1:rj/pb3DitJlQaQD8UW1oxx/KD+PzDZqoywzqRJaFE9A=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241121082754-4ea6426f262a h1:1RFbYQtWXkBZA7Y2rKU0tgYuxHpoanSlhVGanDV9m4U=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20241121082754-4ea6426f262a/go.mod h1:rj/pb3DitJlQaQD8UW1oxx/KD+PzDZqoywzqRJaFE9A=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/compute/storage_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ const (
STORAGE_ECLOUD_SSD = compute.STORAGE_ECLOUD_SSD // 高性能盘
STORAGE_ECLOUD_SSDEBS = compute.STORAGE_ECLOUD_SSDEBS // 性能优化盘
STORAGE_ECLOUD_SYSTEM = compute.STORAGE_ECLOUD_SYSTEM // 系统盘

STORAGE_BAIDU_SSD = compute.STORAGE_BAIDU_SSD
STORAGE_BAIDU_PREMIUM_SSD = compute.STORAGE_BAIDU_PREMIUM_SSD
STORAGE_BAIDU_HDD = compute.STORAGE_BAIDU_HDD
STORAGE_BAIDU_ENHANCED_SSD_PL1 = compute.STORAGE_BAIDU_ENHANCED_SSD_PL1
STORAGE_BAIDU_ENHANCED_SSD_PL2 = compute.STORAGE_BAIDU_ENHANCED_SSD_PL2
STORAGE_BAIDU_ENHANCED_SSD_PL3 = compute.STORAGE_BAIDU_ENHANCED_SSD_PL3
)

const (
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/webconsole/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ const (
CLOUDPODS = "cloudpods"
PROXMOX = "proxmox"
VOLC_ENGINE = "volcengine"
BAIDU = "baidu"
SANGFOR = "sangfor"
)
165 changes: 165 additions & 0 deletions pkg/cloudmon/providerdriver/baidu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// 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 providerdriver

import (
"context"
"strconv"
"strings"
"sync"
"time"

"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/util/influxdb"
)

type BaiduCollect struct {
SBaseCollectDriver
}

func (self *BaiduCollect) GetProvider() string {
return api.CLOUD_PROVIDER_BAIDU
}

func (self *BaiduCollect) IsSupportMetrics() bool {
return true
}

func init() {
Register(&BaiduCollect{})
}

func (self *BaiduCollect) CollectAccountMetrics(ctx context.Context, account api.CloudaccountDetail) (influxdb.SMetricData, error) {
metric := influxdb.SMetricData{
Name: string(cloudprovider.METRIC_RESOURCE_TYPE_CLOUD_ACCOUNT),
Timestamp: time.Now(),
Tags: []influxdb.SKeyValue{},
Metrics: []influxdb.SKeyValue{},
}
for k, v := range account.GetMetricTags() {
metric.Tags = append([]influxdb.SKeyValue{
{
Key: k,
Value: v,
},
}, metric.Tags...)
}
for k, v := range account.GetMetricPairs() {
metric.Metrics = append([]influxdb.SKeyValue{
{
Key: k,
Value: v,
},
}, metric.Metrics...)
}
return metric, nil
}

func (self *BaiduCollect) CollectServerMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ServerDetails, start, end time.Time) error {
metrics := []influxdb.SMetricData{}
regionServers := map[string][]api.ServerDetails{}
for i := range res {
regionId := res[i].RegionExtId
_, ok := regionServers[regionId]
if !ok {
regionServers[regionId] = []api.ServerDetails{}
}
regionServers[regionId] = append(regionServers[regionId], res[i])
}
var wg sync.WaitGroup
var mu sync.Mutex
for regionId, servers := range regionServers {
wg.Add(1)
go func(regionId string, servers []api.ServerDetails) {
defer func() {
wg.Done()
}()
data := []cloudprovider.MetricValues{}
for i := 0; i < len(servers); i++ {
for _, metricType := range cloudprovider.ALL_VM_METRIC_TYPES {
opts := &cloudprovider.MetricListOptions{
ResourceType: cloudprovider.METRIC_RESOURCE_TYPE_SERVER,
RegionExtId: regionId,
MetricType: metricType,
StartTime: start,
EndTime: end,
OsType: servers[i].OsType,
ResourceId: servers[i].ExternalId,
}
part, err := provider.GetMetrics(opts)
if err != nil {
if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
log.Errorf("get server %s(%s) error: %v", strings.Join(opts.ResourceIds, ","), regionId, err)
continue
}
continue
}
data = append(data, part...)
}
}
for _, value := range data {
server, ok := res[value.Id]
if !ok {
continue
}
tags := []influxdb.SKeyValue{}
for k, v := range server.GetMetricTags() {
tags = append(tags, influxdb.SKeyValue{
Key: k,
Value: v,
})
}
pairs := []influxdb.SKeyValue{}
for k, v := range server.GetMetricPairs() {
pairs = append(pairs, influxdb.SKeyValue{
Key: k,
Value: v,
})
}
for _, v := range value.Values {
metric := influxdb.SMetricData{
Name: value.MetricType.Name(),
Timestamp: v.Timestamp,
Tags: []influxdb.SKeyValue{},
Metrics: []influxdb.SKeyValue{
{
Key: value.MetricType.Key(),
Value: strconv.FormatFloat(v.Value, 'E', -1, 64),
},
},
}
for k, v := range v.Tags {
metric.Tags = append(metric.Tags, influxdb.SKeyValue{
Key: k,
Value: v,
})
}
metric.Metrics = append(metric.Metrics, pairs...)
metric.Tags = append(metric.Tags, tags...)
mu.Lock()
metrics = append(metrics, metric)
mu.Unlock()
}
}
}(regionId, servers)
}
wg.Wait()

return self.sendMetrics(ctx, manager, "server", len(res), metrics)
}
42 changes: 38 additions & 4 deletions pkg/compute/guestdrivers/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package guestdrivers

import (
"fmt"

"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/pkg/util/billing"
"yunion.io/x/pkg/util/rbacscope"
"yunion.io/x/pkg/utils"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
Expand Down Expand Up @@ -45,13 +48,17 @@ func (self *SBaiduGuestDriver) GetProvider() string {
}

func (self *SBaiduGuestDriver) GetDefaultSysDiskBackend() string {
return ""
return api.STORAGE_BAIDU_SSD
}

func (self *SBaiduGuestDriver) GetMinimalSysDiskSizeGb() int {
return 20
}

func (self *SBaiduGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) {
return chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil
}

func (self *SBaiduGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
keys := models.SComputeResourceKeys{}
keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
Expand Down Expand Up @@ -79,20 +86,40 @@ func (self *SBaiduGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCa
}
}

func (self *SBaiduGuestDriver) GetDetachDiskStatus() ([]string, error) {
return []string{api.VM_READY, api.VM_RUNNING}, nil
}

func (self *SBaiduGuestDriver) GetAttachDiskStatus() ([]string, error) {
return []string{api.VM_READY, api.VM_RUNNING}, nil
}

func (self *SBaiduGuestDriver) GetChangeConfigStatus(guest *models.SGuest) ([]string, error) {
return []string{api.VM_READY}, nil
}

func (self *SBaiduGuestDriver) GetDeployStatus() ([]string, error) {
return []string{api.VM_READY, api.VM_RUNNING}, nil
}

func (self *SBaiduGuestDriver) GetGuestInitialStateAfterCreate() string {
return api.VM_READY
return api.VM_RUNNING
}

func (self *SBaiduGuestDriver) GetGuestInitialStateAfterRebuild() string {
return api.VM_READY
return api.VM_RUNNING
}

func (self *SBaiduGuestDriver) GetRebuildRootStatus() ([]string, error) {
return []string{api.VM_READY, api.VM_RUNNING}, nil
}

func (self *SBaiduGuestDriver) AllowReconfigGuest() bool {
return true
}

func (self *SBaiduGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool {
return false
return true
}

func (self *SBaiduGuestDriver) IsSupportPublicipToEip() bool {
Expand All @@ -102,3 +129,10 @@ func (self *SBaiduGuestDriver) IsSupportPublicipToEip() bool {
func (self *SBaiduGuestDriver) IsSupportSetAutoRenew() bool {
return false
}

func (self *SBaiduGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) {
return fmt.Errorf("cannot resize disk when guest in status %s", guest.Status)
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/compute/guestdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ func (drv *SManagedVirtualizedGuestDriver) GetJsonDescAtHost(ctx context.Context
provider := host.GetCloudprovider()
config.ProjectId, err = provider.SyncProject(ctx, userCred, guest.ProjectId)
if err != nil {
logclient.AddSimpleActionLog(guest, logclient.ACT_SYNC_CLOUD_PROJECT, err, userCred, false)
if errors.Cause(err) != cloudprovider.ErrNotSupported && errors.Cause(err) != cloudprovider.ErrNotImplemented {
logclient.AddSimpleActionLog(guest, logclient.ACT_SYNC_CLOUD_PROJECT, err, userCred, false)
}
}

disks, err := guest.GetDisks()
Expand Down
18 changes: 18 additions & 0 deletions pkg/compute/hostdrivers/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package hostdrivers

import (
"fmt"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/compute/models"
)
Expand All @@ -39,3 +41,19 @@ func (self *SBaiduHostDriver) GetHypervisor() string {
func (self *SBaiduHostDriver) GetProvider() string {
return api.CLOUD_PROVIDER_BAIDU
}

func (self *SBaiduHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
min, max := 20, 32765
switch storage.StorageType {
case api.STORAGE_BAIDU_HDD:
min = 5
case api.STORAGE_BAIDU_SSD:
min = 50
case api.STORAGE_BAIDU_PREMIUM_SSD:
min = 5
}
if sizeGb < min || sizeGb > max {
return fmt.Errorf("The %s disk size must be in the range of %d ~ %dGB", storage.StorageType, min, max)
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/compute/hostdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ func (self *SManagedVirtualizationHostDriver) RequestAllocateDiskOnStorage(ctx c
}
projectId, err := _cloudprovider.SyncProject(ctx, userCred, disk.ProjectId)
if err != nil {
logclient.AddSimpleActionLog(disk, logclient.ACT_SYNC_CLOUD_PROJECT, err, userCred, false)
if errors.Cause(err) != cloudprovider.ErrNotSupported && errors.Cause(err) != cloudprovider.ErrNotImplemented {
logclient.AddSimpleActionLog(disk, logclient.ACT_SYNC_CLOUD_PROJECT, err, userCred, false)
}
}
conf := cloudprovider.DiskCreateConfig{
Name: disk.GetName(),
Expand Down
6 changes: 5 additions & 1 deletion pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/billing"
"yunion.io/x/pkg/util/httputils"
Expand Down Expand Up @@ -1559,7 +1560,10 @@ func (self *SGuest) StartGueststartTask(
data *jsonutils.JSONDict, parentTaskId string,
) error {
schedStart := self.Hypervisor == api.HYPERVISOR_KVM && self.guestDisksStorageTypeIsShared()
startFromCreate := jsonutils.QueryBoolean(data, "start_from_create", false)
startFromCreate := false
if !gotypes.IsNil(data) {
startFromCreate = jsonutils.QueryBoolean(data, "start_from_create", false)
}
if options.Options.IgnoreNonrunningGuests {
host := HostManager.FetchHostById(self.HostId)
if !startFromCreate && host != nil && host.EnableNumaAllocate {
Expand Down
1 change: 1 addition & 0 deletions pkg/compute/models/regiondrivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type ISecurityGroupDriver interface {
RequestCreateSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, secgroup *SSecurityGroup, rules api.SSecgroupRuleResourceSet) error
// 根据安全组归属vpc还是region进行过滤
GetSecurityGroupFilter(vpc *SVpc) (func(q *sqlchemy.SQuery) *sqlchemy.SQuery, error)
GetDefaultSecurityGroupNamePrefix() string
CreateDefaultSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, vpc *SVpc) (*SSecurityGroup, error)
RequestPrepareSecurityGroups(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, secgroups []SSecurityGroup, vpc *SVpc, callback func(ids []string) error, task taskman.ITask) error
RequestDeleteSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, secgroup *SSecurityGroup, task taskman.ITask) error
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@ func (self *SVpc) GetSecurityGroups() ([]SSecurityGroup, error) {
}

func (self *SVpc) GetDefaultSecurityGroup(ownerId mcclient.IIdentityProvider, filter func(q *sqlchemy.SQuery) *sqlchemy.SQuery) (*SSecurityGroup, error) {
q := SecurityGroupManager.Query().Equals("status", api.SECGROUP_STATUS_READY).Like("name", "default%")
q := SecurityGroupManager.Query().Equals("status", api.SECGROUP_STATUS_READY).Like("name", "%"+"default"+"%")

q = filter(q)
q = q.Filter(
Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/regiondrivers/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ func (self *SBaiduRegionDriver) GetProvider() string {
func (self *SBaiduRegionDriver) ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, storage *models.SStorage, input *api.SnapshotCreateInput) error {
return nil
}

func (self *SBaiduRegionDriver) GetDefaultSecurityGroupNamePrefix() string {
return "auot-default"
}
Loading

0 comments on commit 6dbe908

Please sign in to comment.