Skip to content

Commit

Permalink
Refactor reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed May 24, 2023
1 parent 83aa946 commit 6fb90d2
Show file tree
Hide file tree
Showing 2 changed files with 898 additions and 881 deletions.
58 changes: 27 additions & 31 deletions azure/services/scalesets/scalesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,39 @@ func (s *Service) Reconcile(ctx context.Context) (retErr error) {
}

scaleSetSpec.VMSSInstances = vmssInstances
s.Scope.SaveVMImageToStatus(scaleSetSpec.VMImage)

_, err = s.CreateOrUpdateResource(ctx, scaleSetSpec, serviceName)
if err != nil {
// TODO: ???
s.Scope.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, err)
}

// TODO: figure out how to
image, err := s.Scope.GetVMImage(ctx)
if err != nil {
return errors.Wrap(err, "failed to get VM image")
}

s.Scope.SaveVMImageToStatus(image)

fetchedVMSS, err := s.getVirtualMachineScaleSet(ctx, scaleSetSpec)
if err != nil && !azure.ResourceNotFound(err) {
log.Error(err, "failed to get vmss in deferred update")
}
result, err := s.CreateOrUpdateResource(ctx, scaleSetSpec, serviceName)
s.Scope.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, err)

if fetchedVMSS != nil {
if err := s.Scope.ReconcileReplicas(ctx, fetchedVMSS); err != nil {
// TODO: move this so we don't return
s.Scope.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, err)
return errors.Wrap(err, "unable to reconcile replicas")
if err == nil && result != nil {
vmss, ok := result.(compute.VirtualMachineScaleSet)
if !ok {
return errors.Errorf("%T is not a compute.VirtualMachineScaleSet", result)
}

// Transform the VMSS resource representation to conform to the cloud-provider-azure representation
providerID, err := azprovider.ConvertResourceGroupNameToLower(azure.ProviderIDPrefix + fetchedVMSS.ID)
vmssInstances, err := s.Client.ListInstances(ctx, spec)
if err != nil {
log.Error(err, "failed to parse VMSS ID", "ID", fetchedVMSS.ID)
return errors.Wrap(err, "failed to list instances")
}
s.Scope.SetProviderID(providerID)
s.Scope.SetVMSSState(fetchedVMSS)
}

s.Scope.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, err)
fetchedVMSS := converters.SDKToVMSS(vmss, vmssInstances)
// Note: converters.SDKToVMSS() will never return nil but we should check anyway
if fetchedVMSS != nil {
if err := s.Scope.ReconcileReplicas(ctx, fetchedVMSS); err != nil {
// TODO: move this so we don't return
s.Scope.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, err)
return errors.Wrap(err, "unable to reconcile replicas")
}

// Transform the VMSS resource representation to conform to the cloud-provider-azure representation
providerID, err := azprovider.ConvertResourceGroupNameToLower(azure.ProviderIDPrefix + fetchedVMSS.ID)
if err != nil {
log.Error(err, "failed to parse VMSS ID", "ID", fetchedVMSS.ID)
}
s.Scope.SetProviderID(providerID)
s.Scope.SetVMSSState(fetchedVMSS)
}
}

return err
}
Expand Down
Loading

0 comments on commit 6fb90d2

Please sign in to comment.