From 06b35c39de300057c68e140677044b36f34fc509 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Thu, 18 Nov 2021 03:40:08 -0500 Subject: [PATCH] hcp packer registry build labels (#11401) * Add basic support for build_labels argument * Update support for build_labels configuration argument * Update complete test-fixture with a build_labels configuration * Add test for deprecated labels argument * Add deprecation for hcp_packer_registry.labels When using the now deprecated labels argument of the new bucket_labels a Warning will be presented to the user. ``` ~> HCP_PACKER_BUILD_FINGERPRINT=356786543567865456789656789 packer build source.pkr.hcl Warning: the argument hcp_packer_registry.labels has been deprecated and will be removed in a future release; please use hcp_packer_registry.bucket_labels ``` When trying to use both bucket_labels and labels together an error is presented to the user. ``` ~> HCP_PACKER_BUILD_FINGERPRINT=ss6786543567865456789656789 packer build source.pkr.hcl Error: hcp_packer_registry.labels and hcp_packer_registry.bucket_labels are mutely exclusive; please use the recommended argument hcp_packer_registry.bucket_labels on source.pkr.hcl line 17: (source code not available) ``` * Update documentation for build_labels * Apply suggestions from code review Co-authored-by: Adrien Delorme * Update hcl2template/types.build.hcp_packer_registry.go Co-authored-by: Adrien Delorme --- .../testdata/hcp_par/complete.pkr.hcl | 5 +- .../hcp_par/deprecated_labels.pkr.hcl | 18 +++ .../testdata/hcp_par/duplicate.pkr.hcl | 2 +- hcl2template/testdata/hcp_par/slug.pkr.hcl | 2 +- .../types.build.hcp_packer_registry.go | 40 +++++-- .../types.build.hcp_packer_registry_test.go | 111 ++++++++++++++---- internal/registry/types.bucket.go | 21 ++-- .../blocks/build/hcp_packer_registry.mdx | 36 ++++-- 8 files changed, 182 insertions(+), 53 deletions(-) create mode 100644 hcl2template/testdata/hcp_par/deprecated_labels.pkr.hcl diff --git a/hcl2template/testdata/hcp_par/complete.pkr.hcl b/hcl2template/testdata/hcp_par/complete.pkr.hcl index 4dda76fd3..b4d17e5fd 100644 --- a/hcl2template/testdata/hcp_par/complete.pkr.hcl +++ b/hcl2template/testdata/hcp_par/complete.pkr.hcl @@ -5,9 +5,12 @@ build { description = < 0 && len(b.BucketLabels) > 0 { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: fmt.Sprintf("%s.labels and %[1]s.bucket_labels are mutually exclusive; please use the recommended argument %[1]s.bucket_labels", buildHCPPackerRegistryLabel), + Subject: block.DefRange.Ptr(), + }) + return nil, diags + } + + if len(b.Labels) > 0 { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: fmt.Sprintf("the argument %s.labels has been deprecated and will be removed in the next minor release; please use %[1]s.bucket_labels", buildHCPPackerRegistryLabel), + }) + + b.BucketLabels = b.Labels + } + + par.BucketLabels = b.BucketLabels + par.BuildLabels = b.BuildLabels return par, diags } diff --git a/hcl2template/types.build.hcp_packer_registry_test.go b/hcl2template/types.build.hcp_packer_registry_test.go index b3eccb1d6..50d577cfc 100644 --- a/hcl2template/types.build.hcp_packer_registry_test.go +++ b/hcl2template/types.build.hcp_packer_registry_test.go @@ -27,8 +27,9 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { &BuildBlock{ Name: "bucket-slug", HCPPackerRegistry: &HCPPackerRegistryBlock{ - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, + BuildLabels: map[string]string{"python_version": "3.0"}, }, Sources: []SourceUseBlock{ { @@ -52,9 +53,10 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { Name: "virtualbox-iso.ubuntu-1204", Builder: emptyMockBuilder, ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, + BuildLabels: map[string]string{"python_version": "3.0"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored }, @@ -67,9 +69,10 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { PostProcessor: &packer.RegistryPostProcessor{ BuilderType: "virtualbox-iso.ubuntu-1204", ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, + BuildLabels: map[string]string{"python_version": "3.0"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", }, @@ -87,9 +90,10 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { Name: "amazon-ebs.aws-ubuntu-16.04", Builder: emptyMockBuilder, ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, + BuildLabels: map[string]string{"python_version": "3.0"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored }, @@ -102,9 +106,10 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { PostProcessor: &packer.RegistryPostProcessor{ BuilderType: "amazon-ebs.aws-ubuntu-16.04", ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, + BuildLabels: map[string]string{"python_version": "3.0"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", }, @@ -130,9 +135,9 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { &BuildBlock{ Name: "bucket-slug", HCPPackerRegistry: &HCPPackerRegistryBlock{ - Slug: "real-bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "real-bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, }, Sources: []SourceUseBlock{ { @@ -152,9 +157,9 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { Name: "virtualbox-iso.ubuntu-1204", Builder: emptyMockBuilder, ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "real-bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "real-bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored }, @@ -167,9 +172,9 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { PostProcessor: &packer.RegistryPostProcessor{ BuilderType: "virtualbox-iso.ubuntu-1204", ArtifactMetadataPublisher: &packer_registry.Bucket{ - Slug: "real-bucket-slug", - Description: "Some description\n", - Labels: map[string]string{"foo": "bar"}, + Slug: "real-bucket-slug", + Description: "Some description\n", + BucketLabels: map[string]string{"foo": "bar"}, Iteration: &packer_registry.Iteration{ Fingerprint: "ignored-fingerprint", }, @@ -371,6 +376,66 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) { }, false, }, + {"deprecated labels in hcp_packer_registry block", + defaultParser, + parseTestArgs{"testdata/hcp_par/deprecated_labels.pkr.hcl", nil, nil}, + &PackerConfig{ + CorePackerVersionString: lockedVersion, + Basedir: filepath.Join("testdata", "hcp_par"), + Sources: map[SourceRef]SourceBlock{ + refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"}, + }, + Builds: Builds{ + &BuildBlock{ + HCPPackerRegistry: &HCPPackerRegistryBlock{ + Slug: "bucket-slug", + BucketLabels: map[string]string{"foo": "bar"}, + }, + Sources: []SourceUseBlock{ + { + SourceRef: refVBIsoUbuntu1204, + }, + }, + }, + }, + }, + true, false, + []packersdk.Build{ + &packer.CoreBuild{ + Type: "virtualbox-iso.ubuntu-1204", + Prepared: true, + Builder: &packer.RegistryBuilder{ + Name: "virtualbox-iso.ubuntu-1204", + Builder: emptyMockBuilder, + ArtifactMetadataPublisher: &packer_registry.Bucket{ + Slug: "bucket-slug", + BucketLabels: map[string]string{"foo": "bar"}, + Iteration: &packer_registry.Iteration{ + Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored + }, + }, + }, + Provisioners: []packer.CoreBuildProvisioner{}, + PostProcessors: [][]packer.CoreBuildPostProcessor{ + { + { + PostProcessor: &packer.RegistryPostProcessor{ + BuilderType: "virtualbox-iso.ubuntu-1204", + ArtifactMetadataPublisher: &packer_registry.Bucket{ + Slug: "bucket-slug", + BucketLabels: map[string]string{"foo": "bar"}, + Iteration: &packer_registry.Iteration{ + Fingerprint: "ignored-fingerprint", + }, + }, + }, + }, + }, + }, + }, + }, + false, + }, {"invalid hcp_packer_registry config", defaultParser, parseTestArgs{"testdata/hcp_par/invalid.pkr.hcl", nil, nil}, diff --git a/internal/registry/types.bucket.go b/internal/registry/types.bucket.go index e78442820..63b6268b9 100644 --- a/internal/registry/types.bucket.go +++ b/internal/registry/types.bucket.go @@ -17,12 +17,13 @@ import ( // Bucket represents a single Image bucket on the HCP Packer registry. type Bucket struct { - Slug string - Description string - Destination string - Labels map[string]string - Iteration *Iteration - client *Client + Slug string + Description string + Destination string + BucketLabels map[string]string + BuildLabels map[string]string + Iteration *Iteration + client *Client } // NewBucketWithIteration initializes a simple Bucket that can be used publishing Packer build @@ -79,7 +80,7 @@ func (b *Bucket) Initialize(ctx context.Context) error { bucketInput := &models.HashicorpCloudPackerCreateBucketRequest{ BucketSlug: b.Slug, Description: b.Description, - Labels: b.Labels, + Labels: b.BucketLabels, } err := UpsertBucket(ctx, b.client, bucketInput) @@ -121,12 +122,16 @@ func (b *Bucket) CreateInitialBuildForIteration(ctx context.Context, componentTy return err } + if b.BuildLabels == nil { + b.BuildLabels = make(map[string]string) + } + build := &Build{ ID: id, ComponentType: componentType, RunUUID: b.Iteration.RunUUID, Status: status, - Labels: make(map[string]string), + Labels: b.BuildLabels, Images: make(map[string]registryimage.Image), } diff --git a/website/content/docs/templates/hcl_templates/blocks/build/hcp_packer_registry.mdx b/website/content/docs/templates/hcl_templates/blocks/build/hcp_packer_registry.mdx index 646a2ab71..ef341fd06 100644 --- a/website/content/docs/templates/hcl_templates/blocks/build/hcp_packer_registry.mdx +++ b/website/content/docs/templates/hcl_templates/blocks/build/hcp_packer_registry.mdx @@ -31,26 +31,29 @@ builders (source blocks) and the HCP Packer registry. ```hcl # file: builds.pkr.hcl -source "file" "basic-example" { - content = "Lorem ipsum dolor sit amet" - target = "sample_artifact" +source "happycloud" "macos" { + os = "macos_amd64" } build { hcp_packer_registry { - bucket_name = "sample-artifact" + bucket_name = "ios-dev" description = <