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

refactor: only add / remove minimul number of cdroms #355

Conversation

Hi-Angel
Copy link
Contributor

In PR #344 a feature to reuse an existing VM is being added. As part of that feature I had to factor out from AddCdrom the logic of creating a CD-rom as opposed to just mounting an image. Recently merged feature reattach_cdroms turned out to be a new user of the older logic, which made rebasing #344 non-trivial.

In this PR I refactor the reattach_cdroms logic to avoid unnecessarily calling EjectCdroms, RemoveCdroms and AddCdrom by calculating amount of CDroms that we want to have added/removed, and only calling the mentioned functions for that amount (see the last commit).

This is a useful change in itself, disregarding whether #344 exists, so I'm sending it separately from the other PR. And it should simplify rebasing #344 later.

@Hi-Angel Hi-Angel requested a review from a team as a code owner January 15, 2024 14:17
@Hi-Angel Hi-Angel force-pushed the separate-cdrom-creation-n-mount-have-old-AddCdrom branch 2 times, most recently from dedd2d5 to 017f49a Compare January 15, 2024 14:24
@nywilken
Copy link
Member

@Hi-Angel thanks for opening up this quick refactor PR. I was planning to release the vSphere plugin with the latest changes.

Should I wait until this is merged? Or is the refactor for supporting #344

@Hi-Angel
Copy link
Contributor Author

I don't have any preference here. On one hand, I presume the code that is already in the repo has probably seen more testing than the new one, so regressions are less likely. On the other hand, this refactor reduces a bit calls which (per my understanding) communicate to ESXi via network, which is slow. So if you decide to wait for it to land, the new functional might be a tiny bit faster.

@nywilken
Copy link
Member

I don't have any preference here. On one hand, I presume the code that is already in the repo has probably seen more testing than the new one, so regressions are less likely. On the other hand, this refactor reduces a bit calls which (per my understanding) communicate to ESXi via network, which is slow. So if you decide to wait for it to land, the new functional might be a tiny bit faster.

@Hi-Angel that's a good call out on the testing part. We release every week, or shoot for it when we have pending changes. I think we can focus on getting this PR reviewed and merged for the next release to give the new feature time to setting.

@Hi-Angel
Copy link
Contributor Author

Hi-Angel commented Feb 1, 2024

Any news here?

@tenthirtyam tenthirtyam force-pushed the separate-cdrom-creation-n-mount-have-old-AddCdrom branch 2 times, most recently from 8eff8a9 to 19cfa45 Compare February 1, 2024 19:32
@tenthirtyam
Copy link
Collaborator

Any news here?

I've rebased from main and have squashed the commits. I'll review this w/in the next week.

@tenthirtyam tenthirtyam self-requested a review February 1, 2024 19:33
@tenthirtyam tenthirtyam added the chore Chore label Feb 1, 2024
@tenthirtyam tenthirtyam changed the title Refactor reattach_cdroms to only remove/add amount of CDroms that is necessary refactor: only add / remove minimul number of cdroms Feb 1, 2024
@Hi-Angel
Copy link
Contributor Author

Hi-Angel commented Feb 1, 2024

Any news here?

I've rebased from main and have squashed the commits. I'll review this w/in the next week.

Why have you squashed the commits before review? 😅 I mean, I know the project squashes them on merge for some reason, but don't you want for review purposes to see each functional change separated?

@tenthirtyam
Copy link
Collaborator

It's all good - I've read through them already.

Copy link
Member

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

Hi @Hi-Angel I finally got a chance to get back to you on this. I was heads down on a few other Packer related changes. I left a few nits and a suggestion to your RemoveCdroms function change. The code looks good overall but I found a few things hard to wrap my head around.

Give the suggestions a look over and let me know if you have any questions.

builder/vsphere/common/step_reattach_cdrom.go Outdated Show resolved Hide resolved
builder/vsphere/common/step_reattach_cdrom.go Outdated Show resolved Hide resolved
builder/vsphere/common/step_reattach_cdrom.go Outdated Show resolved Hide resolved
builder/vsphere/driver/vm_cdrom.go Outdated Show resolved Hide resolved
builder/vsphere/driver/vm_cdrom.go Outdated Show resolved Hide resolved
@tenthirtyam tenthirtyam marked this pull request as draft February 12, 2024 21:56
The `range` body is skipped anyway when len(ISOPaths) == 0.
This simplifies the code a bit, and it will also be useful later when
we add support for reusing a VM
@Hi-Angel Hi-Angel force-pushed the separate-cdrom-creation-n-mount-have-old-AddCdrom branch from 19cfa45 to 05e1a98 Compare February 13, 2024 07:32
@Hi-Angel
Copy link
Contributor Author

Everything seems addressed

@Hi-Angel Hi-Angel marked this pull request as ready for review February 13, 2024 08:09
Copy link
Member

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

@Hi-Angel then updated changes look good to me. After your explanation dropping Virtual from the function name makes senses. Thanks for responding so quickly to the review.

@tenthirtyam would you like to give this another set of eyes before I merge?

@tenthirtyam
Copy link
Collaborator

@tenthirtyam would you like to give this another set of eyes before I merge?

Will review and test as well and update you.

Comment on lines 86 to 87
// Bug in govmomi: can't batch-create cdroms and then mount ISO files, that
// resuls in wrong UnitNumber. So do that one-by-one.
Copy link
Collaborator

Choose a reason for hiding this comment

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

If calling out a bug (or is it actually a limitation) in vmware/govmomi, it would be ideal to include a reference so that this can be reviewed in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well… I currently don't have resources for creating a minimal testcase for this, so let's just rename this to a "limitation" if that's okay with you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed per comment above

// resuls in wrong UnitNumber. So do that one-by-one.
for _, path := range s.Config.ISOPaths {
if path == "" {
state.Put("error", fmt.Errorf("Invalid path: empty string"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
state.Put("error", fmt.Errorf("Invalid path: empty string"))
state.Put("error", fmt.Errorf("invalid path: empty string"))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

state.Put("error", fmt.Errorf("error adding sata controller: %v", err))
return multistep.ActionHalt
}
nActableCdroms := ReattachCDRom - len(s.CDRomConfig.ISOPaths)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nAttachableCdroms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

return multistep.ActionHalt
}
nActableCdroms := ReattachCDRom - len(s.CDRomConfig.ISOPaths)
if nActableCdroms < 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nAttachableCdroms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Comment on lines 70 to 71
// Add the CD-ROM devices to the image based on the value of `reattach_cdroms`.
// A valid ISO path is required for this step. The media will subsequently be ejected.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this still useful information?

Copy link
Contributor Author

@Hi-Angel Hi-Angel Feb 14, 2024

Choose a reason for hiding this comment

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

Hmm, well, I suppose I can put it before nAttachableCdroms calculation… I initially removed it because the whole point of the class and its method is to process reattach_cdroms option. I.e. if you are inside this function, then you already know what this code supposed to do.

But I suppose the comment wouldn't hurt either, so I put it back.

return multistep.ActionHalt
}
} else {
// Eject media from pre-existing CD-ROM devices.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Eject media from pre-existing CD-ROM devices.
// Eject media from the existing CD-ROM devices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

if err != nil {
state.Put("error", fmt.Errorf("error ejecting cdrom media: %v", err))
return multistep.ActionHalt
// create more CD-ROMs if required
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// create more CD-ROMs if required
// Add CD-ROMs, if required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

state.Put("error", fmt.Errorf("error ejecting cdrom media: %v", err))
return multistep.ActionHalt
// create more CD-ROMs if required
if nActableCdroms > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nAttachableCdroms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

}

ui.Say("Adding CD-ROM devices...")
for i := 0; i < nActableCdroms; i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nAttachableCdroms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

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.

Minor changes requested.

This commit changes the "reattach" logic so that instead of removing
all CDroms and recreating everything anew, we only remove the number
of CDroms that are excess, and only add new CDroms when there was not
enough pre-existing ones.
@Hi-Angel Hi-Angel force-pushed the separate-cdrom-creation-n-mount-have-old-AddCdrom branch from 05e1a98 to 860f710 Compare February 14, 2024 06:48
@nywilken nywilken dismissed tenthirtyam’s stale review February 21, 2024 18:24

I'm dismissing in order to merge. All requested changes have been made

@nywilken
Copy link
Member

Hi Folks, apologies I've been out sick. Given that @Hi-Angel has addressed all of your request @tenthirtyam I dismissed your last review in order to unblock the merge and pending rebase. We can revisit anything if needed but this looks good to go.

@nywilken nywilken merged commit 6c6667c into hashicorp:main Feb 21, 2024
12 checks passed
Hi-Angel added a commit to Hi-Angel/packer-plugin-vsphere that referenced this pull request Mar 18, 2024
This fixes regression introduced by the commit

     "common: deduplicate AddCdrom call by appending path to ISOPaths"

that was squashed into:

     "refactor: only add / remove minimul number of cdroms (hashicorp#355)"

It assumed that the order is not guaranteed and does not matter,
however the documentation says:

> If the "iso_url" is defined in addition to the "iso_paths", the
> "iso_url" is added to the VM first. This keeps the "iso_url" first
> in the boot order by default allowing the boot iso being defined by
> the iso_url and the vmware tools iso added from the datastore.

So fix the regression.

Fixes: hashicorp#386
Hi-Angel added a commit to Hi-Angel/packer-plugin-vsphere that referenced this pull request Mar 18, 2024
This fixes regression introduced by the commit

     "common: deduplicate AddCdrom call by appending path to ISOPaths"

that was squashed into:

     "refactor: only add / remove minimul number of cdroms (hashicorp#355)"

It assumed that the order is not guaranteed and does not matter,
however the documentation says:

> If the "iso_url" is defined in addition to the "iso_paths", the
> "iso_url" is added to the VM first. This keeps the "iso_url" first
> in the boot order by default allowing the boot iso being defined by
> the iso_url and the vmware tools iso added from the datastore.

So fix the regression.

Fixes: hashicorp#386

Signed-off-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Hi-Angel added a commit to Hi-Angel/packer-plugin-vsphere that referenced this pull request Mar 18, 2024
This fixes regression introduced by the commit

     "common: deduplicate AddCdrom call by appending path to ISOPaths"

that was squashed into:

     "refactor: only add / remove minimul number of cdroms (hashicorp#355)"

It assumed that the order is not guaranteed and does not matter,
however the documentation says:

> If the "iso_url" is defined in addition to the "iso_paths", the
> "iso_url" is added to the VM first. This keeps the "iso_url" first
> in the boot order by default allowing the boot iso being defined by
> the iso_url and the vmware tools iso added from the datastore.

So fix the regression.

Fixes: hashicorp#386

Signed-off-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
nywilken pushed a commit that referenced this pull request Mar 19, 2024
This fixes regression introduced by the commit

     "common: deduplicate AddCdrom call by appending path to ISOPaths"

that was squashed into:

     "refactor: only add / remove minimul number of cdroms (#355)"

It assumed that the order is not guaranteed and does not matter,
however the documentation says:

> If the "iso_url" is defined in addition to the "iso_paths", the
> "iso_url" is added to the VM first. This keeps the "iso_url" first
> in the boot order by default allowing the boot iso being defined by
> the iso_url and the vmware tools iso added from the datastore.

So fix the regression.

Fixes: #386

Signed-off-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Chore
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants