From 087cdeec5b0fae8e542fcd4699c593cb09f67b01 Mon Sep 17 00:00:00 2001 From: ioito Date: Thu, 9 Mar 2023 15:45:30 +0800 Subject: [PATCH] fix(region): add h3c --- go.mod | 2 +- go.sum | 4 +- pkg/apis/compute/cloudaccount_const.go | 9 +- pkg/apis/compute/guest_const.go | 5 + pkg/apis/compute/host_const.go | 2 + pkg/compute/guestdrivers/h3c.go | 184 ++++++++++++++++++ pkg/compute/hostdrivers/h3c.go | 67 +++++++ pkg/compute/regiondrivers/h3c.go | 45 +++++ vendor/modules.txt | 2 +- .../pkg/apis/compute/cloudaccount_const.go | 1 + .../cloudmux/pkg/apis/compute/guest_const.go | 1 + .../x/cloudmux/pkg/apis/compute/host_const.go | 1 + 12 files changed, 318 insertions(+), 5 deletions(-) create mode 100644 pkg/compute/guestdrivers/h3c.go create mode 100644 pkg/compute/hostdrivers/h3c.go create mode 100644 pkg/compute/regiondrivers/h3c.go diff --git a/go.mod b/go.mod index cb0b555528..cd139ca602 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( k8s.io/client-go v0.19.3 k8s.io/cluster-bootstrap v0.19.3 moul.io/http2curl/v2 v2.3.0 - yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309064841-9f842006b21d + yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309073903-be630d4cb16b yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 yunion.io/x/jsonutils v1.0.1-0.20220819091305-3bab322ab4fd yunion.io/x/log v1.0.0 diff --git a/go.sum b/go.sum index 379c439c07..d869e4f520 100644 --- a/go.sum +++ b/go.sum @@ -1164,8 +1164,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.20230309064841-9f842006b21d h1:EBYggOjBPBxVyMLeXqMyoF7/YCwXDDsz7zVsDXBR+EU= -yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309064841-9f842006b21d/go.mod h1:sWqblYRhQCO63xeKdxdAT3wczCCY8Mc+TDa3WD/a5Kg= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309073903-be630d4cb16b h1:DDZB/Z0NrpgDthZiLUGJ8UpEvlirpUt4oqHgh8Dlmcg= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309073903-be630d4cb16b/go.mod h1:sWqblYRhQCO63xeKdxdAT3wczCCY8Mc+TDa3WD/a5Kg= yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 h1:0zlZD5uhZoIHgLVAWCz2aHaYk2ZrNsACCYD7R6EIBII= yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws= yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634= diff --git a/pkg/apis/compute/cloudaccount_const.go b/pkg/apis/compute/cloudaccount_const.go index 70b4910397..56502e2f06 100644 --- a/pkg/apis/compute/cloudaccount_const.go +++ b/pkg/apis/compute/cloudaccount_const.go @@ -64,6 +64,7 @@ const ( CLOUD_PROVIDER_INCLOUD_SPHERE = compute.CLOUD_PROVIDER_INCLOUD_SPHERE CLOUD_PROVIDER_PROXMOX = compute.CLOUD_PROVIDER_PROXMOX CLOUD_PROVIDER_REMOTEFILE = compute.CLOUD_PROVIDER_REMOTEFILE + CLOUD_PROVIDER_H3C = compute.CLOUD_PROVIDER_H3C CLOUD_PROVIDER_GENERICS3 = compute.CLOUD_PROVIDER_GENERICS3 CLOUD_PROVIDER_CEPH = compute.CLOUD_PROVIDER_CEPH @@ -111,7 +112,9 @@ var ( CLOUD_PROVIDER_VALID_HEALTH_STATUS = []string{CLOUD_PROVIDER_HEALTH_NORMAL, CLOUD_PROVIDER_HEALTH_NO_PERMISSION} PRIVATE_CLOUD_PROVIDERS = []string{CLOUD_PROVIDER_ZSTACK, CLOUD_PROVIDER_OPENSTACK, CLOUD_PROVIDER_APSARA, CLOUD_PROVIDER_HCSO, CLOUD_PROVIDER_HCS, CLOUD_PROVIDER_HCSOP, - CLOUD_PROVIDER_INCLOUD_SPHERE, CLOUD_PROVIDER_PROXMOX, CLOUD_PROVIDER_REMOTEFILE} + CLOUD_PROVIDER_INCLOUD_SPHERE, CLOUD_PROVIDER_PROXMOX, CLOUD_PROVIDER_REMOTEFILE, + CLOUD_PROVIDER_H3C, + } CLOUD_PROVIDERS = []string{ CLOUD_PROVIDER_ONECLOUD, @@ -138,6 +141,7 @@ var ( CLOUD_PROVIDER_INCLOUD_SPHERE, CLOUD_PROVIDER_PROXMOX, CLOUD_PROVIDER_REMOTEFILE, + CLOUD_PROVIDER_H3C, } CLOUD_PROVIDER_HOST_TYPE_MAP = map[string][]string{ @@ -215,6 +219,9 @@ var ( CLOUD_PROVIDER_REMOTEFILE: { HOST_TYPE_REMOTEFILE, }, + CLOUD_PROVIDER_H3C: { + HOST_TYPE_H3C, + }, } ) diff --git a/pkg/apis/compute/guest_const.go b/pkg/apis/compute/guest_const.go index a1f7cc8654..6612568e41 100644 --- a/pkg/apis/compute/guest_const.go +++ b/pkg/apis/compute/guest_const.go @@ -196,6 +196,7 @@ const ( HYPERVISOR_INCLOUD_SPHERE = compute.HYPERVISOR_INCLOUD_SPHERE HYPERVISOR_PROXMOX = compute.HYPERVISOR_PROXMOX HYPERVISOR_REMOTEFILE = compute.HYPERVISOR_REMOTEFILE + HYPERVISOR_H3C = compute.HYPERVISOR_H3C // HYPERVISOR_DEFAULT = HYPERVISOR_KVM HYPERVISOR_DEFAULT = HYPERVISOR_KVM @@ -263,6 +264,7 @@ var HYPERVISORS = []string{ HYPERVISOR_INCLOUD_SPHERE, HYPERVISOR_PROXMOX, HYPERVISOR_REMOTEFILE, + HYPERVISOR_H3C, } var ONECLOUD_HYPERVISORS = []string{ @@ -297,6 +299,7 @@ var PRIVATE_CLOUD_HYPERVISORS = []string{ HYPERVISOR_INCLOUD_SPHERE, HYPERVISOR_PROXMOX, HYPERVISOR_REMOTEFILE, + HYPERVISOR_H3C, } // var HYPERVISORS = []string{HYPERVISOR_ALIYUN} @@ -328,6 +331,7 @@ var HYPERVISOR_HOSTTYPE = map[string]string{ HYPERVISOR_INCLOUD_SPHERE: HOST_TYPE_INCLOUD_SPHERE, HYPERVISOR_PROXMOX: HOST_TYPE_PROXMOX, HYPERVISOR_REMOTEFILE: HOST_TYPE_REMOTEFILE, + HYPERVISOR_H3C: HOST_TYPE_H3C, } var HOSTTYPE_HYPERVISOR = map[string]string{ @@ -357,6 +361,7 @@ var HOSTTYPE_HYPERVISOR = map[string]string{ HOST_TYPE_INCLOUD_SPHERE: HYPERVISOR_INCLOUD_SPHERE, HOST_TYPE_PROXMOX: HYPERVISOR_PROXMOX, HOST_TYPE_REMOTEFILE: HYPERVISOR_REMOTEFILE, + HOST_TYPE_H3C: HYPERVISOR_H3C, } const ( diff --git a/pkg/apis/compute/host_const.go b/pkg/apis/compute/host_const.go index c59522dd5b..aad781f8f3 100644 --- a/pkg/apis/compute/host_const.go +++ b/pkg/apis/compute/host_const.go @@ -47,6 +47,7 @@ const ( HOST_TYPE_INCLOUD_SPHERE = compute.HOST_TYPE_INCLOUD_SPHERE HOST_TYPE_PROXMOX = compute.HOST_TYPE_PROXMOX HOST_TYPE_REMOTEFILE = compute.HOST_TYPE_REMOTEFILE + HOST_TYPE_H3C = compute.HOST_TYPE_H3C HOST_TYPE_DEFAULT = HOST_TYPE_HYPERVISOR @@ -136,6 +137,7 @@ var HOST_TYPES = []string{ HOST_TYPE_INCLOUD_SPHERE, HOST_TYPE_PROXMOX, HOST_TYPE_REMOTEFILE, + HOST_TYPE_H3C, } var NIC_TYPES = []string{NIC_TYPE_IPMI, NIC_TYPE_ADMIN} diff --git a/pkg/compute/guestdrivers/h3c.go b/pkg/compute/guestdrivers/h3c.go new file mode 100644 index 0000000000..45254e2303 --- /dev/null +++ b/pkg/compute/guestdrivers/h3c.go @@ -0,0 +1,184 @@ +// 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 guestdrivers + +import ( + "context" + "database/sql" + "fmt" + + "yunion.io/x/cloudmux/pkg/cloudprovider" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/billing" + "yunion.io/x/pkg/util/rbacscope" + "yunion.io/x/pkg/utils" + "yunion.io/x/sqlchemy" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas" + "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/onecloud/pkg/mcclient" +) + +type SH3CGuestDriver struct { + SManagedVirtualizedGuestDriver +} + +func init() { + driver := SH3CGuestDriver{} + models.RegisterGuestDriver(&driver) +} + +func (self *SH3CGuestDriver) GetHypervisor() string { + return api.HYPERVISOR_H3C +} + +func (self *SH3CGuestDriver) GetProvider() string { + return api.CLOUD_PROVIDER_H3C +} + +func (self *SH3CGuestDriver) DoScheduleSKUFilter() bool { + return false +} + +func (self *SH3CGuestDriver) DoScheduleStorageFilter() bool { + return false +} + +func (self *SH3CGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys { + keys := models.SComputeResourceKeys{} + keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId) + keys.CloudEnv = api.CLOUD_ENV_PRIVATE_CLOUD + keys.Provider = api.CLOUD_PROVIDER_H3C + keys.Brand = api.CLOUD_PROVIDER_H3C + keys.Hypervisor = api.HYPERVISOR_H3C + return keys +} + +func (self *SH3CGuestDriver) GetDefaultSysDiskBackend() string { + return api.STORAGE_LOCAL +} + +func (self *SH3CGuestDriver) GetMinimalSysDiskSizeGb() int { + return 40 +} + +func (self *SH3CGuestDriver) GetStorageTypes() []string { + storages, _ := models.StorageManager.GetStorageTypesByHostType(api.HYPERVISOR_HOSTTYPE[self.GetHypervisor()]) + return storages +} + +func (self *SH3CGuestDriver) ChooseHostStorage(host *models.SHost, guest *models.SGuest, diskConfig *api.DiskConfig, storageIds []string) (*models.SStorage, error) { + return self.chooseHostStorage(self, host, diskConfig.Backend, storageIds), nil +} + +func (self *SH3CGuestDriver) GetDetachDiskStatus() ([]string, error) { + return []string{api.VM_READY, api.VM_RUNNING}, nil +} + +func (self *SH3CGuestDriver) GetAttachDiskStatus() ([]string, error) { + return []string{api.VM_READY, api.VM_RUNNING}, nil +} + +func (self *SH3CGuestDriver) GetRebuildRootStatus() ([]string, error) { + return []string{api.VM_READY, api.VM_RUNNING}, nil +} + +func (self *SH3CGuestDriver) GetChangeConfigStatus(guest *models.SGuest) ([]string, error) { + return []string{api.VM_READY}, nil +} + +func (self *SH3CGuestDriver) GetDeployStatus() ([]string, error) { + return []string{api.VM_READY, api.VM_RUNNING}, nil +} + +func (self *SH3CGuestDriver) 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 +} + +func (self *SH3CGuestDriver) GetGuestInitialStateAfterCreate() string { + return api.VM_RUNNING +} + +func (self *SH3CGuestDriver) GetGuestInitialStateAfterRebuild() string { + return api.VM_RUNNING +} + +func (self *SH3CGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability { + return cloudprovider.SInstanceCapability{ + Hypervisor: self.GetHypervisor(), + Provider: self.GetProvider(), + DefaultAccount: cloudprovider.SDefaultAccount{ + Linux: cloudprovider.SOsDefaultAccount{ + DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER, + }, + Windows: cloudprovider.SOsDefaultAccount{ + DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER, + }, + }, + } +} + +func (self *SH3CGuestDriver) RemoteDeployGuestSyncHost(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, host *models.SHost, iVM cloudprovider.ICloudVM) (cloudprovider.ICloudHost, error) { + if hostId := iVM.GetIHostId(); len(hostId) > 0 { + nh, err := db.FetchByExternalIdAndManagerId(models.HostManager, hostId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { + return q.Equals("manager_id", host.ManagerId) + }) + if err != nil { + log.Debugf("failed to found new hostId(%s) for ivm %s(%s) error: %v", hostId, guest.Name, guest.Id, err) + if errors.Cause(err) != sql.ErrNoRows { + return nil, errors.Wrap(err, "FetchByExternalIdAndManagerId") + } + + // HYPERVISOR_H3C VM被部署到一台全新的宿主机 + zone, err := host.GetZone() + if err != nil { + log.Warningf("host %s GetZone: %s", host.GetId(), err) + } else { + _host, err := models.HostManager.NewFromCloudHost(ctx, userCred, iVM.GetIHost(), host.GetCloudprovider(), zone) + if err != nil { + log.Warningf("NewFromCloudHost %s: %s", iVM.GetIHostId(), err) + } else { + host = _host + } + } + } else { + host = nh.(*models.SHost) + } + } + + if host.GetId() != guest.HostId { + guest.OnScheduleToHost(ctx, userCred, host.GetId()) + } + + return host.GetIHost(ctx) +} + +func (self *SH3CGuestDriver) IsSupportedBillingCycle(bc billing.SBillingCycle) bool { + return false +} + +func (self *SH3CGuestDriver) IsNeedInjectPasswordByCloudInit() bool { + return true +} + +func (self *SH3CGuestDriver) IsSupportSetAutoRenew() bool { + return false +} diff --git a/pkg/compute/hostdrivers/h3c.go b/pkg/compute/hostdrivers/h3c.go new file mode 100644 index 0000000000..3bcecc9087 --- /dev/null +++ b/pkg/compute/hostdrivers/h3c.go @@ -0,0 +1,67 @@ +// 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 hostdrivers + +import ( + "context" + "fmt" + + "yunion.io/x/jsonutils" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient" +) + +type SH3CHostDriver struct { + SManagedVirtualizationHostDriver +} + +func init() { + driver := SH3CHostDriver{} + models.RegisterHostDriver(&driver) +} + +func (self *SH3CHostDriver) GetHostType() string { + return api.HOST_TYPE_H3C +} + +func (self *SH3CHostDriver) GetHypervisor() string { + return api.HYPERVISOR_H3C +} + +// 系统盘必须至少40G +func (self *SH3CHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error { + if sizeGb < 1 || sizeGb > 65536 { + return fmt.Errorf("The %s disk size must be in the range of 1G ~ 65536GB", storage.StorageType) + } + return nil +} + +func (self *SH3CHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { + if len(guests) >= 1 { + if disk.DiskType == api.DISK_TYPE_SYS { + for _, g := range guests { + if g.Status != api.VM_READY { + return nil, httperrors.NewBadRequestError("Server %s must in status ready", g.GetName()) + } + } + } else { + return nil, httperrors.NewBadRequestError("Disk must be detached") + } + } + return data, nil +} diff --git a/pkg/compute/regiondrivers/h3c.go b/pkg/compute/regiondrivers/h3c.go new file mode 100644 index 0000000000..946dc0f4a0 --- /dev/null +++ b/pkg/compute/regiondrivers/h3c.go @@ -0,0 +1,45 @@ +// 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 regiondrivers + +import ( + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/compute/models" +) + +type SH3CRegionDriver struct { + SHuaWeiRegionDriver +} + +func init() { + driver := SH3CRegionDriver{} + models.RegisterRegionDriver(&driver) +} + +func (self *SH3CRegionDriver) GetProvider() string { + return api.CLOUD_PROVIDER_H3C +} + +func (self *SH3CRegionDriver) IsSecurityGroupBelongVpc() bool { + return true +} + +func (self *SH3CRegionDriver) IsSupportedElasticcacheSecgroup() bool { + return false +} + +func (self *SH3CRegionDriver) GetMaxElasticcacheSecurityGroupCount() int { + return 1 +} diff --git a/vendor/modules.txt b/vendor/modules.txt index ff709c4c1c..2b0234e36b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1442,7 +1442,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.2.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309064841-9f842006b21d +# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230309073903-be630d4cb16b ## explicit; go 1.18 yunion.io/x/cloudmux/pkg/apis yunion.io/x/cloudmux/pkg/apis/billing diff --git a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/cloudaccount_const.go b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/cloudaccount_const.go index 36e338376d..bf368c4df1 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/cloudaccount_const.go +++ b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/cloudaccount_const.go @@ -39,6 +39,7 @@ const ( CLOUD_PROVIDER_INCLOUD_SPHERE = "InCloudSphere" CLOUD_PROVIDER_PROXMOX = "Proxmox" CLOUD_PROVIDER_REMOTEFILE = "RemoteFile" + CLOUD_PROVIDER_H3C = "H3C" CLOUD_PROVIDER_GENERICS3 = "S3" CLOUD_PROVIDER_CEPH = "Ceph" diff --git a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/guest_const.go b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/guest_const.go index 7e6b67ce4f..c33c5948c3 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/guest_const.go +++ b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/guest_const.go @@ -72,6 +72,7 @@ const ( HYPERVISOR_INCLOUD_SPHERE = "incloudsphere" HYPERVISOR_PROXMOX = "proxmox" HYPERVISOR_REMOTEFILE = "remotefile" + HYPERVISOR_H3C = "h3c" ) const ( diff --git a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/host_const.go b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/host_const.go index c310549600..a0dc3cd3be 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/apis/compute/host_const.go +++ b/vendor/yunion.io/x/cloudmux/pkg/apis/compute/host_const.go @@ -39,6 +39,7 @@ const ( HOST_TYPE_INCLOUD_SPHERE = "incloudsphere" HOST_TYPE_PROXMOX = "proxmox" HOST_TYPE_REMOTEFILE = "remotefile" + HOST_TYPE_H3C = "h3c" // # possible status HOST_ONLINE = "online"