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

[V2] Migrate all builders to hashicorp/go-azure-sdk #326

Merged
merged 15 commits into from
Aug 11, 2023

Conversation

JenGoldstrich
Copy link
Contributor

@JenGoldstrich JenGoldstrich commented Aug 2, 2023

This PR combines several other PRs which remove the now deprecated "track 1" SDK we were using and switching over to the same SDK that the Terraform AzureRM provider uses https://github.com/hashicorp/go-azure-sdk

Removes VHD Deprecation warning

Removes use_interactive_auth from ARM/DTL builders, we recommend using the Azure CLI via az login --use-device-code and then using Azure CLI auth

adds winrm_expiration_time field to the ARM builder

#306 - ARM Builder
JenGoldstrich#4 - DTL Builder
JenGoldstrich#5 - Chroot Builder
JenGoldstrich#3 - Unwind template capture TODO
JenGoldstrich#6 - Add Expiry Winrm Field, fail fast on in valid SIG version, and poll consistently as new SDK expects

closes #243
closes #250
closes #235
closes #187
closes #315

@@ -112,13 +126,14 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) {
}
if !state.Get(constants.ArmKeepOSDisk).(bool) {
ui.Say(fmt.Sprintf(" Deleting -> %s : '%s'", imageType, imageName))
err = s.deleteDisk(context.TODO(), imageName, resourceGroupName, isManagedDisk)
err = s.deleteDisk(ctx, imageName, resourceGroupName, (isManagedDisk || isSIGImage), subscriptionId, armStorageAccountName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the change that fixes #315, since before SIG images would use the VHD deletion path

go.mod Outdated
github.com/hashicorp/hcl/v2 v2.16.2
github.com/hashicorp/packer-plugin-sdk v0.5.1
github.com/hashicorp/packer-plugin-sdk v0.4.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had downgraded temporarily this please let me know what version I should upgrade this back to for the release!

Copy link
Contributor

@nywilken nywilken Aug 4, 2023

Choose a reason for hiding this comment

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

When ready you will need github.com/hashicorp/packer-plugin-sdk v0.5.1 with the go-cty fix for the Azure plugin

go get github.com/hashicorp/packer-plugin-sdk@v0.5.1
make install-packer-sdc
packer-sdc fix .
go mod tidy

@@ -3,32 +3,32 @@ module github.com/hashicorp/packer-plugin-azure
go 1.19

require (
github.com/Azure/azure-sdk-for-go v64.0.0+incompatible
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🎉

Copy link
Contributor

@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.

Jenna this is looking good already. Thank you a 💯 times for taking on this work and doing such a great job leading the effort.

I believe most of these changes have been reviewed from the previous PRs correct?

I've slated time to go over the changes on Monday. To help with the review a bit could you rebase and squash the commits into logical PR merges? It would make it easy to cross reference.

go.mod Outdated
github.com/hashicorp/hcl/v2 v2.16.2
github.com/hashicorp/packer-plugin-sdk v0.5.1
github.com/hashicorp/packer-plugin-sdk v0.4.0
Copy link
Contributor

@nywilken nywilken Aug 4, 2023

Choose a reason for hiding this comment

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

When ready you will need github.com/hashicorp/packer-plugin-sdk v0.5.1 with the go-cty fix for the Azure plugin

go get github.com/hashicorp/packer-plugin-sdk@v0.5.1
make install-packer-sdc
packer-sdc fix .
go mod tidy

Rebasing entire ARM builder migration on top of main

Remove some missed old SDK stuff, including the autorest To package from ARM Builder

Update oidc-example.json

Pass SIG ID subscription through to state

Use Hamilton MSGraph SDK to fetch Object ID for Windows builds

Format and add comment

commit generated code

Switch AuthType back to private variable and use helper function to get it, switch from MeClient to ServicePrincipals client to get objectID

Print MSgraph error again

Don't use Hamilton, get it from the Access Token

fmt and pass in correct context on cleanup

Mess with backoff time while testing to see if this can be a bit smoother of a process for users

Don't use context.TODOs in ARM Builder client calls

Use hcl file in oidc test

Use Giovanni instead of Blob Container serivce

Implement new SDK environment logic

Address fmt

Remove unused function, go mod tidy

Switch to forked release process
…ing to create it

Implement Lucas's feedback

Make generate
=======

DTL almost done! Just need to get VMID instead of capture template method and start testing

Port DTL provisioner to new SDK

Make generate

Accidently commited playing around with this field, but was missing comma

Appease the lint lord

DTL never supported VHDs, it just had all the code of it, so I didn't need to port some of this

Use retry config instead of an infinite loop

Delete more unused code

Update comments

Address linter
====
EOD WIP: I've ported all the client code over, on Monday I will test that it works in a VM before porting the unit tests as the mocking strategy used is heavily reliant on auto rest, and I want to port it to stretchify mocks that don't care as much about the autorest specifics

Don't use Subscription ID as Snapshot URL

Large commit: Chroot unit tests passing, azure-sdk-for-go no longer imported by go.mod, remove references to device code, clear up missing pieces of DTL builder

Fix linux disk attatcher

Remove unnused mock that I added earlier

Re-add Fetching of Tenant ID

Forgot to use New Steps

null check was wrong way for SDK model

see above

Don't try and get tenant ID if we're using CLI auth

Address linter, fix linux test

StepCreateNewDiskTest -> Return proper autorest future fake

remove device code acceptance tests, handle flakey unit test by sorting resulting objects to insure consistant comparison

Update builder/azure/chroot/step_verify_source_disk_test.go

Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com>

Remove CHRoot Acceptance Test && rename all hashi* imports removing prefix

Make generate and fix lint

Readd subscription, tenant ID fetching for CLI auth, so we know which subscription to save stuff to, this caused acceptance test failures which this commit fixes

Lint
…ntext timeout throughout codebase

Reset readme, version file, and workflows to state in main origin
[chroot] PollingDelay != Polling Duration >_>
@JenGoldstrich
Copy link
Contributor Author

@nywilken done and done!

@@ -526,27 +526,6 @@ prevent running afoul of Azure decency controls.
The password alphabet used for random values is
**0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ**.

### Deprecation Warning
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will post a longer clarification on the removal of the deprecation warning for VHDs in the release notes for v2.0.0

Copy link
Contributor

@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.

This is looking good. I have a few suggestions to help improve readability. I'm still working through a few more of the commits.

@@ -56,7 +50,7 @@ type Artifact struct {
StateData map[string]interface{}
}

func NewManagedImageArtifact(osType, resourceGroup, name, location, id, osDiskSnapshotName, dataDiskSnapshotPrefix string, generatedData map[string]interface{}, keepOSDisk bool, template *CaptureTemplate, getSasUrl func(name string) string) (*Artifact, error) {
func NewManagedImageArtifact(osType, resourceGroup, name, location, id, osDiskSnapshotName, dataDiskSnapshotPrefix string, generatedData map[string]interface{}, osDiskUri string) (*Artifact, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Future todo - we should rethink the number of parameters for this function.

return nil, fmt.Errorf("malformed capture template, expected one resource")
}
func NewArtifact(vmInternalID string, storageAccountUrl string, storageAccountLocation string, osType string, additionalDiskCount int, generatedData map[string]interface{}) (*Artifact, error) {
vhdUri := fmt.Sprintf("%ssystem/Microsoft.Compute/Images/images/packer-osDisk.%s.vhd", storageAccountUrl, vmInternalID)
Copy link
Contributor

Choose a reason for hiding this comment

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

If I was to create an Artifact without this constructor artifact := &Artifact{//...} how would I know the values to be used for OSDiskUri and TemplateUri?

Should there be functions for generating these values with valid inputs?

When it comes to generating the vhdUri do we need to validate that the storageAccountUrl ends with a /?
If so maybe we use the url package for joining URL strings.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to close the loop - as we discuss internally we will hold on refactors to not introduce untested changes.

if err != nil {
return nil, err
}
templateUri := fmt.Sprintf("%ssystem/Microsoft.Compute/Images/images/packer-vmTemplate.%s.json", storageAccountUrl, vmInternalID)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto on having a generate template URI function

builder/azure/arm/azure_client.go Show resolved Hide resolved
builder/azure/arm/azure_client.go Outdated Show resolved Hide resolved
builder/azure/arm/builder.go Outdated Show resolved Hide resolved
builder/azure/arm/builder.go Outdated Show resolved Hide resolved
@JenGoldstrich JenGoldstrich changed the title V2.0.0: Migrate all builders to hashicorp/go-azure-sdk [V2] Migrate all builders to hashicorp/go-azure-sdk Aug 7, 2023
builder/azure/dtl/template_factory.go Outdated Show resolved Hide resolved
builder/azure/arm/builder.go Outdated Show resolved Hide resolved
builder/azure/arm/step_capture_image.go Outdated Show resolved Hide resolved
@nywilken nywilken added enhancement breaking-change version/bump major A PR that breaks backwards compatibility. security Security issues/fixes. labels Aug 11, 2023
Copy link
Contributor

@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.

Nicely done. Please use the upgrade notes sections once released to communicate major breaking changes and migration steps for folks using v1 of the plugin.

Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

Left a few nits, but overall this looks good @JenGoldstrich! Good job on that

@@ -1239,6 +1245,11 @@ func assertRequiredParametersSet(c *Config, errs *packersdk.MultiError) {
}
if c.SharedGalleryDestination.SigDestinationImageVersion == "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("An image_version must be specified for shared_image_gallery_destination"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this is a pattern in the codebase, but I would think we can only check for the regex match and combine the two messages in one like An image_version must be specified for shared_image_gallery_destination and must follow the Major(int).Minor(int).Patch(int) format.
This can be done later of course as there's already a lot here, but I would think this gives a user more information on how to solve the issue while only checking once in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great point, I will make that change

@@ -525,25 +525,25 @@ func TestConfigInstantiatesCorrectAzureEnvironment(t *testing.T) {
"communicator": "none",
}

// user input is fun :)
// user input is fun :D
Copy link
Contributor

Choose a reason for hiding this comment

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

Best change of the whole PR

s.say(s.client.LastError.Error())
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

I would think you can return err in the main path as you did for captureImageFromVM

if err != nil {
return err
}

return f.WaitForCompletionRef(ctx, s.client.DeploymentsClient.Client)
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This could become a return err if you remove the error check

Copy link
Contributor

Choose a reason for hiding this comment

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

NOTE: I see this in several places in the code, so maybe that's a cleanup to do for later as well

builder/azure/common/client/config.go Outdated Show resolved Hide resolved
@JenGoldstrich JenGoldstrich merged commit a810cc7 into hashicorp:main Aug 11, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change enhancement security Security issues/fixes. version/bump major A PR that breaks backwards compatibility.
Projects
None yet
3 participants