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