Skip to content

Commit

Permalink
adding image_architecture to enable ARM64 or x86 image builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennenMM7 committed Mar 25, 2024
1 parent 289eb59 commit d0e0d1f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .web-docs/components/builder/googlecompute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ builder.

- `skip_create_image` (bool) - Skip creating the image. Useful for setting to `true` during a build test stage. Defaults to `false`.

- `image_architecture` (string) - The architecture of the resulting image. Defaults to "x86_64".

- `image_name` (string) - The unique name of the resulting image. Defaults to
`packer-{{timestamp}}`.

Expand Down
14 changes: 12 additions & 2 deletions builder/googlecompute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type Config struct {
IAPConfig `mapstructure:",squash"`
// Skip creating the image. Useful for setting to `true` during a build test stage. Defaults to `false`.
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
// The architecture of the resulting image. Defaults to "x86_64".
ImageArchitecture string `mapstructure:"image_architecture" required:"false"`
// The unique name of the resulting image. Defaults to
// `packer-{{timestamp}}`.
ImageName string `mapstructure:"image_name" required:"false"`
Expand Down Expand Up @@ -443,16 +445,24 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
}
}

if c.ImageArchitecture == "" {
c.ImageArchitecture = "x86_64"
}

// used for ImageName and ImageFamily
imageErrorText := "Invalid image %s %q: The first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash"
if !validImageName.MatchString(c.ImageName) {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(imageErrorText, "name", c.ImageName))
}

if len(c.ImageName) > 63 {
errs = packersdk.MultiErrorAppend(errs,
errors.New("Invalid image name: Must not be longer than 63 characters"))
}

if !validImageName.MatchString(c.ImageName) {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf(imageErrorText, "name", c.ImageName))
if !(c.ImageArchitecture == "x86_64" || c.ImageArchitecture == "ARM64") {
errs = packersdk.MultiErrorAppend(errs,
errors.New("Invalid image architecture: Must be either x86_64 or ARM64"))
}

if len(c.ImageFamily) > 63 {
Expand Down
2 changes: 2 additions & 0 deletions builder/googlecompute/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion builder/googlecompute/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func TestConfigPrepare(t *testing.T) {
"foo",
false,
},

{
"zone",
nil,
Expand Down Expand Up @@ -685,6 +684,33 @@ func TestConfigExtraBlockDevice_create_image_multiple(t *testing.T) {
}
}

func TestConfigPrepareImageArchitecture(t *testing.T) {
cases := []struct {
Architecture string
ExpectValid bool
}{
{"x86_64", true},
{"ARM64", true},
{"i386", false}, // Assuming 'i386' is not supported, adjust as needed
}

for _, tc := range cases {
raw, tempfile := testConfig(t)
defer os.Remove(tempfile)

raw["image_architecture"] = tc.Architecture

var c Config
_, errs := c.Prepare(raw)

if tc.ExpectValid && errs != nil {
t.Errorf("Expected architecture '%s' to be valid, but got error: %s", tc.Architecture, errs)
} else if !tc.ExpectValid && errs == nil {
t.Errorf("Expected architecture '%s' to be invalid, but got no error", tc.Architecture)
}
}
}

// Helper stuff below

func testConfig(t *testing.T) (config map[string]interface{}, tempAccountFile string) {
Expand Down
1 change: 1 addition & 0 deletions builder/googlecompute/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *StepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul
})
}
imagePayload := &compute.Image{
Architecture: config.ImageArchitecture,
Description: config.ImageDescription,
Name: config.ImageName,
Family: config.ImageFamily,
Expand Down
2 changes: 2 additions & 0 deletions docs-partials/builder/googlecompute/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

- `skip_create_image` (bool) - Skip creating the image. Useful for setting to `true` during a build test stage. Defaults to `false`.

- `image_architecture` (string) - The architecture of the resulting image. Defaults to "x86_64".

- `image_name` (string) - The unique name of the resulting image. Defaults to
`packer-{{timestamp}}`.

Expand Down

0 comments on commit d0e0d1f

Please sign in to comment.