@@ -163,24 +163,17 @@ func (s *Service) CreateASG(machinePoolScope *scope.MachinePoolScope) (*expinfra
163163 return nil , fmt .Errorf ("getting subnets for ASG: %w" , err )
164164 }
165165
166- input := & expinfrav1.AutoScalingGroup {
167- Name : machinePoolScope .Name (),
168- MaxSize : machinePoolScope .AWSMachinePool .Spec .MaxSize ,
169- MinSize : machinePoolScope .AWSMachinePool .Spec .MinSize ,
170- Subnets : subnets ,
171- DefaultCoolDown : machinePoolScope .AWSMachinePool .Spec .DefaultCoolDown ,
172- DefaultInstanceWarmup : machinePoolScope .AWSMachinePool .Spec .DefaultInstanceWarmup ,
173- CapacityRebalance : machinePoolScope .AWSMachinePool .Spec .CapacityRebalance ,
174- MixedInstancesPolicy : machinePoolScope .AWSMachinePool .Spec .MixedInstancesPolicy ,
175- }
166+ name := machinePoolScope .Name ()
167+ s .scope .Info ("Creating ASG" , "name" , name )
176168
177169 // Default value of MachinePool replicas set by CAPI is 1.
178170 mpReplicas := * machinePoolScope .MachinePool .Spec .Replicas
171+ var desiredCapacity * int32
179172
180173 // Check that MachinePool replicas number is between the minimum and maximum size of the AWSMachinePool.
181174 // Ignore the problem for externally managed clusters because MachinePool replicas will be updated to the right value automatically.
182175 if mpReplicas >= machinePoolScope .AWSMachinePool .Spec .MinSize && mpReplicas <= machinePoolScope .AWSMachinePool .Spec .MaxSize {
183- input . DesiredCapacity = & mpReplicas
176+ desiredCapacity = & mpReplicas
184177 } else if ! annotations .ReplicasManagedByExternalAutoscaler (machinePoolScope .MachinePool ) {
185178 return nil , fmt .Errorf ("incorrect number of replicas %d in MachinePool %v" , mpReplicas , machinePoolScope .MachinePool .Name )
186179 }
@@ -194,62 +187,46 @@ func (s *Service) CreateASG(machinePoolScope *scope.MachinePoolScope) (*expinfra
194187 // Set the cloud provider tag
195188 additionalTags [infrav1 .ClusterAWSCloudProviderTagKey (s .scope .KubernetesClusterName ())] = string (infrav1 .ResourceLifecycleOwned )
196189
197- input .Tags = infrav1 .Build (infrav1.BuildParams {
198- ClusterName : s .scope .KubernetesClusterName (),
199- Lifecycle : infrav1 .ResourceLifecycleOwned ,
200- Name : aws .String (machinePoolScope .Name ()),
201- Role : aws .String ("node" ),
202- Additional : additionalTags ,
203- })
204-
205- s .scope .Info ("Running instance" )
206- if err := s .runPool (input , machinePoolScope .AWSMachinePool .Status .LaunchTemplateID ); err != nil {
207- // Only record the failure event if the error is not related to failed dependencies.
208- // This is to avoid spamming failure events since the machine will be requeued by the actuator.
209- // if !awserrors.IsFailedDependency(errors.Cause(err)) {
210- // record.Warnf(scope.AWSMachinePool, "FailedCreate", "Failed to create instance: %v", err)
211- // }
212- s .scope .Error (err , "unable to create AutoScalingGroup" )
213- return nil , err
214- }
215- record .Eventf (machinePoolScope .AWSMachinePool , "SuccessfulCreate" , "Created new ASG: %s" , machinePoolScope .Name ())
216-
217- return nil , nil
218- }
219-
220- func (s * Service ) runPool (i * expinfrav1.AutoScalingGroup , launchTemplateID string ) error {
221190 input := & autoscaling.CreateAutoScalingGroupInput {
222- AutoScalingGroupName : aws .String (i .Name ),
223- MaxSize : aws .Int64 (int64 (i .MaxSize )),
224- MinSize : aws .Int64 (int64 (i .MinSize )),
225- VPCZoneIdentifier : aws .String (strings .Join (i .Subnets , ", " )),
226- DefaultCooldown : aws .Int64 (int64 (i .DefaultCoolDown .Duration .Seconds ())),
227- DefaultInstanceWarmup : aws .Int64 (int64 (i .DefaultInstanceWarmup .Duration .Seconds ())),
228- CapacityRebalance : aws .Bool (i .CapacityRebalance ),
191+ AutoScalingGroupName : aws .String (name ),
192+ MaxSize : aws .Int64 (int64 (machinePoolScope .AWSMachinePool .Spec .MaxSize )),
193+ MinSize : aws .Int64 (int64 (machinePoolScope .AWSMachinePool .Spec .MinSize )),
194+ VPCZoneIdentifier : aws .String (strings .Join (subnets , ", " )),
195+ DefaultCooldown : aws .Int64 (int64 (machinePoolScope .AWSMachinePool .Spec .DefaultCoolDown .Duration .Seconds ())),
196+ DefaultInstanceWarmup : aws .Int64 (int64 (machinePoolScope .AWSMachinePool .Spec .DefaultInstanceWarmup .Duration .Seconds ())),
197+ CapacityRebalance : aws .Bool (machinePoolScope .AWSMachinePool .Spec .CapacityRebalance ),
198+ LifecycleHookSpecificationList : getLifecycleHookSpecificationList (machinePoolScope .GetLifecycleHooks ()),
229199 }
230200
231- if i . DesiredCapacity != nil {
232- input .DesiredCapacity = aws .Int64 (int64 (aws .Int32Value (i . DesiredCapacity )))
201+ if desiredCapacity != nil {
202+ input .DesiredCapacity = aws .Int64 (int64 (aws .Int32Value (desiredCapacity )))
233203 }
234204
235- if i .MixedInstancesPolicy != nil {
236- input .MixedInstancesPolicy = createSDKMixedInstancesPolicy (i . Name , i .MixedInstancesPolicy )
205+ if machinePoolScope . AWSMachinePool . Spec .MixedInstancesPolicy != nil {
206+ input .MixedInstancesPolicy = createSDKMixedInstancesPolicy (name , machinePoolScope . AWSMachinePool . Spec .MixedInstancesPolicy )
237207 } else {
238208 input .LaunchTemplate = & autoscaling.LaunchTemplateSpecification {
239- LaunchTemplateId : aws .String (launchTemplateID ),
209+ LaunchTemplateId : aws .String (machinePoolScope . AWSMachinePool . Status . LaunchTemplateID ),
240210 Version : aws .String (expinfrav1 .LaunchTemplateLatestVersion ),
241211 }
242212 }
243213
244- if i .Tags != nil {
245- input .Tags = BuildTagsFromMap (i .Name , i .Tags )
246- }
214+ input .Tags = BuildTagsFromMap (name , infrav1 .Build (infrav1.BuildParams {
215+ ClusterName : s .scope .KubernetesClusterName (),
216+ Lifecycle : infrav1 .ResourceLifecycleOwned ,
217+ Name : aws .String (name ),
218+ Role : aws .String ("node" ),
219+ Additional : additionalTags ,
220+ }))
247221
248222 if _ , err := s .ASGClient .CreateAutoScalingGroupWithContext (context .TODO (), input ); err != nil {
249- return errors .Wrap (err , "failed to create autoscaling group" )
223+ s .scope .Error (err , "unable to create AutoScalingGroup" )
224+ return nil , errors .Wrap (err , "failed to create autoscaling group" )
250225 }
251226
252- return nil
227+ record .Eventf (machinePoolScope .AWSMachinePool , "SuccessfulCreate" , "Created new ASG: %s" , machinePoolScope .Name ())
228+
229+ return nil , nil
253230}
254231
255232// DeleteASGAndWait will delete an ASG and wait until it is deleted.
0 commit comments