@@ -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
@@ -782,25 +772,30 @@ func (s *Service) SDKToLaunchTemplate(d *ec2.LaunchTemplateVersion) (*expinfrav1
782
772
}
783
773
784
774
// 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 {
790
779
return true , nil
791
780
}
792
781
793
- if incoming .InstanceType != existing .InstanceType {
782
+ if incomingLaunchTemplate .InstanceType != existingLaunchTemplate .InstanceType {
794
783
return true , nil
795
784
}
796
- if ! cmp .Equal (incoming .InstanceMetadataOptions , existing .InstanceMetadataOptions ) {
785
+
786
+ if ! cmp .Equal (incomingLaunchTemplate .InstanceMetadataOptions , existingLaunchTemplate .InstanceMetadataOptions ) {
797
787
return true , nil
798
788
}
799
- if ! cmp .Equal (incoming .SpotMarketOptions , existing .SpotMarketOptions ) {
789
+
790
+ if ! cmp .Equal (incomingLaunchTemplate .SpotMarketOptions , existingLaunchTemplate .SpotMarketOptions ) {
800
791
return true , nil
801
792
}
802
793
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 )
804
799
if err != nil {
805
800
return false , err
806
801
}
@@ -811,7 +806,7 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
811
806
}
812
807
813
808
incomingIDs = append (incomingIDs , coreIDs ... )
814
- existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existing .AdditionalSecurityGroups )
809
+ existingIDs , err := s .GetAdditionalSecurityGroupsIDs (existingLaunchTemplate .AdditionalSecurityGroups )
815
810
if err != nil {
816
811
return false , err
817
812
}
@@ -822,6 +817,15 @@ func (s *Service) LaunchTemplateNeedsUpdate(scope scope.LaunchTemplateScope, inc
822
817
return true , nil
823
818
}
824
819
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
+
825
829
return false , nil
826
830
}
827
831
0 commit comments