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

Associate Packer build to HCP Packer parent iteration #11832

Merged
merged 3 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions hcl2template/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ var cmpOpts = []cmp.Option{
cmpopts.IgnoreFields(packerregistry.Iteration{},
"Fingerprint", // Fingerprint will change everytime
),
cmpopts.IgnoreFields(packerregistry.Bucket{},
"SourceImagesToParentIterations", // Requires execution of datasource at this time
),
cmpopts.IgnoreFields(VariableAssignment{},
"Expr", // its an interface
),
Expand Down
15 changes: 15 additions & 0 deletions hcl2template/types.build.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
for _, source := range build.Sources {
cfg.bucket.RegisterBuildForComponent(source.String())
}

// Known HCP Packer Image Datasource, whose id is the SourceImageId for some build.
const hcpDatasourceType string = "hcp-packer-image"
values := ectx.Variables[dataAccessor].AsValueMap()

dsValues, ok := values[hcpDatasourceType]
if !ok {
return build, diags
}

for _, value := range dsValues.AsValueMap() {
values := value.AsValueMap()
imgID, itID := values["id"], values["iteration_id"]
cfg.bucket.SourceImagesToParentIterations[imgID.AsString()] = itID.AsString()
}
}

return build, diags
Expand Down
3 changes: 2 additions & 1 deletion hcl2template/types.build.hcp_packer_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (p *Parser) decodeHCPRegistry(block *hcl.Block, cfg *PackerConfig) (*HCPPac
BuildLabels map[string]string `hcl:"build_labels,optional"`
Config hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(body, cfg.EvalContext(LocalContext, nil), &b)
ectx := cfg.EvalContext(BuildContext, nil)
diags := gohcl.DecodeBody(body, ectx, &b)
if diags.HasErrors() {
return nil, diags
}
Expand Down
4 changes: 4 additions & 0 deletions hcl2template/types.build.hcp_packer_registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcl2template

import (
"os"
"path/filepath"
"testing"

Expand All @@ -11,6 +12,9 @@ import (
)

func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "hcp-par-test")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")

defaultParser := getBasicParser()

tests := []parseTest{
Expand Down
14 changes: 8 additions & 6 deletions internal/registry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (client *Client) UpdateBuild(
runUUID,
cloudProvider,
sourceImageID string,
sourceIterationID string,
labels map[string]string,
status models.HashicorpCloudPackerBuildStatus,
images []*models.HashicorpCloudPackerImageCreateBody,
Expand All @@ -205,12 +206,13 @@ func (client *Client) UpdateBuild(
params.Body = &models.HashicorpCloudPackerUpdateBuildRequest{
BuildID: buildID,
Updates: &models.HashicorpCloudPackerBuildUpdates{
Images: images,
PackerRunUUID: runUUID,
Labels: labels,
Status: status,
CloudProvider: cloudProvider,
SourceImageID: sourceImageID,
Images: images,
PackerRunUUID: runUUID,
Labels: labels,
Status: status,
CloudProvider: cloudProvider,
SourceImageID: sourceImageID,
SourceIterationID: sourceIterationID,
},
}

Expand Down
29 changes: 19 additions & 10 deletions internal/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ import (

// Bucket represents a single Image bucket on the HCP Packer registry.
type Bucket struct {
Slug string
Description string
Destination string
BucketLabels map[string]string
BuildLabels map[string]string
Iteration *Iteration
client *Client
Slug string
Description string
Destination string
BucketLabels map[string]string
BuildLabels map[string]string
SourceImagesToParentIterations map[string]string
Iteration *Iteration
client *Client
}

// NewBucketWithIteration initializes a simple Bucket that can be used publishing Packer build
// images to the HCP Packer registry.
func NewBucketWithIteration(opts IterationOptions) (*Bucket, error) {
b := Bucket{
BucketLabels: make(map[string]string),
BuildLabels: make(map[string]string),
BucketLabels: make(map[string]string),
BuildLabels: make(map[string]string),
SourceImagesToParentIterations: make(map[string]string),
}

i, err := NewIteration(opts)
Expand Down Expand Up @@ -156,6 +158,7 @@ func (b *Bucket) UpdateBuildStatus(ctx context.Context, name string, status mode
buildToUpdate.RunUUID,
buildToUpdate.CloudProvider,
"",
"",
buildToUpdate.Labels,
status,
nil,
Expand Down Expand Up @@ -192,7 +195,7 @@ func (b *Bucket) markBuildComplete(ctx context.Context, name string) error {
return fmt.Errorf("setting a build to DONE with no published images is not currently supported.")
}

var providerName, sourceID string
var providerName, sourceID, sourceIterationID string
images := make([]*models.HashicorpCloudPackerImageCreateBody, 0, len(buildToUpdate.Images))
for _, image := range buildToUpdate.Images {
// These values will always be the same for all images in a single build,
Expand All @@ -204,6 +207,11 @@ func (b *Bucket) markBuildComplete(ctx context.Context, name string) error {
sourceID = image.SourceImageID
}

// Check if image is using some other HCP Packer image
if v, ok := b.SourceImagesToParentIterations[image.SourceImageID]; ok {
sourceIterationID = v
}

images = append(images, &models.HashicorpCloudPackerImageCreateBody{ImageID: image.ImageID, Region: image.ProviderRegion})
}

Expand All @@ -212,6 +220,7 @@ func (b *Bucket) markBuildComplete(ctx context.Context, name string) error {
buildToUpdate.RunUUID,
buildToUpdate.CloudProvider,
sourceID,
sourceIterationID,
buildToUpdate.Labels,
status,
images,
Expand Down
12 changes: 6 additions & 6 deletions internal/registry/types.bucket_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestInitialize_NewBucketNewIteration(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()

Expand Down Expand Up @@ -66,7 +66,7 @@ func TestInitialize_NewBucketNewIteration(t *testing.T) {

func TestInitialize_ExistingBucketNewIteration(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestInitialize_ExistingBucketNewIteration(t *testing.T) {

func TestInitialize_ExistingBucketExistingIteration(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestInitialize_ExistingBucketExistingIteration(t *testing.T) {

func TestInitialize_ExistingBucketCompleteIteration(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestInitialize_ExistingBucketCompleteIteration(t *testing.T) {

func TestUpdateBuildStatus(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestUpdateBuildStatus(t *testing.T) {

func TestUpdateBuildStatus_DONENoImages(t *testing.T) {
//nolint:errcheck
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "testnumber")
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
Expand Down
6 changes: 5 additions & 1 deletion internal/registry/types.bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"context"
"os"
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -278,10 +279,13 @@ func TestBucket_PopulateIteration(t *testing.T) {
},
}

for _, tt := range tc {
for i, tt := range tc {
tt := tt
t.Run(tt.desc, func(t *testing.T) {

os.Setenv("HCP_PACKER_BUILD_FINGERPRINT", "test-run-"+strconv.Itoa(i))
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")

mockService := NewMockPackerClientService()
mockService.BucketAlreadyExist = true
mockService.IterationAlreadyExist = true
Expand Down