@@ -97,7 +97,7 @@ func (s *Service) Reconcile(ctx context.Context) (retErr error) {
9797 return errors .Errorf ("%T is not of type ScaleSetSpec" , spec )
9898 }
9999
100- _ , err := s .Client .Get (ctx , spec )
100+ result , err := s .Client .Get (ctx , spec )
101101 if err == nil {
102102 // We can only get the existing instances if the VMSS already exists
103103 scaleSetSpec .VMSSInstances , err = s .Client .ListInstances (ctx , spec .ResourceGroupName (), spec .ResourceName ())
@@ -106,34 +106,51 @@ func (s *Service) Reconcile(ctx context.Context) (retErr error) {
106106 s .Scope .UpdatePutStatus (infrav1 .BootstrapSucceededCondition , serviceName , err )
107107 return err
108108 }
109+ if result != nil {
110+ if err := s .updateScopeState (ctx , result , scaleSetSpec ); err != nil {
111+ return err
112+ }
113+ }
109114 } else if ! azure .ResourceNotFound (err ) {
110115 return errors .Wrapf (err , "failed to get existing VMSS" )
111116 }
112117
113- result , err : = s .CreateOrUpdateResource (ctx , scaleSetSpec , serviceName )
118+ result , err = s .CreateOrUpdateResource (ctx , scaleSetSpec , serviceName )
114119 s .Scope .UpdatePutStatus (infrav1 .BootstrapSucceededCondition , serviceName , err )
115120
116121 if err == nil && result != nil {
117- vmss , ok := result .(armcompute.VirtualMachineScaleSet )
118- if ! ok {
119- return errors .Errorf ("%T is not an armcompute.VirtualMachineScaleSet" , result )
122+ if err := s .updateScopeState (ctx , result , scaleSetSpec ); err != nil {
123+ return err
120124 }
125+ }
121126
122- fetchedVMSS := converters .SDKToVMSS (vmss , scaleSetSpec .VMSSInstances )
123- if err := s .Scope .ReconcileReplicas (ctx , & fetchedVMSS ); err != nil {
124- return errors .Wrap (err , "unable to reconcile VMSS replicas" )
125- }
127+ return err
128+ }
126129
127- // Transform the VMSS resource representation to conform to the cloud-provider-azure representation
128- providerID , err := azprovider .ConvertResourceGroupNameToLower (azureutil .ProviderIDPrefix + fetchedVMSS .ID )
129- if err != nil {
130- return errors .Wrapf (err , "failed to parse VMSS ID %s" , fetchedVMSS .ID )
131- }
132- s .Scope .SetProviderID (providerID )
133- s .Scope .SetVMSSState (& fetchedVMSS )
130+ // updateScopeState updates the scope's VMSS state and provider ID
131+ //
132+ // Code later in the reconciler uses scope's VMSS state for determining scale status and whether to create/delete
133+ // AzureMachinePoolMachines.
134+ // N.B.: before calling this function, make sure scaleSetSpec.VMSSInstances is updated to the latest state.
135+ func (s * Service ) updateScopeState (ctx context.Context , result interface {}, scaleSetSpec * ScaleSetSpec ) error {
136+ vmss , ok := result .(armcompute.VirtualMachineScaleSet )
137+ if ! ok {
138+ return errors .Errorf ("%T is not an armcompute.VirtualMachineScaleSet" , result )
134139 }
135140
136- return err
141+ fetchedVMSS := converters .SDKToVMSS (vmss , scaleSetSpec .VMSSInstances )
142+ if err := s .Scope .ReconcileReplicas (ctx , & fetchedVMSS ); err != nil {
143+ return errors .Wrap (err , "unable to reconcile VMSS replicas" )
144+ }
145+
146+ // Transform the VMSS resource representation to conform to the cloud-provider-azure representation
147+ providerID , err := azprovider .ConvertResourceGroupNameToLower (azureutil .ProviderIDPrefix + fetchedVMSS .ID )
148+ if err != nil {
149+ return errors .Wrapf (err , "failed to parse VMSS ID %s" , fetchedVMSS .ID )
150+ }
151+ s .Scope .SetProviderID (providerID )
152+ s .Scope .SetVMSSState (& fetchedVMSS )
153+ return nil
137154}
138155
139156// Delete deletes a scale set asynchronously. Delete sends a DELETE request to Azure and if accepted without error,
0 commit comments