From 902497d6ce6b6b8ff512a9963476bd7fd54c0a43 Mon Sep 17 00:00:00 2001 From: Sargun Dhillon Date: Tue, 4 Sep 2018 18:13:18 -0700 Subject: [PATCH] Allow users to build hvm images from pv images to have ena_support --- builder/amazon/chroot/builder.go | 1 + builder/amazon/common/step_source_ami_info.go | 26 +++++++++--- .../common/step_source_ami_info_test.go | 42 +++++++++++++++++++ builder/amazon/ebs/builder.go | 1 + builder/amazon/ebssurrogate/builder.go | 1 + builder/amazon/instance/builder.go | 1 + 6 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 builder/amazon/common/step_source_ami_info_test.go diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 5c5830d17..c0453ba6b 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -223,6 +223,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, AmiFilters: b.config.SourceAmiFilter, + AMIVirtType: b.config.AMIVirtType, }, &StepCheckRootDevice{}, ) diff --git a/builder/amazon/common/step_source_ami_info.go b/builder/amazon/common/step_source_ami_info.go index 5e57c2fbf..cb7a08b51 100644 --- a/builder/amazon/common/step_source_ami_info.go +++ b/builder/amazon/common/step_source_ami_info.go @@ -21,6 +21,7 @@ type StepSourceAMIInfo struct { SourceAmi string EnableAMISriovNetSupport bool EnableAMIENASupport bool + AMIVirtType string AmiFilters AmiFilterOptions } @@ -105,11 +106,13 @@ func (s *StepSourceAMIInfo) Run(_ context.Context, state multistep.StateBag) mul // Enhanced Networking can only be enabled on HVM AMIs. // See http://goo.gl/icuXh5 - if (s.EnableAMIENASupport || s.EnableAMISriovNetSupport) && *image.VirtualizationType != "hvm" { - err := fmt.Errorf("Cannot enable enhanced networking, source AMI '%s' is not HVM", s.SourceAmi) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + if s.EnableAMIENASupport || s.EnableAMISriovNetSupport { + err = s.canEnableEnhancedNetworking(image) + if err != nil { + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } state.Put("source_image", image) @@ -117,3 +120,16 @@ func (s *StepSourceAMIInfo) Run(_ context.Context, state multistep.StateBag) mul } func (s *StepSourceAMIInfo) Cleanup(multistep.StateBag) {} + +func (s *StepSourceAMIInfo) canEnableEnhancedNetworking(image *ec2.Image) error { + if s.AMIVirtType == "hvm" { + return nil + } + if s.AMIVirtType != "" { + return fmt.Errorf("Cannot enable enhanced networking, AMIVirtType '%s' is not HVM", s.AMIVirtType) + } + if *image.VirtualizationType != "hvm" { + return fmt.Errorf("Cannot enable enhanced networking, source AMI '%s' is not HVM", s.SourceAmi) + } + return nil +} diff --git a/builder/amazon/common/step_source_ami_info_test.go b/builder/amazon/common/step_source_ami_info_test.go new file mode 100644 index 000000000..e0734bda1 --- /dev/null +++ b/builder/amazon/common/step_source_ami_info_test.go @@ -0,0 +1,42 @@ +package common + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestStepSourceAmiInfo_PVImage(t *testing.T) { + err := new(StepSourceAMIInfo).canEnableEnhancedNetworking(&ec2.Image{ + VirtualizationType: aws.String("paravirtual"), + }) + assert.Error(t, err) +} + +func TestStepSourceAmiInfo_HVMImage(t *testing.T) { + err := new(StepSourceAMIInfo).canEnableEnhancedNetworking(&ec2.Image{ + VirtualizationType: aws.String("hvm"), + }) + assert.NoError(t, err) +} + +func TestStepSourceAmiInfo_PVImageWithAMIVirtPV(t *testing.T) { + stepSourceAMIInfo := StepSourceAMIInfo{ + AMIVirtType: "paravirtual", + } + err := stepSourceAMIInfo.canEnableEnhancedNetworking(&ec2.Image{ + VirtualizationType: aws.String("paravirtual"), + }) + assert.Error(t, err) +} + +func TestStepSourceAmiInfo_PVImageWithAMIVirtHVM(t *testing.T) { + stepSourceAMIInfo := StepSourceAMIInfo{ + AMIVirtType: "hvm", + } + err := stepSourceAMIInfo.canEnableEnhancedNetworking(&ec2.Image{ + VirtualizationType: aws.String("paravirtual"), + }) + assert.NoError(t, err) +} diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 2fd29e87f..9184ea760 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -177,6 +177,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, AmiFilters: b.config.SourceAmiFilter, + AMIVirtType: b.config.AMIVirtType, }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index d9d5157a6..98f45de4d 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -194,6 +194,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, AmiFilters: b.config.SourceAmiFilter, + AMIVirtType: b.config.AMIVirtType, }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 051546d89..d7a3a43b2 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -255,6 +255,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, AmiFilters: b.config.SourceAmiFilter, + AMIVirtType: b.config.AMIVirtType, }, &awscommon.StepKeyPair{ Debug: b.config.PackerDebug,