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

Enable setting a description for EBS Volume Snapshots #471

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .web-docs/components/builder/ebsvolume/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concept

- `snapshot_volume` (bool) - Create a Snapshot of this Volume.

- `snapshot_description` (string) - The description for the snapshot.

<!-- End of code generated from the comments of the BlockDevice struct in builder/ebsvolume/block_device.go; -->


Expand Down
3 changes: 3 additions & 0 deletions builder/ebsvolume/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type BlockDevice struct {
// Create a Snapshot of this Volume.
SnapshotVolume bool `mapstructure:"snapshot_volume" required:"false"`

// The description for the snapshot.
SnapshotDescription string `mapstructure:"snapshot_description" required:"false"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I need to delve a bit into the code for understanding it better, but I believe we should add a check that if snapshot_description is set, snapshot_volume must be true, and error if that's not the case.
Probably the kind of thing to do in the Prepare function for the builder.

Alternatively if you don't want to error, maybe at least warn that it'll be ignored as no snapshot will be created for the block device?

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've implemented something and the output looks like this:

$ packer build example.pkr.hcl 
Warning: snapshot_description is ignored when snapshot_volume is not set to true.

  on example.pkr.hcl line 12:
  (source code not available)

This only shows up if snapshot_description is set but snapshot_volume is not (or is set to false).

Do I need to do something to cause the code line to be printed here? The line number is also not quite correct, it's the line number of the source definition.


Comment on lines +30 to +32
Copy link
Contributor Author

@saxonww saxonww Mar 25, 2024

Choose a reason for hiding this comment

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

I chose to add this here instead of builder/common/snapshot_config.go because it doesn't look like SnapshotDescription can be applied to the underlying volume snapshot when building an AMI with the CreateImage() call. You also already support setting an AMI Description using the ami_description field. I can move this there if that would be better, but it seemed like it would be confusing, to me.

awscommon.SnapshotConfig `mapstructure:",squash"`
}

Expand Down
7 changes: 7 additions & 0 deletions builder/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@
"Packer, inclusion of enable_t2_unlimited will error your builds.")
}

for _, configVolumeMapping := range b.config.VolumeMappings {
if configVolumeMapping.SnapshotDescription != "" && configVolumeMapping.SnapshotVolume != true {
warns = append(warns, "snapshot_description is ignored when "+
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lbajolet-hashicorp it looks like all of the fields in ebs_volumes are optional, so I don't know of a good way to make this more specific. Do you have any suggestions, or is this good enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I just realized if someone has multiple volumes they set a snapshot_description for, and more than one is missing snapshot_volume, this message will be printed more than one time. If there's no nice way to make each message specific to an ebs_volumes entry, I would propose adding a break here so it's only printed once. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think it's good enough, if we're able to pinpoint which volume is the problem that would be even better, but not sure there's something we can rely on there.

If we cannot pinpoint which block is the problematic one, we can definitely break after the warning is present once yes! I would maybe suggest changing the phrasing a bit to signal that at least one block is faulty but there may be others.

Suggested change
warns = append(warns, "snapshot_description is ignored when "+
warns = append(warns, "at least one block's `snapshot_description` was ignored, as it needs `snapshot_volume` to be also set.")

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, this could also conceivably be an error, as they happen early in the process (before the build even starts unless I'm mistaken), this would force users to address the issue before continuing.

"snapshot_volume is not set to true.")

Check failure on line 167 in builder/ebsvolume/builder.go

View workflow job for this annotation

GitHub Actions / Lint check

File is not `goimports`-ed (goimports)
}
}

if errs != nil && len(errs.Errors) > 0 {
return nil, warns, errs
}
Expand Down
2 changes: 2 additions & 0 deletions builder/ebsvolume/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/ebsvolume/step_snapshot_ebs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *stepSnapshotEBSVolumes) Run(ctx context.Context, state multistep.StateB
input := &ec2.CreateSnapshotInput{
VolumeId: aws.String(*instanceBlockDevice.Ebs.VolumeId),
TagSpecifications: []*ec2.TagSpecification{tagSpec},
Description: aws.String(configVolumeMapping.SnapshotDescription),
}

//Dont try to set an empty tag spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

- `snapshot_volume` (bool) - Create a Snapshot of this Volume.

- `snapshot_description` (string) - The description for the snapshot.

<!-- End of code generated from the comments of the BlockDevice struct in builder/ebsvolume/block_device.go; -->
Loading