Skip to content

Commit

Permalink
hcp packer registry build labels (#11401)
Browse files Browse the repository at this point in the history
* 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 <azr@users.noreply.github.com>

* Update hcl2template/types.build.hcp_packer_registry.go

Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
  • Loading branch information
nywilken and azr committed Nov 18, 2021
1 parent acd67d5 commit 06b35c3
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 53 deletions.
5 changes: 4 additions & 1 deletion hcl2template/testdata/hcp_par/complete.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ build {
description = <<EOT
Some description
EOT
labels = {
bucket_labels = {
"foo" = "bar"
}
build_labels = {
"python_version" = "3.0"
}
}

sources = [
Expand Down
18 changes: 18 additions & 0 deletions hcl2template/testdata/hcp_par/deprecated_labels.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

build {

hcp_packer_registry {
bucket_name = "bucket-slug"
labels = {
"foo" = "bar"
}
}

sources = [
"source.virtualbox-iso.ubuntu-1204",
]
}

source "virtualbox-iso" "ubuntu-1204" {
}

2 changes: 1 addition & 1 deletion hcl2template/testdata/hcp_par/duplicate.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ build {
name = "bucket-slug"
hcp_packer_registry {
description = ""
labels = {
bucket_labels = {
"foo" = "bar"
}
}
Expand Down
2 changes: 1 addition & 1 deletion hcl2template/testdata/hcp_par/slug.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build {
description = <<EOT
Some description
EOT
labels = {
bucket_labels = {
"foo" = "bar"
}
}
Expand Down
40 changes: 33 additions & 7 deletions hcl2template/types.build.hcp_packer_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type HCPPackerRegistryBlock struct {
// Bucket description
Description string
// Bucket labels
Labels map[string]string
BucketLabels map[string]string
// Build labels
BuildLabels map[string]string

HCL2Ref
}
Expand All @@ -24,7 +26,8 @@ func (b *HCPPackerRegistryBlock) WriteToBucketConfig(bucket *packerregistry.Buck
return
}
bucket.Description = b.Description
bucket.Labels = b.Labels
bucket.BucketLabels = b.BucketLabels
bucket.BuildLabels = b.BuildLabels
// If there's already a Slug this was set from env variable.
// In Packer, env variable overrides config values so we keep it that way for consistency.
if bucket.Slug == "" && b.Slug != "" {
Expand All @@ -37,10 +40,13 @@ func (p *Parser) decodeHCPRegistry(block *hcl.Block) (*HCPPackerRegistryBlock, h
body := block.Body

var b struct {
Slug string `hcl:"bucket_name,optional"`
Description string `hcl:"description,optional"`
Labels map[string]string `hcl:"labels,optional"`
Config hcl.Body `hcl:",remain"`
Slug string `hcl:"bucket_name,optional"`
Description string `hcl:"description,optional"`
//Deprecated labels for bucket_labels
Labels map[string]string `hcl:"labels,optional"`
BucketLabels map[string]string `hcl:"bucket_labels,optional"`
BuildLabels map[string]string `hcl:"build_labels,optional"`
Config hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(body, nil, &b)
if diags.HasErrors() {
Expand All @@ -58,7 +64,27 @@ func (p *Parser) decodeHCPRegistry(block *hcl.Block) (*HCPPackerRegistryBlock, h

par.Slug = b.Slug
par.Description = b.Description
par.Labels = b.Labels

if len(b.Labels) > 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
}
111 changes: 88 additions & 23 deletions hcl2template/types.build.hcp_packer_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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
},
Expand All @@ -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",
},
Expand All @@ -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
},
Expand All @@ -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",
},
Expand All @@ -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{
{
Expand All @@ -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
},
Expand All @@ -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",
},
Expand Down Expand Up @@ -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},
Expand Down
21 changes: 13 additions & 8 deletions internal/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
}

Expand Down
Loading

0 comments on commit 06b35c3

Please sign in to comment.