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

HPR-1537: Packer Core sends metadata to HCP Packer in UpdateBuild API #12891

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/hashicorp/packer

replace github.com/hashicorp/hcp-sdk-go => ../hcp-sdk-go-internal

require (
cloud.google.com/go v0.110.8 // indirect
github.com/biogo/hts v1.4.3
Expand All @@ -22,7 +24,7 @@ require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/hcp-sdk-go v0.85.0
// github.com/hashicorp/hcp-sdk-go v0.85.0
github.com/hashicorp/packer-plugin-amazon v1.2.1
github.com/hashicorp/packer-plugin-sdk v0.5.2
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869
Expand Down Expand Up @@ -59,6 +61,7 @@ require (

require (
github.com/go-openapi/strfmt v0.21.10
github.com/hashicorp/hcp-sdk-go v0.0.0-00010101000000-000000000000
devashish-patel marked this conversation as resolved.
Show resolved Hide resolved
github.com/oklog/ulid v1.3.1
github.com/pierrec/lz4/v4 v4.1.18
github.com/shirou/gopsutil/v3 v3.23.4
Expand Down
4 changes: 1 addition & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/hcp-sdk-go v0.85.0 h1:RDXpIf4gIGfz6je8Qq0FGg+8kwj7KboQTOxnklWyVJk=
github.com/hashicorp/hcp-sdk-go v0.85.0/go.mod h1:vQ4fzdL1AmhIAbCw+4zmFe5Hbpajj3NvRWkJoVuxmAk=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM=
Expand Down Expand Up @@ -759,8 +757,8 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
7 changes: 7 additions & 0 deletions internal/hcp/api/service_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *Client) UpdateBuild(
buildLabels map[string]string,
buildStatus hcpPackerModels.HashicorpCloudPacker20230101BuildStatus,
artifacts []*hcpPackerModels.HashicorpCloudPacker20230101ArtifactCreateBody,
metadata map[string]interface{},
) (string, error) {

params := hcpPackerAPI.NewPackerServiceUpdateBuildParamsWithContext(ctx)
Expand All @@ -78,6 +79,12 @@ func (c *Client) UpdateBuild(
Status: &buildStatus,
}

if metadata != nil {
params.Body.Metadata = &hcpPackerModels.HashicorpCloudPacker20230101BuildMetadata{
Packer: metadata,
}
}

resp, err := c.Packer.PackerServiceUpdateBuild(params, nil)
if err != nil {
return "", err
Expand Down
10 changes: 10 additions & 0 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func (h *HCLRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) err
if ok {
name = cb.Type
}

metadata := cb.GetMetadata()
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
return h.bucket.startBuild(ctx, name)
}

Expand Down Expand Up @@ -97,6 +103,10 @@ func (h *HCLRegistry) CompleteBuild(
name, k, pluginDetails.Description.Version,
)
}
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
}

Expand Down
14 changes: 13 additions & 1 deletion internal/hcp/registry/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ func (h *JSONRegistry) PopulateVersion(ctx context.Context) error {

// StartBuild is invoked when one build for the configuration is starting to be processed
func (h *JSONRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) error {
return h.bucket.startBuild(ctx, build.Name())
name := build.Name()

metadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
return h.bucket.startBuild(ctx, name)
}

// CompleteBuild is invoked when one build for the configuration has finished
Expand All @@ -106,6 +113,11 @@ func (h *JSONRegistry) CompleteBuild(
name, k, pluginDetails.Description.Version,
)
}

err := h.bucket.AddMetadataToBuild(ctx, name, metadata)
devashish-patel marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
}

Expand Down
30 changes: 30 additions & 0 deletions internal/hcp/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/packer/hcl2template"
hcpPackerAPI "github.com/hashicorp/packer/internal/hcp/api"
"github.com/hashicorp/packer/internal/hcp/env"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/mapstructure"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -213,6 +214,7 @@ func (bucket *Bucket) UpdateBuildStatus(
nil,
status,
nil,
buildToUpdate.Metadata,
)
if err != nil {
return err
Expand Down Expand Up @@ -285,6 +287,7 @@ func (bucket *Bucket) markBuildComplete(ctx context.Context, name string) error
buildToUpdate.Labels,
status,
artifacts,
buildToUpdate.Metadata,
)
if err != nil {
return err
Expand Down Expand Up @@ -535,6 +538,7 @@ func (bucket *Bucket) HeartbeatBuild(ctx context.Context, build string) (func(),
nil,
hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDRUNNING,
nil,
nil,
)
if err != nil {
log.Printf("[ERROR] failed to send heartbeat for build %q: %s", build, err)
Expand Down Expand Up @@ -599,6 +603,32 @@ type NotAHCPArtifactError struct {
error
}

// AddMetadataToBuild adds metadata to a build in the HCP Packer registry.
func (bucket *Bucket) AddMetadataToBuild(
ctx context.Context, buildName string, metadata packer.BuildMetadata,
) error {
buildToUpdate, err := bucket.Version.Build(buildName)
if err != nil {
return err
}

preparedMetadata := make(map[string]interface{})
preparedMetadata["version"] = metadata.PackerVersion
devashish-patel marked this conversation as resolved.
Show resolved Hide resolved

var pluginsMetadata []map[string]interface{}
for _, plugin := range metadata.Plugins {
pluginMetadata := map[string]interface{}{
"version": plugin.Description.Version,
"name": plugin.Name,
}
pluginsMetadata = append(pluginsMetadata, pluginMetadata)
}
preparedMetadata["plugins"] = pluginsMetadata

buildToUpdate.Metadata = preparedMetadata
return nil
}

func (bucket *Bucket) completeBuild(
ctx context.Context,
buildName string,
Expand Down
1 change: 1 addition & 0 deletions internal/hcp/registry/types.builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Build struct {
Labels map[string]string
Artifacts map[string]packerSDKRegistry.Image
Status hcpPackerModels.HashicorpCloudPacker20230101BuildStatus
Metadata map[string]interface{}
devashish-patel marked this conversation as resolved.
Show resolved Hide resolved
}

// NewBuildFromCloudPackerBuild converts a HashicorpCloudPackerBuild to a local build that can be tracked and
Expand Down
Loading