@@ -71,21 +71,21 @@ func (s *Service) ReconcileLaunchTemplate(
7171 bootstrapDataHash := userdata .ComputeHash (bootstrapData )
7272
7373 scope .Info ("checking for existing launch template" )
74- launchTemplate , launchTemplateUserDataHash , launchTemplateUserDataSecretKey , err := ec2svc .GetLaunchTemplate (scope .LaunchTemplateName ())
74+ existingLaunchTemplate , launchTemplateUserDataHash , launchTemplateUserDataSecretKey , err := ec2svc .GetLaunchTemplate (scope .LaunchTemplateName ())
7575 if err != nil {
7676 conditions .MarkUnknown (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateNotFoundReason , "%s" , err .Error ())
7777 return err
7878 }
7979
80- imageID , err := ec2svc .DiscoverLaunchTemplateAMI (scope )
80+ currentlyUsedAMIID , err := ec2svc .DiscoverLaunchTemplateAMI (scope )
8181 if err != nil {
8282 conditions .MarkFalse (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateCreateFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
8383 return err
8484 }
8585
86- if launchTemplate == nil {
86+ if existingLaunchTemplate == nil {
8787 scope .Info ("no existing launch template found, creating" )
88- launchTemplateID , err := ec2svc .CreateLaunchTemplate (scope , imageID , * bootstrapDataSecretKey , bootstrapData )
88+ launchTemplateID , err := ec2svc .CreateLaunchTemplate (scope , currentlyUsedAMIID , * bootstrapDataSecretKey , bootstrapData )
8989 if err != nil {
9090 conditions .MarkFalse (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateCreateFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
9191 return err
@@ -119,21 +119,11 @@ func (s *Service) ReconcileLaunchTemplate(
119119 }
120120 }
121121
122- annotation , err := MachinePoolAnnotationJSON (scope , TagsLastAppliedAnnotation )
123- if err != nil {
124- return err
125- }
126-
127- // Check if the instance tags were changed. If they were, create a new LaunchTemplate.
128- tagsChanged , _ , _ , _ := tagsChanged (annotation , scope .AdditionalTags ()) //nolint:dogsled
129-
130- needsUpdate , err := ec2svc .LaunchTemplateNeedsUpdate (scope , scope .GetLaunchTemplate (), launchTemplate )
122+ needsUpdate , err := ec2svc .LaunchTemplateNeedsUpdate (scope , existingLaunchTemplate , currentlyUsedAMIID )
131123 if err != nil {
132124 return err
133125 }
134126
135- amiChanged := * imageID != * launchTemplate .AMI .ID
136-
137127 // `launchTemplateUserDataSecretKey` can be nil since it comes from a tag on the launch template
138128 // which may not exist in older launch templates created by older CAPA versions.
139129 // On change, we trigger instance refresh (rollout of new nodes). Therefore, do not consider it a change if the
@@ -142,7 +132,7 @@ func (s *Service) ReconcileLaunchTemplate(
142132 userDataSecretKeyChanged := launchTemplateUserDataSecretKey != nil && bootstrapDataSecretKey .String () != launchTemplateUserDataSecretKey .String ()
143133 launchTemplateNeedsUserDataSecretKeyTag := launchTemplateUserDataSecretKey == nil
144134
145- if needsUpdate || tagsChanged || amiChanged || userDataSecretKeyChanged {
135+ if needsUpdate || userDataSecretKeyChanged {
146136 canUpdate , err := canUpdateLaunchTemplate ()
147137 if err != nil {
148138 return err
@@ -157,14 +147,14 @@ func (s *Service) ReconcileLaunchTemplate(
157147
158148 // Create a new launch template version if there's a difference in configuration, tags,
159149 // userdata, OR we've discovered a new AMI ID.
160- if needsUpdate || tagsChanged || amiChanged || userDataHashChanged || userDataSecretKeyChanged || launchTemplateNeedsUserDataSecretKeyTag {
161- scope .Info ("creating new version for launch template" , "existing" , launchTemplate , "incoming" , scope .GetLaunchTemplate (), "needsUpdate" , needsUpdate , "tagsChanged" , tagsChanged , "amiChanged" , amiChanged , "userDataHashChanged" , userDataHashChanged , "userDataSecretKeyChanged" , userDataSecretKeyChanged )
150+ if needsUpdate || userDataHashChanged || userDataSecretKeyChanged || launchTemplateNeedsUserDataSecretKeyTag {
151+ scope .Info ("creating new version for launch template" , "existing" , existingLaunchTemplate , "incoming" , scope .GetLaunchTemplate (), "needsUpdate" , needsUpdate , "userDataHashChanged" , userDataHashChanged , "userDataSecretKeyChanged" , userDataSecretKeyChanged )
162152 // There is a limit to the number of Launch Template Versions.
163153 // We ensure that the number of versions does not grow without bound by following a simple rule: Before we create a new version, we delete one old version, if there is at least one old version that is not in use.
164154 if err := ec2svc .PruneLaunchTemplateVersions (scope .GetLaunchTemplateIDStatus ()); err != nil {
165155 return err
166156 }
167- if err := ec2svc .CreateLaunchTemplateVersion (scope .GetLaunchTemplateIDStatus (), scope , imageID , * bootstrapDataSecretKey , bootstrapData ); err != nil {
157+ if err := ec2svc .CreateLaunchTemplateVersion (scope .GetLaunchTemplateIDStatus (), scope , currentlyUsedAMIID , * bootstrapDataSecretKey , bootstrapData ); err != nil {
168158 return err
169159 }
170160 version , err := ec2svc .GetLaunchTemplateLatestVersion (scope .GetLaunchTemplateIDStatus ())
@@ -178,7 +168,7 @@ func (s *Service) ReconcileLaunchTemplate(
178168 }
179169 }
180170
181- if needsUpdate || tagsChanged || amiChanged || userDataSecretKeyChanged {
171+ if needsUpdate || userDataSecretKeyChanged {
182172 if err := runPostLaunchTemplateUpdateOperation (); err != nil {
183173 conditions .MarkFalse (scope .GetSetter (), expinfrav1 .PostLaunchTemplateUpdateOperationCondition , expinfrav1 .PostLaunchTemplateUpdateOperationFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
184174 return err
@@ -782,25 +772,30 @@ func (s *Service) SDKToLaunchTemplate(d *ec2.LaunchTemplateVersion) (*expinfrav1
782772}
783773
784774// LaunchTemplateNeedsUpdate checks if a new launch template version is needed.
785- //
786- // FIXME(dlipovetsky): This check should account for changed userdata, but does not yet do so.
787- // Although userdata is stored in an EC2 Launch Template, it is not a field of AWSLaunchTemplate.
788- func (s * Service ) LaunchTemplateNeedsUpdate (scope scope.LaunchTemplateScope , incoming * expinfrav1.AWSLaunchTemplate , existing * expinfrav1.AWSLaunchTemplate ) (bool , error ) {
789- if incoming .IamInstanceProfile != existing .IamInstanceProfile {
775+ func (s * Service ) LaunchTemplateNeedsUpdate (scope scope.LaunchTemplateScope , existingLaunchTemplate * expinfrav1.AWSLaunchTemplate , currentlyUsedAMIID * string ) (bool , error ) {
776+ incomingLaunchTemplate := scope .GetLaunchTemplate ()
777+
778+ if incomingLaunchTemplate .IamInstanceProfile != existingLaunchTemplate .IamInstanceProfile {
790779 return true , nil
791780 }
792781
793- if incoming .InstanceType != existing .InstanceType {
782+ if incomingLaunchTemplate .InstanceType != existingLaunchTemplate .InstanceType {
794783 return true , nil
795784 }
796- if ! cmp .Equal (incoming .InstanceMetadataOptions , existing .InstanceMetadataOptions ) {
785+
786+ if ! cmp .Equal (incomingLaunchTemplate .InstanceMetadataOptions , existingLaunchTemplate .InstanceMetadataOptions ) {
797787 return true , nil
798788 }
799- if ! cmp .Equal (incoming .SpotMarketOptions , existing .SpotMarketOptions ) {
789+
790+ if ! cmp .Equal (incomingLaunchTemplate .SpotMarketOptions , existingLaunchTemplate .SpotMarketOptions ) {
800791 return true , nil
801792 }
802793
803- incomingIDs , err := s .GetAdditionalSecurityGroupsIDs (incoming .AdditionalSecurityGroups )
794+ if incomingLaunchTemplate .AMI .ID != nil && * incomingLaunchTemplate .AMI .ID != * currentlyUsedAMIID {
795+ return true , nil
796+ }
797+
798+ incomingIDs , err := s .GetAdditionalSecurityGroupsIDs (incomingLaunchTemplate .AdditionalSecurityGroups )
804799 if err != nil {
805800 return false , err
806801 }
@@ -811,7 +806,7 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
811806 }
812807
813808 incomingIDs = append (incomingIDs , coreIDs ... )
814- existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existing .AdditionalSecurityGroups )
809+ existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existingLaunchTemplate .AdditionalSecurityGroups )
815810 if err != nil {
816811 return false , err
817812 }
@@ -822,6 +817,15 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
822817 return true , nil
823818 }
824819
820+ annotation , err := MachinePoolAnnotationJSON (scope , TagsLastAppliedAnnotation )
821+ if err != nil {
822+ return false , err
823+ }
824+ tagsHaveChanged , _ , _ , _ := tagsChanged (annotation , scope .AdditionalTags ())
825+ if tagsHaveChanged {
826+ return true , nil
827+ }
828+
825829 return false , nil
826830}
827831
0 commit comments