Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

vSphere Refactoring code - datastore object #158

Merged
merged 6 commits into from
Jun 16, 2017

Conversation

divyenpatel
Copy link

This PR is part of refactoring changes for datastore object.

It includes volume creation and volume deletion methods. Implemented factory pattern and kept the code for each approach in separate files.

@luomiao @rohitjogvmw @tusharnt @BaluDontu

}

// IsVSANDatastore checks the Datastore object is of the type VSAN
func (ds Datastore) IsVSANDatastore(ctx context.Context) (bool, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, instead of having IsVSANDatastore lets change it to GetType and consumers can use it like

if (Datastore.GetTyoe() == VSANDatastore) {
.
.
.
}

In future I am sure Datastore. GetType() will be required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to

// GetType returns the type of datastore
func (ds *Datastore) GetType(ctx context.Context) (string, error) {

}

// Disk defines interfaces for creating disk
type Disk interface {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, Disk APIs needs to be in Disk file and not in datastore.
Datastore should be a parameter to create.
For delete VolumeID / Path is sufficient.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created new Object VirtualDisk and moved methods there

type VirtualDisk struct {
	DiskPath      string
	VolumeOptions VolumeOptions
	VMOptions     VMOptions
}

func (disk *VirtualDisk) CreateVirtualDisk(ctx context.Context, datastore *Datastore) (err error) {

func (disk *VirtualDisk) DeleteVolume(ctx context.Context, datastore *Datastore) error {

@divyenpatel
Copy link
Author

@rohitjogvmw Addressed review comments.

}

// GetDiskManager returns vmDiskManager or vdmDiskManager based on given volumeoptions
func GetDiskManager(disk *VirtualDisk) Disk {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you pass operation along with virtualDisk then for delete also you can make use of the factory.
IMHO below parameters in factory with dummy implementation will be extensible..

func GetDiskManager(disk *VirtualDisk, op VirtualDiskOperation) Disk {
if op == "create" {
// your current code
} else if op == "delete" {
// currently just return VDM but very soon VStorageObjectManager needs to be returned based on disk.diskId
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated as below

// GetDiskManager returns vmDiskManager or vdmDiskManager based on given volumeoptions
func GetDiskManager(disk *VirtualDisk, diskOperation string) Disk {
	var diskManager Disk
	switch diskOperation {
	case VirtualDeleteOperation:
		diskManager = vdmDiskManager{disk.DiskPath, disk.VolumeOptions}
	case VirtualDiskCreateOperation:
		if disk.VolumeOptions.StoragePolicyName != "" || disk.VolumeOptions.VSANStorageProfileData != "" || disk.VolumeOptions.StoragePolicyID != "" {
			diskManager = vmDiskManager{disk.DiskPath, disk.VolumeOptions, disk.VMOptions}
		} else {
			diskManager = vdmDiskManager{disk.DiskPath, disk.VolumeOptions}
		}
	}
	return diskManager
}

// DeleteVolume deletes a disk at given disk path.
func (disk *VirtualDisk) DeleteVolume(ctx context.Context, datastore *Datastore) error {
// Create a virtual disk manager
virtualDiskManager := object.NewVirtualDiskManager(datastore.Client())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will suggest the diskManager from the factory and implement delete only in vdm.go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to vdm.go

"github.com/vmware/govmomi/vim25/types"
)

type vdmDiskManager struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vdm itself stands for VirtualDiskManager.
Can I suggest 2 things:

  1. Put all disk managers in vclib/diskManagers
  2. Define one interface for diskManager havig APIs like create, delete etc. and vdm and vmDiskManager will implement them or throw exception if operation is not supported. e.g delete is not supported in vmDiskManager
  3. rename vdmDiskManager.go to vdm.go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created diskmanagers package under vclib and moved files there.

divyenp-m01:diskmanagers divyenp$ ls
vdm.go		virtualdisk.go	vmdm.go

ExtensionKey: "com.vmware.vim.sps",
ObjectData: vmdisk.volumeOptions.VSANStorageProfileData,
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please return err in else.
if storagePolicyId != "" {
} else if VSANStorageProfileData != "" {
} else {
return err;
}

VM based disk creation need to be used if no policy / vsan policy data is not provided

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@divyenpatel
Copy link
Author

@rohitjogvmw addressed 2nd round of review comments.

)

// Disk defines interfaces for creating disk
type Disk interface {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be VirtualDiskProvider and not Disk?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed Disk to VirtualDiskProvider

"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
)

type vdmDiskManager struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think VirtualDiskManager is better than vdmDiskManager.
vdm itself stands for VirtualDiskManager. so vdmDiskManager will be read as "VirtualDiskManagerDiskManager" isn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. let me change it to virtualDiskManager.
will be read as diskManager virtualDiskManager


// GetType returns the type of datastore
func (ds *Datastore) GetType(ctx context.Context) (string, error) {
var mds mo.Datastore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is mds?
Is there any particular convention being followed for MOs?

IMHO mds is not good for reading instead "dsMo" tells exactly what it holds.
But are we using Mo suffix for all MOs in code? I don't think so. We need to stick to some convention.
At other places I see name of that object with no "Mo" suffix (e.g. VirtualDiskManager).
If so please change input parameter of this function from "ds" to "dsRef" and use ds instead of mds.

Other alternatives are welcome.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is mds?
Is there any particular convention being followed for MOs?

Govmomi follows this to create instance of Datastore struct declared under package mo.
Used this to differentiate variable from object.Datastore and mo.Datastore

Copy link

@rjog rjog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@luomiao luomiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated:
General feedback is, I am not sure if we should use this VirtualDiskProvider interface for vdm and vmdm... Maybe my following understanding is wrong but still want to raise the concern.

The whole point of using interface is generalization. We create this interface X and there are two instances A and B. At some point, we initialize A or B and return it back as an instance of interface X. Later the upper layer just need to call X.something and it will land in the desired functions A.something or B.something automatically.

However in our case, for EVERY disk operation, we need to call GetDiskManager to create an instance of VirtualDiskProvider. And after this, it goes into vdm or vmdm's function, and it's done... The instances are never reused... This seems lost the whole point of using interface. If anyway we need to do this check every time, why not just make a check inside a Create function, and then add two sub functions? Using interface here is just a overkill (or not the right use case...)

"github.com/vmware/govmomi/object"
)

// VMOptions provides helper objects for provisioning volume with SPBM Policy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to add more functions to it?
If not, why not directly put the following three fields directly into vmDiskManager and VirtualDisk?


type virtualDiskManager struct {
diskPath string
volumeOptions vclib.VolumeOptions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalizing? DiskPath, VolumeOptions...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since virtualDiskManager and vmDiskManager are private and just implementing VirtualDiskProvider, so kept it private.

Data can be accessed using

VirtualDisk.DiskPath
VirtualDisk.VolumeOptions
VirtualDisk.VMOptions 

"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
)

type vmDiskManager struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalizing again. DiskPath, VolumeOptions, VmOptions...

"github.com/vmware/govmomi/vim25/types"
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is vdm and vmdm. Maybe add one line description about them at the beginning to help ppl understand what's the difference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one line description for each.

// virtualDiskManager implements VirtualDiskProvider Interface for creating and deleting volume using VirtualDiskManager

// vmDiskManager implements VirtualDiskProvider interface for creating volume using Virtual Machine Reconfigure approach

@divyenpatel
Copy link
Author

Addressing @luomiao 's review comments.
Will keep getDiskManager private
func getDiskManager(disk *VirtualDisk, diskOperation string) VirtualDiskProvider {

and VirtualDisk will now internally get DiskManager, instead of exposing to the cloud provider.

func (virtualDisk *VirtualDisk) Create(ctx context.Context, datastore *vclib.Datastore) error {
	return getDiskManager(virtualDisk,VirtualDiskCreateOperation).Create(ctx, datastore)
}

func (virtualDisk *VirtualDisk) Delete(ctx context.Context, datastore *vclib.Datastore) error {
	return getDiskManager(virtualDisk,VirtualDeleteOperation).Delete(ctx, datastore)
}

@divyenpatel
Copy link
Author

@luomiao Addressed review comments.

@luomiao
Copy link

luomiao commented Jun 16, 2017

LGTM

@divyenpatel divyenpatel merged commit 3f6b55a into refactored-code Jun 16, 2017
BaluDontu pushed a commit that referenced this pull request Jun 17, 2017
* adding VCLib package - Datastore object

* adding vmoptions.go

* addressed Rohit's review comments

* addressed rohit's review comments

* addressed 3rd round of review comments

* addressed Miao's review comments
divyenpatel added a commit that referenced this pull request Jul 13, 2017
* adding VCLib package - Datastore object

* adding vmoptions.go

* addressed Rohit's review comments

* addressed rohit's review comments

* addressed 3rd round of review comments

* addressed Miao's review comments
divyenpatel added a commit that referenced this pull request Jul 14, 2017
* adding VCLib package - Datastore object

* adding vmoptions.go

* addressed Rohit's review comments

* addressed rohit's review comments

* addressed 3rd round of review comments

* addressed Miao's review comments
@divyenpatel divyenpatel deleted the divyen-refactoring-datastore branch September 5, 2017 23:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants