@@ -71,21 +71,21 @@ func (s *Service) ReconcileLaunchTemplate(
71
71
bootstrapDataHash := userdata .ComputeHash (bootstrapData )
72
72
73
73
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 ())
75
75
if err != nil {
76
76
conditions .MarkUnknown (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateNotFoundReason , "%s" , err .Error ())
77
77
return err
78
78
}
79
79
80
- imageID , err := ec2svc .DiscoverLaunchTemplateAMI (scope )
80
+ currentlyUsedAMIID , err := ec2svc .DiscoverLaunchTemplateAMI (scope )
81
81
if err != nil {
82
82
conditions .MarkFalse (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateCreateFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
83
83
return err
84
84
}
85
85
86
- if launchTemplate == nil {
86
+ if existingLaunchTemplate == nil {
87
87
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 )
89
89
if err != nil {
90
90
conditions .MarkFalse (scope .GetSetter (), expinfrav1 .LaunchTemplateReadyCondition , expinfrav1 .LaunchTemplateCreateFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
91
91
return err
@@ -119,21 +119,11 @@ func (s *Service) ReconcileLaunchTemplate(
119
119
}
120
120
}
121
121
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 )
131
123
if err != nil {
132
124
return err
133
125
}
134
126
135
- amiChanged := * imageID != * launchTemplate .AMI .ID
136
-
137
127
// `launchTemplateUserDataSecretKey` can be nil since it comes from a tag on the launch template
138
128
// which may not exist in older launch templates created by older CAPA versions.
139
129
// 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(
142
132
userDataSecretKeyChanged := launchTemplateUserDataSecretKey != nil && bootstrapDataSecretKey .String () != launchTemplateUserDataSecretKey .String ()
143
133
launchTemplateNeedsUserDataSecretKeyTag := launchTemplateUserDataSecretKey == nil
144
134
145
- if needsUpdate || tagsChanged || amiChanged || userDataSecretKeyChanged {
135
+ if needsUpdate || userDataSecretKeyChanged {
146
136
canUpdate , err := canUpdateLaunchTemplate ()
147
137
if err != nil {
148
138
return err
@@ -157,14 +147,14 @@ func (s *Service) ReconcileLaunchTemplate(
157
147
158
148
// Create a new launch template version if there's a difference in configuration, tags,
159
149
// 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 )
162
152
// There is a limit to the number of Launch Template Versions.
163
153
// 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.
164
154
if err := ec2svc .PruneLaunchTemplateVersions (scope .GetLaunchTemplateIDStatus ()); err != nil {
165
155
return err
166
156
}
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 {
168
158
return err
169
159
}
170
160
version , err := ec2svc .GetLaunchTemplateLatestVersion (scope .GetLaunchTemplateIDStatus ())
@@ -178,7 +168,7 @@ func (s *Service) ReconcileLaunchTemplate(
178
168
}
179
169
}
180
170
181
- if needsUpdate || tagsChanged || amiChanged || userDataSecretKeyChanged {
171
+ if needsUpdate || userDataSecretKeyChanged {
182
172
if err := runPostLaunchTemplateUpdateOperation (); err != nil {
183
173
conditions .MarkFalse (scope .GetSetter (), expinfrav1 .PostLaunchTemplateUpdateOperationCondition , expinfrav1 .PostLaunchTemplateUpdateOperationFailedReason , clusterv1 .ConditionSeverityError , "%s" , err .Error ())
184
174
return err
@@ -788,39 +778,42 @@ func (s *Service) SDKToLaunchTemplate(d *ec2.LaunchTemplateVersion) (*expinfrav1
788
778
}
789
779
790
780
// LaunchTemplateNeedsUpdate checks if a new launch template version is needed.
791
- //
792
- // FIXME(dlipovetsky): This check should account for changed userdata, but does not yet do so.
793
- // Although userdata is stored in an EC2 Launch Template, it is not a field of AWSLaunchTemplate.
794
- func (s * Service ) LaunchTemplateNeedsUpdate (scope scope.LaunchTemplateScope , incoming * expinfrav1.AWSLaunchTemplate , existing * expinfrav1.AWSLaunchTemplate ) (bool , error ) {
795
- if incoming .IamInstanceProfile != existing .IamInstanceProfile {
781
+ func (s * Service ) LaunchTemplateNeedsUpdate (scope scope.LaunchTemplateScope , existingLaunchTemplate * expinfrav1.AWSLaunchTemplate , currentlyUsedAMIID * string ) (bool , error ) {
782
+ incomingLaunchTemplate := scope .GetLaunchTemplate ()
783
+
784
+ if incomingLaunchTemplate .IamInstanceProfile != existingLaunchTemplate .IamInstanceProfile {
796
785
return true , nil
797
786
}
798
787
799
- if incoming .InstanceType != existing .InstanceType {
788
+ if incomingLaunchTemplate .InstanceType != existingLaunchTemplate .InstanceType {
800
789
return true , nil
801
790
}
802
791
803
- if ! cmp .Equal (incoming .InstanceMetadataOptions , existing .InstanceMetadataOptions ) {
792
+ if ! cmp .Equal (incomingLaunchTemplate .InstanceMetadataOptions , existingLaunchTemplate .InstanceMetadataOptions ) {
804
793
return true , nil
805
794
}
806
795
807
- if ! cmp .Equal (incoming .SpotMarketOptions , existing .SpotMarketOptions ) {
796
+ if ! cmp .Equal (incomingLaunchTemplate .SpotMarketOptions , existingLaunchTemplate .SpotMarketOptions ) {
808
797
return true , nil
809
798
}
810
799
811
- if ! cmp . Equal ( incoming . CapacityReservationID , existing . CapacityReservationID ) {
800
+ if incomingLaunchTemplate . AMI . ID != nil && * incomingLaunchTemplate . AMI . ID != * currentlyUsedAMIID {
812
801
return true , nil
813
802
}
814
803
815
- if ! cmp .Equal (incoming . PrivateDNSName , existing . PrivateDNSName ) {
804
+ if ! cmp .Equal (incomingLaunchTemplate . CapacityReservationID , existingLaunchTemplate . CapacityReservationID ) {
816
805
return true , nil
817
806
}
818
807
819
- if ! cmp .Equal (incoming . SSHKeyName , existing . SSHKeyName ) {
808
+ if ! cmp .Equal (incomingLaunchTemplate . PrivateDNSName , existingLaunchTemplate . PrivateDNSName ) {
820
809
return true , nil
821
810
}
822
811
823
- incomingIDs , err := s .GetAdditionalSecurityGroupsIDs (incoming .AdditionalSecurityGroups )
812
+ if ! cmp .Equal (incomingLaunchTemplate .SSHKeyName , existingLaunchTemplate .SSHKeyName ) {
813
+ return true , nil
814
+ }
815
+
816
+ incomingIDs , err := s .GetAdditionalSecurityGroupsIDs (incomingLaunchTemplate .AdditionalSecurityGroups )
824
817
if err != nil {
825
818
return false , err
826
819
}
@@ -831,7 +824,7 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
831
824
}
832
825
833
826
incomingIDs = append (incomingIDs , coreIDs ... )
834
- existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existing .AdditionalSecurityGroups )
827
+ existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existingLaunchTemplate .AdditionalSecurityGroups )
835
828
if err != nil {
836
829
return false , err
837
830
}
@@ -842,6 +835,16 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
842
835
return true , nil
843
836
}
844
837
838
+ annotation , err := MachinePoolAnnotationJSON (scope , TagsLastAppliedAnnotation )
839
+ if err != nil {
840
+ return false , err
841
+ }
842
+ //nolint:dogsled
843
+ tagsHaveChanged , _ , _ , _ := tagsChanged (annotation , scope .AdditionalTags ())
844
+ if tagsHaveChanged {
845
+ return true , nil
846
+ }
847
+
845
848
return false , nil
846
849
}
847
850
0 commit comments