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

Expose versionFingerprint instead of versionId from packer template #12803

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 15 additions & 3 deletions hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package hcl2template

import (
"fmt"
"log"
"sort"
"strings"

"github.com/gobwas/glob"
hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/hcl/v2/hclsyntax"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -110,8 +111,9 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty.
}),
buildAccessor: cty.UnknownVal(cty.EmptyObject),
packerAccessor: cty.ObjectVal(map[string]cty.Value{
lbajolet-hashicorp marked this conversation as resolved.
Show resolved Hide resolved
"version": cty.StringVal(cfg.CorePackerVersionString),
"iterationID": cty.UnknownVal(cty.String),
"version": cty.StringVal(cfg.CorePackerVersionString),
"iterationID": cty.UnknownVal(cty.String),
"versionFingerprint": cty.UnknownVal(cty.String),
}),
pathVariablesAccessor: cty.ObjectVal(map[string]cty.Value{
"cwd": cty.StringVal(strings.ReplaceAll(cfg.Cwd, `\`, `/`)),
Expand All @@ -122,12 +124,22 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty.

iterID, ok := cfg.HCPVars["iterationID"]
if ok {
log.Printf("[WARN] Deprecation: Contextual Variable `iterationID` has been deprecated packer context. " +
Copy link
Contributor

Choose a reason for hiding this comment

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

The log here will be problematic, the context is used for evaluating HCL expressions, and we create it often.
The fact that iterationID exists does not mean it is used, that happens in other places.
Note: it may not be trivial to figure out with certainty that it's used, as evaluation is done by the HCL library, outside of Packer's code.

"Please use `versionFingerprint` variable instead.")
ectx.Variables[packerAccessor] = cty.ObjectVal(map[string]cty.Value{
"version": cty.StringVal(cfg.CorePackerVersionString),
"iterationID": iterID,
})
}

versionFingerprint, ok := cfg.HCPVars["versionFingerprint"]
Copy link
Contributor

Choose a reason for hiding this comment

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

That will make iterationID unusable in this state unfortunately, since we create the object in this branch, and we do the same above, so this assignation will erase the previous value.

I would suggest changing a couple of things here, the previous approach is probably not valid anymore.
Let's remove the creation of the packerAccessor object from the declaration of the context, we can build it along the way, and only create it when it is in its final state.

packerVars := map[string]cty.Value{
	"version": cty.StringVal(cfg.CorePackerVersionString),
	"iterationID": cty.UnknownVal(cty.String),
	"versionFingerprint": cty.UnknownVal(cty.String),
}

iterID, ok := cfg.HCPVars["iterationID"]
if ok {
	packerVars["iterationID"] = cty.StringVal(iterID)
}
versionFP, ok := cfg.HCPVars["versionFingerprint"]
if ok {
	packerVars["versionFingerprint"] = cty.String(versionFP)
}

ectx.Variables[packerAccessor] = cty.ObjectVal(packerVars)

if ok {
ectx.Variables[packerAccessor] = cty.ObjectVal(map[string]cty.Value{
"version": cty.StringVal(cfg.CorePackerVersionString),
"versionFingerprint": versionFingerprint,
})
}

// In the future we'd like to load and execute HCL blocks using a graph
// dependency tree, so that any block can use any block whatever the
// order.
Expand Down
3 changes: 2 additions & 1 deletion internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func (h *HCLRegistry) PopulateVersion(ctx context.Context) error {
}

versionID := h.bucket.Version.ID
versionFingerprint := h.bucket.Version.Fingerprint

// FIXME: Remove
h.configuration.HCPVars["iterationID"] = cty.StringVal(versionID)
h.configuration.HCPVars["versionID"] = cty.StringVal(versionID)
h.configuration.HCPVars["versionFingerprint"] = cty.StringVal(versionFingerprint)

sha, err := getGitSHA(h.configuration.Basedir)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ parenthesis may through off your shell escaping otherwise.

# HCP Packer Iteration ID

~> **Note**: Deprecation: Contextual Variable `iterationID` has been deprecated packer context. Please use `versionFingerprint` variable instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we can suggest using versionFingerprint without encouraging them to update the data sources that use it at the same time.
I would maybe reword this here so it explains the rationale behind the change.

Suggested change
~> **Note**: Deprecation: Contextual Variable `iterationID` has been deprecated packer context. Please use `versionFingerprint` variable instead.
~> **Note**: the `packer.iterationID` variable is now deprecated and will be removed in a future version of Packer. HCP Packer Versions should be accessed with their fingerprint instead. The `packer.versionFingerprint` variable is now exposed to be used in its stead with the new HCP Packer data sources.


If your build is pushing metadata to the HCP Packer registry, this variable is
set to the value of the Iteration ID associated with this run.

Expand All @@ -145,6 +147,22 @@ source "amazon-ebs" "cannonical-ubuntu-server" {
}
```

# HCP Packer Version Fingerprint

If your build is pushing metadata to the HCP Packer registry, this variable is
set to the value of the Version Fingerprint associated with this run.

```hcl
source "amazon-ebs" "cannonical-ubuntu-server" {
ami_name = "packer-example"
// ...
run_volume_tags = {
hcp_version_fingerprint = packer.versionFingerprint
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder, does that work? I'm not certain the fingerprint will be instanciated by the time we evaluate this.

Copy link
Contributor

Choose a reason for hiding this comment

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

To be clear if it doesn't we should fix it later, but I'm cautious giving this example if it doesn't work

}
}
```


```shell-session
==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance
vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder"
Copy link
Contributor

Choose a reason for hiding this comment

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

We should update the logs here so they don't show the hcp_iteration_id in there right?

Expand All @@ -158,6 +176,8 @@ You can also add this value to post-processors, for example to add to a manifest
output = "manifest.json"
strip_path = true
custom_data = {
version_fingerprint = "${packer.versionFingerprint}"
// `packer.iterationID` has been deprecated.
iteration = "${packer.iterationID}"
}
}
Expand Down
Loading