From d618f61c169899d9187647990ea8af93c386e39b Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 13 Oct 2020 13:01:14 +0800 Subject: [PATCH 1/5] feat(esxiagent): keep data disks when cloning vm --- pkg/hostman/storageman/storage_agent.go | 5 ++--- pkg/multicloud/esxi/host.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/hostman/storageman/storage_agent.go b/pkg/hostman/storageman/storage_agent.go index 57b3e6f61b..d793825a54 100644 --- a/pkg/hostman/storageman/storage_agent.go +++ b/pkg/hostman/storageman/storage_agent.go @@ -310,10 +310,9 @@ func (as *SAgentStorage) AgentDeployGuest(ctx context.Context, data interface{}) } array := jsonutils.NewArray() - diskArray, _ := desc.GetArray("disks") - for idx, d := range disks { + for _, d := range disks { disk := d.(*esxi.SVirtualDisk) - diskId, _ := diskArray[idx].GetString("disk_id") + diskId := disk.GetId() diskDict := jsonutils.NewDict() diskDict.Add(jsonutils.NewString(diskId), "disk_id") diskDict.Add(jsonutils.NewString(disk.GetGlobalId()), "uuid") diff --git a/pkg/multicloud/esxi/host.go b/pkg/multicloud/esxi/host.go index 3b8fe70565..07c3d15381 100644 --- a/pkg/multicloud/esxi/host.go +++ b/pkg/multicloud/esxi/host.go @@ -1008,15 +1008,15 @@ func (host *SHost) CloneVM(ctx context.Context, from *SVirtualMachine, ds *SData log.Infof("resize system disk: %dGB => %dGB", from.vdisks[0].GetDiskSizeMB()/1024, vdisk.CapacityInKB/1024/1024) } // remove extra disk - for i := 1; i < len(from.vdisks); i++ { - dev := from.vdisks[i].dev - spec := &types.VirtualDeviceConfigSpec{} - spec.Operation = types.VirtualDeviceConfigSpecOperationRemove - spec.Device = dev - spec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy - deviceChange = append(deviceChange, spec) - log.Debugf("remove disk, index: %d", i) - } + // for i := 1; i < len(from.vdisks); i++ { + // dev := from.vdisks[i].dev + // spec := &types.VirtualDeviceConfigSpec{} + // spec.Operation = types.VirtualDeviceConfigSpecOperationRemove + // spec.Device = dev + // spec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy + // deviceChange = append(deviceChange, spec) + // log.Debugf("remove disk, index: %d", i) + // } } dc, err := host.GetDatacenter() From e43c86586b0550bf02aea883886dd8a02a74843d Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 13 Oct 2020 13:02:17 +0800 Subject: [PATCH 2/5] fix(esxiagent): check whether regexp is nil --- pkg/multicloud/esxi/datacenter.go | 2 +- pkg/multicloud/esxi/storagecache.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/multicloud/esxi/datacenter.go b/pkg/multicloud/esxi/datacenter.go index 502a152593..f089b17f01 100644 --- a/pkg/multicloud/esxi/datacenter.go +++ b/pkg/multicloud/esxi/datacenter.go @@ -287,7 +287,7 @@ func (dc *SDatacenter) fetchFakeTemplateVMs(movms []mo.VirtualMachine, regex str objs := make([]types.ManagedObjectReference, 0) for i := range movms { name := movms[i].Name - if tNameRegex.MatchString(name) { + if tNameRegex != nil && tNameRegex.MatchString(name) { objs = append(objs, movms[i].Reference()) } } diff --git a/pkg/multicloud/esxi/storagecache.go b/pkg/multicloud/esxi/storagecache.go index c3923edb24..b4eb3ce3af 100644 --- a/pkg/multicloud/esxi/storagecache.go +++ b/pkg/multicloud/esxi/storagecache.go @@ -100,6 +100,9 @@ func (self *SDatastoreImageCache) getTempalteVMs() ([]*SVirtualMachine, error) { } func (self *SDatastoreImageCache) getFakeTempateVMs() ([]*SVirtualMachine, error) { + if tempalteNameRegex == nil { + return nil, nil + } return self.datastore.FetchFakeTempateVMs("") } From 281d1c45df1007dd4deed7e94bf0289eea1d73f4 Mon Sep 17 00:00:00 2001 From: Rain Date: Tue, 13 Oct 2020 15:42:30 +0800 Subject: [PATCH 3/5] fix: support fake tempalte when searching template vm --- pkg/multicloud/esxi/manager.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/multicloud/esxi/manager.go b/pkg/multicloud/esxi/manager.go index b8e5cdfb5b..6c9b4018e0 100644 --- a/pkg/multicloud/esxi/manager.go +++ b/pkg/multicloud/esxi/manager.go @@ -351,7 +351,6 @@ func (cli *SESXiClient) scanAllMObjects(props []string, dst interface{}) error { func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) { filter := property.Filter{} - filter["config.template"] = true filter["summary.config.uuid"] = id var movms []mo.VirtualMachine err := cli.scanMObjectsWithFilter(cli.client.ServiceContent.RootFolder, VIRTUAL_MACHINE_PROPS, &movms, filter) @@ -362,6 +361,9 @@ func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) { return nil, errors.ErrNotFound } vm := NewVirtualMachine(cli, &movms[0], nil) + if !vm.IsTemplate() { + return nil, errors.ErrNotFound + } dc, err := vm.fetchDatacenter() if err != nil { return nil, errors.Wrap(err, "fetchDatacenter") From 335c8eecc06a071e040f72ed4d29f515b6f444e6 Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 15 Oct 2020 10:40:00 +0800 Subject: [PATCH 4/5] feat: change templateid as / in esxi --- pkg/cloudcommon/db/namevalidator.go | 4 ++++ pkg/compute/guestdrivers/esxi.go | 7 ++++++- pkg/compute/models/cachedimages.go | 6 +++++- pkg/multicloud/esxi/manager.go | 3 ++- pkg/multicloud/esxi/template.go | 19 ++++++++++++++++++- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/cloudcommon/db/namevalidator.go b/pkg/cloudcommon/db/namevalidator.go index e4d3aa18dc..f8af1ba840 100644 --- a/pkg/cloudcommon/db/namevalidator.go +++ b/pkg/cloudcommon/db/namevalidator.go @@ -87,6 +87,10 @@ func alterNameValidator(model IModel, name string) error { return nil } +func GenerateAlertName(model IModel, hint string) (string, error) { + return GenerateName2(model.GetModelManager(), nil, hint, model, 1) +} + func GenerateName(manager IModelManager, ownerId mcclient.IIdentityProvider, hint string) (string, error) { return GenerateName2(manager, ownerId, hint, nil, 1) } diff --git a/pkg/compute/guestdrivers/esxi.go b/pkg/compute/guestdrivers/esxi.go index 2ef110797f..6f5e569197 100644 --- a/pkg/compute/guestdrivers/esxi.go +++ b/pkg/compute/guestdrivers/esxi.go @@ -192,8 +192,13 @@ func (self *SESXiGuestDriver) GetJsonDescAtHost(ctx context.Context, userCred mc var hostIp string storageCacheHost, err := storageCaches[0].GetHost() if err != nil { - log.Errorf("fail to GetHost of storageCache %s", storageCaches[0].Id) + log.Errorf("unable to GetHost of storageCache %s: %v", storageCaches[0].Id, err) hostIp = storageCaches[0].ExternalId + } else if storageCacheHost == nil { + log.Errorf("unable to GetHost of storageCache %s: result is nil", storageCaches[0].Id) + hostIp = storageCaches[0].ExternalId + } else { + hostIp = storageCacheHost.AccessIp } hostIp = storageCacheHost.AccessIp imageInfo := SEsxiImageInfo{ diff --git a/pkg/compute/models/cachedimages.go b/pkg/compute/models/cachedimages.go index ddcbde5700..68f495b738 100644 --- a/pkg/compute/models/cachedimages.go +++ b/pkg/compute/models/cachedimages.go @@ -472,7 +472,11 @@ func (self *SCachedimage) canDeleteLastCache() bool { func (self *SCachedimage) syncWithCloudImage(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, image cloudprovider.ICloudImage, managerId string) error { diff, err := db.UpdateWithLock(ctx, self, func() error { - self.Name = image.GetName() + newName, err := db.GenerateAlertName(self, image.GetName()) + if err != nil { + return errors.Wrap(err, "GenerateAlertName") + } + self.Name = newName self.Size = image.GetSizeByte() self.ExternalId = image.GetGlobalId() self.ImageType = image.GetImageType() diff --git a/pkg/multicloud/esxi/manager.go b/pkg/multicloud/esxi/manager.go index 6c9b4018e0..e2fe25adef 100644 --- a/pkg/multicloud/esxi/manager.go +++ b/pkg/multicloud/esxi/manager.go @@ -351,7 +351,8 @@ func (cli *SESXiClient) scanAllMObjects(props []string, dst interface{}) error { func (cli *SESXiClient) SearchTemplateVM(id string) (*SVirtualMachine, error) { filter := property.Filter{} - filter["summary.config.uuid"] = id + uuid := toTemplateUuid(id) + filter["summary.config.uuid"] = uuid var movms []mo.VirtualMachine err := cli.scanMObjectsWithFilter(cli.client.ServiceContent.RootFolder, VIRTUAL_MACHINE_PROPS, &movms, filter) if err != nil { diff --git a/pkg/multicloud/esxi/template.go b/pkg/multicloud/esxi/template.go index 717957cc16..9963511140 100644 --- a/pkg/multicloud/esxi/template.go +++ b/pkg/multicloud/esxi/template.go @@ -16,6 +16,8 @@ package esxi import ( "context" + "fmt" + "strings" "time" "yunion.io/x/jsonutils" @@ -42,8 +44,23 @@ func NewVMTemplate(vm *SVirtualMachine, cache *SDatastoreImageCache) *SVMTemplat } } +const splitStr = "/" + +func toTemplateUuid(templateId string) string { + ids := strings.Split(templateId, splitStr) + if len(ids) == 1 { + return ids[0] + } + return ids[1] +} + +func toTemplateId(providerId string, templateUuid string) string { + return fmt.Sprintf("%s%s%s", providerId, splitStr, templateUuid) +} + func (t *SVMTemplate) GetId() string { - return t.uuid + providerId := t.vm.manager.cpcfg.Id + return toTemplateId(providerId, t.uuid) } func (t *SVMTemplate) UEFI() bool { From 7ab4decab36372c439b2001edafd01212a7c8127 Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 15 Oct 2020 10:41:04 +0800 Subject: [PATCH 5/5] feat: add 'vlan_id' param when creating network --- pkg/apis/compute/network.go | 6 ++++++ pkg/compute/models/networks.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/apis/compute/network.go b/pkg/apis/compute/network.go index f6b6ede8a9..4292953be7 100644 --- a/pkg/apis/compute/network.go +++ b/pkg/apis/compute/network.go @@ -187,6 +187,12 @@ type NetworkCreateInput struct { // 是否加入自动分配地址池 IsAutoAlloc *bool `json:"is_auto_alloc"` + + // VlanId + VlanId *int `json:"vlan_id"` + + // deprecated + Vlan *int `json:"vlan" yunion-deprecated-by:"vlan_id"` } type NetworkDetails struct { diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index a47ba2b649..45cd61c203 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -1333,6 +1333,20 @@ func (manager *SNetworkManager) ValidateCreateData(ctx context.Context, userCred return input, httperrors.NewInputParameterError("Invalid server_type: %s", input.ServerType) } + { + defaultVlanId := 1 + + if input.VlanId == nil { + input.VlanId = &defaultVlanId + } else if *input.VlanId < 1 { + input.VlanId = &defaultVlanId + } + + if *input.VlanId > 4095 { + return input, httperrors.NewInputParameterError("valid vlan id") + } + } + { if len(input.IfnameHint) == 0 { input.IfnameHint = input.Name