From 4997ec46623f8e63b59de843fe044b3e8a478967 Mon Sep 17 00:00:00 2001 From: Jose Armesto Date: Wed, 28 May 2025 16:11:41 +0200 Subject: [PATCH] Update launch template if capacity-block reservation id changes --- pkg/cloud/services/ec2/launchtemplate.go | 10 +++++++ pkg/cloud/services/ec2/launchtemplate_test.go | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/pkg/cloud/services/ec2/launchtemplate.go b/pkg/cloud/services/ec2/launchtemplate.go index 01e81fc013..ead242003a 100644 --- a/pkg/cloud/services/ec2/launchtemplate.go +++ b/pkg/cloud/services/ec2/launchtemplate.go @@ -712,6 +712,12 @@ func (s *Service) SDKToLaunchTemplate(d *ec2.LaunchTemplateVersion) (*expinfrav1 VersionNumber: d.VersionNumber, } + if v.CapacityReservationSpecification != nil && + v.CapacityReservationSpecification.CapacityReservationTarget != nil && + v.CapacityReservationSpecification.CapacityReservationTarget.CapacityReservationId != nil { + i.CapacityReservationID = v.CapacityReservationSpecification.CapacityReservationTarget.CapacityReservationId + } + if v.MetadataOptions != nil { i.InstanceMetadataOptions = &infrav1.InstanceMetadataOptions{ HTTPPutResponseHopLimit: aws.Int64Value(v.MetadataOptions.HttpPutResponseHopLimit), @@ -802,6 +808,10 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc return true, nil } + if !cmp.Equal(incoming.CapacityReservationID, existing.CapacityReservationID) { + return true, nil + } + if !cmp.Equal(incoming.PrivateDNSName, existing.PrivateDNSName) { return true, nil } diff --git a/pkg/cloud/services/ec2/launchtemplate_test.go b/pkg/cloud/services/ec2/launchtemplate_test.go index 93fd040f21..99ed68bf45 100644 --- a/pkg/cloud/services/ec2/launchtemplate_test.go +++ b/pkg/cloud/services/ec2/launchtemplate_test.go @@ -860,6 +860,36 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) { want: true, wantErr: false, }, + { + name: "Should return true if capacity reservation IDs are different", + incoming: &expinfrav1.AWSLaunchTemplate{ + CapacityReservationID: aws.String("new-reservation"), + }, + existing: &expinfrav1.AWSLaunchTemplate{ + AdditionalSecurityGroups: []infrav1.AWSResourceReference{ + {ID: aws.String("sg-111")}, + {ID: aws.String("sg-222")}, + }, + CapacityReservationID: aws.String("old-reservation"), + }, + want: true, + wantErr: false, + }, + { + name: "Should return true if one has capacity reservation ID and other doesn't", + incoming: &expinfrav1.AWSLaunchTemplate{ + CapacityReservationID: aws.String("new-reservation"), + }, + existing: &expinfrav1.AWSLaunchTemplate{ + AdditionalSecurityGroups: []infrav1.AWSResourceReference{ + {ID: aws.String("sg-111")}, + {ID: aws.String("sg-222")}, + }, + CapacityReservationID: nil, + }, + want: true, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {