Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add instant clone #1516

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Anant99-sys
Copy link

Description

Implementation for instant clone functionality

  • The Instant Clone technology is used to create powered-on virtual machines from the running state of another powered-on virtual machine.
  • The result of an Instant Clone operation is a new virtual machine that is identical to the source virtual machine.
  • With Instant Clone you can create new virtual machines from a controlled point in time.
    Instant cloning is very convenient for large-scale application deployments because it ensures memory efficiency and allows for creating numerous virtual machines on a single host.
  • This functionality can be used for Creating a powered-on Instant Clone of a virtual machine.

Acceptance tests

  • Have you added an acceptance test for the functionality being added?
  • Have you run the acceptance tests on this branch?

Output from acceptance testing:

$ make testacc TESTARGS="-run=TestAccResourceVSphereVirtualMachine_Instantclone"

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestAccResourceVSphereVirtualMachine_Instantclone -timeout 240m
?   	github.com/hashicorp/terraform-provider-vsphere	[no test files]
=== RUN   TestAccResourceVSphereVirtualMachine_Instantclone
--- PASS: TestAccResourceVSphereVirtualMachine_Instantclone (188.07s)
PASS
ok  	github.com/hashicorp/terraform-provider-vsphere/vsphere	191.274s
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/administrationroles	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/clustercomputeresource	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/computeresource	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/contentlibrary	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/customattribute	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datacenter	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datastore	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/dvportgroup	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/envbrowse	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/folder	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/hostsystem	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/network	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/nsx	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/ovfdeploy	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/provider	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/resourcepool	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/spbm	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/storagepod	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/testhelper	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/utils	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/vappcontainer	[no test files]
testing: warning: no tests to run
PASS
ok  	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/viapi	(cached) [no tests to run]
testing: warning: no tests to run
PASS
ok  	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/virtualdisk	(cached) [no tests to run]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine	[no test files]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/vsansystem	[no test files]
testing: warning: no tests to run
PASS
ok  	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/virtualdevice	(cached) [no tests to run]
?   	github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/vmworkflow	[no test files]

...

Release Note

Release note for CHANGELOG:

Capability to Add Instant Clone for a Parent VM.

References

Old PR: #1371

@github-actions github-actions bot added the size/l Relative Sizing: Large label Nov 15, 2021
@Anant99-sys Anant99-sys mentioned this pull request Nov 15, 2021
2 tasks
@Anant99-sys
Copy link
Author

@redeux @markpeek @aareet @mfilipiak-ccg The Old PR CLA is resolved. Please take a look

@appilon appilon self-assigned this Dec 17, 2021
@tenthirtyam tenthirtyam added the enhancement Type: Enhancement label Feb 3, 2022
@tenthirtyam tenthirtyam changed the title feature: instant_clone Add support for instant clone Feb 3, 2022
@tenthirtyam tenthirtyam added the documentation Type: Documentation label Feb 3, 2022
@tenthirtyam
Copy link
Collaborator

@Anant99-sys, taking a moment to quickly scan the pull request and noticed that the vsphere_virtual_machine resource documentation will also need to be updated with context for this option for completeness.

Ryan Johnson
Staff II Solutions Architect, VMware, Inc.

@Anant99-sys
Copy link
Author

eature: instant_clone

@tenthirtyam Can you please guide me as to what to do for updating the documentation for vsphere_virtual_machine?

@tenthirtyam tenthirtyam added the needs-review Status: Pull Request Needs Review label Feb 8, 2022
@tenthirtyam tenthirtyam self-assigned this Feb 10, 2022
@tenthirtyam tenthirtyam self-requested a review February 10, 2022 22:15
@tenthirtyam tenthirtyam added this to the Research milestone Feb 14, 2022
@tenthirtyam tenthirtyam added area/storage Area: Storage area/vm Area: Virtual Machines labels Feb 22, 2022
@tenthirtyam tenthirtyam modified the milestones: Research, v2.2.0 Feb 24, 2022
@tenthirtyam
Copy link
Collaborator

