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

hcp: give a proper error when using conflicting build name #13300

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

Conversation

mogrogan
Copy link
Contributor

@mogrogan mogrogan commented Feb 17, 2025

It was possible to put the same source 2 times in the build and when using HCP, it would error eventually since they are considered the samebuild from HCP side

Test

packer {
  required_plugins {
    docker = {
      version = ">= 1.0.8"
      source = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "ubuntu-jammy" {
  image  = "ubuntu:jammy"
  commit = true
}

source "docker" "ubuntu-focal" {
  image  = "ubuntu:focal"
  commit = true
}

build {
  name = "jammy-build-block"

  hcp_packer_registry {
    bucket_name = "test-root"
    description = "hcp test with root"
  }
  sources = [
    "source.docker.ubuntu-jammy",
    "source.docker.ubuntu-jammy",
  ]
}

This would give this error:

$ packer build .
Error: Build name conflicts

When using HCP registry, build names are used as identifier. In this case, there
is 2 or more build called jammy-build-block.docker.ubuntu-jammy

@mogrogan mogrogan requested a review from a team as a code owner February 17, 2025 21:23
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.

Overall this looks good to me, I think it maintains existing compatibility, but to be sure, have you run HPATS on those?
I left a couple comments, but nothing blocking, I'll let you address those and come back for another round of review later.

func TestRegisterAllBuilds(t *testing.T) {
cases := map[string]struct {
expectedBuilds []string
err bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this could be renamed to expectErr to clarify intent

Severity: hcl.DiagError,
Summary: "Build name conflicts",
Detail: "When using HCP registry, build names are used as identifier. " +
"In this case, there is 2 or more build called " + name,
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 if we can add a lead for solving this conflict in this case, like adding a name to the build block if relevant.
I would also suggest adding a Subject here to have a location in the error message so users know where to look in order to fix the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, the only way to get a conflict is by using the same source on the build block. We probably should add a more precise message error once we support multiple build block

@mogrogan mogrogan force-pushed the prevent-name-conflicts-hcp branch 3 times, most recently from 1688277 to 2d88abf Compare February 20, 2025 20:27

type fullBuildNames map[string]map[string]struct{}

// set will return true if properly inserted and false if the values is conflicting
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// set will return true if properly inserted and false if the values is conflicting
// set will return true if properly inserted and false if the values are conflicting

}

func (h *HCLRegistry) Metadata() Metadata {
return h.metadata
}

type fullBuildNames map[string]map[string]struct{}
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 necessarily against the idea of introducing a structure for this, but it feels a bit verbous to get them, I wonder if having functions directly on HCLRegistry wouldn't be better for this purpose?

configuration *hcl2template.PackerConfig
bucket *Bucket
ui sdkpacker.Ui
metadata *MetadataStore
Copy link
Contributor

Choose a reason for hiding this comment

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

Come to think of it, since we change how we get the build names, does that impact that MetadataStore? It might not but I just want to be sure we considered delving into this before this gets merged, so that we don't break things later down the line

type fullBuildNames map[string]map[string]struct{}

// set will return true if properly inserted and false if the values is conflicting
func (b fullBuildNames) set(buildName string, sourceName string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming we remove that structure, this function is a one-time use thing, so the code might as well be rolled into the function that adds the builds I'd think

}

// get will return the properly formatted string taking name conflict into account
func (b fullBuildNames) get(buildName string, sourceName string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming this is rolled into the function (or not even), the signature could be changed to accept a *CoreBuild instead, so we can do the logic of getting the build from the source/build name combination if relevant

@mogrogan mogrogan force-pushed the prevent-name-conflicts-hcp branch 2 times, most recently from 73401f3 to dd52d0c Compare February 21, 2025 15:11
It was possible to put the same source 2 times in the build and when
using HCP, it would error eventually since they are considered the same
build from HCP side
@mogrogan mogrogan force-pushed the prevent-name-conflicts-hcp branch from dd52d0c to cc744bb Compare February 21, 2025 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants