diff --git a/pkg/eks/create.go b/pkg/eks/create.go index 3030cdb8..d438b336 100644 --- a/pkg/eks/create.go +++ b/pkg/eks/create.go @@ -312,7 +312,7 @@ func CreateNodeGroup(opts *CreateNodeGroupOptions) (string, string, error) { } _, err = opts.EKSService.CreateNodegroup(nodeGroupCreateInput) - if err != nil { + if err != nil && lt.ID != nil { // If there was an error creating the node group, then the template version should be deleted // to prevent many launch template versions from being created before the issue is fixed. DeleteLaunchTemplateVersions(opts.EC2Service, *lt.ID, []*string{launchTemplateVersion}) diff --git a/pkg/eks/create_test.go b/pkg/eks/create_test.go index 3a22b418..ec4861c9 100644 --- a/pkg/eks/create_test.go +++ b/pkg/eks/create_test.go @@ -1023,6 +1023,58 @@ var _ = Describe("CreateNodeGroup", func() { Expect(launchTemplateVersion).To(Equal("1")) Expect(generatedNodeRole).To(Equal("test")) }) + + It("handles no id case gracefully", func() { + createNodeGroupOpts.NodeGroup.ImageID = nil + ec2ServiceMock.EXPECT().CreateLaunchTemplateVersion(gomock.Any()).Return(&ec2.CreateLaunchTemplateVersionOutput{ + LaunchTemplateVersion: &ec2.LaunchTemplateVersion{ + LaunchTemplateName: aws.String("test"), + LaunchTemplateId: nil, + VersionNumber: aws.Int64(1), + }, + }, nil) + + cloudFormationServiceMock.EXPECT().CreateStack(gomock.Any()).Return(nil, nil) + + cloudFormationServiceMock.EXPECT().DescribeStacks(gomock.Any()).Return( + &cloudformation.DescribeStacksOutput{ + Stacks: []*cloudformation.Stack{ + { + StackStatus: aws.String(createCompleteStatus), + Outputs: []*cloudformation.Output{ + { + OutputKey: aws.String("NodeInstanceRole"), + OutputValue: aws.String("test"), + }, + }, + }, + }, + }, nil) + + eksServiceMock.EXPECT().CreateNodegroup(&eks.CreateNodegroupInput{ + ClusterName: aws.String(createNodeGroupOpts.Config.Spec.DisplayName), + NodegroupName: createNodeGroupOpts.NodeGroup.NodegroupName, + Labels: createNodeGroupOpts.NodeGroup.Labels, + ScalingConfig: &eks.NodegroupScalingConfig{ + DesiredSize: createNodeGroupOpts.NodeGroup.DesiredSize, + MaxSize: createNodeGroupOpts.NodeGroup.MaxSize, + MinSize: createNodeGroupOpts.NodeGroup.MinSize, + }, + CapacityType: aws.String(eks.CapacityTypesSpot), + LaunchTemplate: &eks.LaunchTemplateSpecification{ + Id: nil, + Version: aws.String("1"), + }, + InstanceTypes: createNodeGroupOpts.NodeGroup.SpotInstanceTypes, + Subnets: aws.StringSlice(createNodeGroupOpts.NodeGroup.Subnets), + NodeRole: aws.String("test"), + AmiType: aws.String(eks.AMITypesAl2X8664), + }).Return(nil, errors.New("error")) + + _, _, err := CreateNodeGroup(createNodeGroupOpts) + Expect(err).To(HaveOccurred()) + }) + It("set resource tags", func() { createNodeGroupOpts.NodeGroup.ResourceTags = map[string]*string{ "tag1": aws.String("val1"),