@Anant99-sys, I'll follow up over Slack next week to discuss this feature with you along with the documentation and tests.

@tenthirtyam tenthirtyam changed the title Add support for instant clone feat: Add support for instant clone Mar 11, 2022
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

CheckDestroy: testAccResourceVSphereVirtualMachineCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereVirtualMachineConfigInstant(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

UndeclaredName

image

Copy link
Author

Choose a reason for hiding this comment

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

So what change should I do regarding this?

Copy link
Collaborator

@tenthirtyam tenthirtyam left a comment

Choose a reason for hiding this comment

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

Hi @Anant99-sys,

For the updates to the documentation, update the website/docs/r/virtual_machine.html.markdown file with the information for the instant_clone argument.

The options available in the `clone` block are:
* `template_uuid` - (Required) The UUID of the source virtual machine or template.
* `linked_clone` - (Optional) Clone the virtual machine from a snapshot or a template. Default: `false`.
* `timeout` - (Optional) The timeout, in minutes, to wait for the cloning process to complete. Default: 30 minutes.
* `customize` - (Optional) The customization spec for this clone. This allows the user to configure the virtual machine post-clone. For more details, see [virtual machine customizations](#virtual-machine-customizations).

Ryan Johnson
Staff II Solutions Architect | VMware, Inc.

Copy link
Collaborator

@tenthirtyam tenthirtyam left a comment

Choose a reason for hiding this comment

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

Not critical, but we may need to limit to 6.7 and higher, akin to the following for instant_clone:

version := viapi.ParseVersionFromClient(client)
  if version.Newer(viapi.VSphereVersion{Product: version.Product, Major: 6, Minor: 7}) {
  ....
  }
  return obj

But something easy to remove after the end of 6.x support.

@github-actions github-actions bot added the provider Type: Provider label Mar 22, 2022
@tenthirtyam
Copy link
Collaborator

Resolved conflicts and errors; all checks have now passed.

Ready for testing once again.

Ryan Johnson
Senior Staff Solutions Architect | Product Engineering @ VMware, Inc.

@tenthirtyam tenthirtyam added the do-not-merge Status: Draft, Do Not Merge label Sep 11, 2022
@tenthirtyam tenthirtyam marked this pull request as draft September 11, 2022 21:49
@tenthirtyam tenthirtyam modified the milestones: Backlog, v2.4.0 Sep 11, 2022
@FrancescoCrecchi
Copy link

Hi, any updates on this?

@tenthirtyam
Copy link
Collaborator

Hi, any updates on this?

Please refer to the tentative milestone assignment.

@tenthirtyam tenthirtyam modified the milestones: v2.4.0, v2.5.0 May 4, 2023
@tenthirtyam tenthirtyam modified the milestones: v2.5.0, Backlog Jul 25, 2023
@tenthirtyam tenthirtyam removed their assignment Aug 7, 2023
@tenthirtyam tenthirtyam changed the title feat: Add support for instant clone feat: add support for instant clone Oct 15, 2023
Copy link
Contributor

@vasilsatanasov vasilsatanasov left a comment

Choose a reason for hiding this comment

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

In general it looks working, except the comments which need to be addressed.

_about/tf-vsphere-devrc.mk.example Outdated Show resolved Hide resolved
@@ -2563,6 +2581,39 @@ func testAccResourceVSphereVirtualMachinePreCheck(t *testing.T) {
}
}

func testAccResourceVSphereVirtualMachinePreInstantCloneCheck(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It was not necessary the entire testAccResourceVSphereVirtualMachinePreCheck function to be copied, a single function containing just the check
if os.Getenv("TF_VAR_VSPHERE_USE_INSTANT_CLONE") == "" { t.Skip("set TF_VAR_VSPHERE_USE_INSTANT_CLONE to run vsphere_virtual_machine acceptance tests") } could be implemeted and added in the preCheck property like for example it is done in TestAccResourceVSphereVirtualMachine_basic


clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"
instant_clone = "${var.instant_clone != "" ? "true" : "false" }"
Copy link
Contributor

Choose a reason for hiding this comment

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

instant_clone should be explicitly set to true since the test will be skipped in every case if the TF_VAR_VSPHERE_USE_INSTANT_CLONE is set to something falsy

@@ -3418,6 +3469,16 @@ func testAccResourceVSphereVirtualMachineConfigBase() string {
testhelper.ConfigDataRootPortGroup1())
}

func testAccResourceVSphereVirtualMachineInstantCloneConfigBase() string {
return testhelper.CombineConfigs(
Copy link
Contributor

Choose a reason for hiding this comment

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

A minimal set of required resources should be specified here

// InstantClone wraps the creation of a virtual machine instantly and the subsequent waiting of
// the task. A higher-level virtual machine object is returned.
func InstantClone(c *govmomi.Client, src *object.VirtualMachine, f *object.Folder, name string, spec types.VirtualMachineInstantCloneSpec, timeout int) (*object.VirtualMachine, error) {
log.Printf("[DEBUG] Instant Cloning virtual machine %q", fmt.Sprintf("%s/%s", f.InventoryPath, name))
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the Folder pointer and the source VM name are used only for logging and not for the logic inside the function the logging should be done outside the function call / the logging should be changed not to use these variables

}

resource "vsphere_virtual_machine" "vm" {
name = "testacc-test"
Copy link
Contributor

Choose a reason for hiding this comment

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

Use minumum required set of properties for the test, for example the hardware like CPU, Memory, Disk are not necessary since a vm is just being copied and those cant be changed + what will happen if the values in the HCL are different then the values from the template VM ?

@tenthirtyam tenthirtyam added the stale Status: Stale label Feb 12, 2024
@tenthirtyam tenthirtyam force-pushed the instant-module branch 2 times, most recently from 2498f89 to ad02e3b Compare March 6, 2024 17:14
@tenthirtyam tenthirtyam removed the stale Status: Stale label Mar 6, 2024
@tenthirtyam
Copy link
Collaborator

@Anant99-sys - could you review the current comments? Perhaps we can put this one forward now...

@tenthirtyam tenthirtyam changed the title feat: add support for instant clone feat: add instant clone Mar 6, 2024
Adds support for instant clones.
return nil, fmt.Errorf("no storage DRS recommendations were found for the requested action (type: %q)", sps.Type)
}
// result to pin disks to recommended datastores
ds := recs[0].Action[0].(*types.StoragePlacementAction).Destination
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would suggest checking len(recs[0].Action) before accessing.
If it is always populated by design you can add a comment, otherwise it looks like a panic-prone line

"instant_clone": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether or not to create a instant clone when cloning. When this option is used, the source VM must be in a running state.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: "..an instant.."

* `linked_clone` - (Optional) Clone the virtual machine from a snapshot or a template. Default: `false`.
* `instant_clone` - (Optional) Clone the virtual machine from a running powered on virtual machine. Supported on vSphere 6.7 and later. Default: `false`. Only one of `instant_clone` or `linked_clone` can be set to `true`.

* `linked_clone` - (Optional) Clone the virtual machine from a snapshot or a template. Default: `false`. Only one of `linked_clone` or `instant_clone` can be set to `true`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The fact that the two clone modes are mutually exclusive is not actually enforced.
By the looks of it if both are set to true the provider will attempt to do an instant clone.

Just a suggestion - it would be great if you can figure out a neat way to validate that the user is not trying to apply an invalid configuration

@tenthirtyam tenthirtyam removed the documentation Type: Documentation label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/storage Area: Storage area/vm Area: Virtual Machines do-not-merge Status: Draft, Do Not Merge enhancement Type: Enhancement needs-review Status: Pull Request Needs Review provider Type: Provider size/xl Relative Sizing: Extra-Large
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants