Skip to content

Commit

Permalink
fix: use location from machine status if present
Browse files Browse the repository at this point in the history
  • Loading branch information
jriedel-ionos committed Jul 12, 2024
1 parent c3edff1 commit 1ff905a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions internal/service/cloud/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ func (s *Service) getFailoverIPBlock(ctx context.Context, ms *scope.Machine) (*s
return nil, fmt.Errorf("failed to list IP blocks: %w", err)
}

location, err := s.ionosClient.GetDatacenterLocationByID(ctx, ms.IonosMachine.Spec.DatacenterID)
location, err := s.getLocation(ctx, ms)
if err != nil {
return nil, fmt.Errorf("failed to get location of datacenter: %w", err)
return nil, err
}

for _, block := range ptr.Deref(blocks.GetItems(), nil) {
Expand Down Expand Up @@ -339,9 +339,9 @@ func (s *Service) reserveControlPlaneEndpointIPBlock(ctx context.Context, cs *sc

func (s *Service) reserveMachineDeploymentFailoverIPBlock(ctx context.Context, ms *scope.Machine) error {
log := s.logger.WithName("reserveMachineDeploymentFailoverIPBlock")
location, err := s.ionosClient.GetDatacenterLocationByID(ctx, ms.IonosMachine.Spec.DatacenterID)
location, err := s.getLocation(ctx, ms)
if err != nil {
return fmt.Errorf("failed to get location of datacenter: %w", err)
return err
}

ms.IonosMachine.Status.Location = location
Expand Down Expand Up @@ -413,9 +413,9 @@ func (s *Service) getLatestControlPlaneEndpointIPBlockCreationRequest(

// getLatestFailoverIPBlockCreateRequest returns the latest failover IP block creation request.
func (s *Service) getLatestFailoverIPBlockCreateRequest(ctx context.Context, ms *scope.Machine) (*requestInfo, error) {
location, err := s.ionosClient.GetDatacenterLocationByID(ctx, ms.IonosMachine.Spec.DatacenterID)
location, err := s.getLocation(ctx, ms)
if err != nil {
return nil, fmt.Errorf("failed to get location of datacenter: %w", err)
return nil, err
}
return s.getLatestIPBlockRequestByNameAndLocation(
ctx, http.MethodPost,
Expand Down Expand Up @@ -476,3 +476,17 @@ func ignoreErrUserSetIPNotFound(err error) error {
}
return err
}

// getLocation checks if the location of the machine is already set in the status.
// If not, it queries Cloud API to get the location of the datacenter.
func (s *Service) getLocation(ctx context.Context, ms *scope.Machine) (string, error) {
if ms.IonosMachine.Status.Location == "" {
location, err := s.ionosClient.GetDatacenterLocationByID(ctx, ms.IonosMachine.Spec.DatacenterID)
if err != nil {
return "", fmt.Errorf("failed to get location of datacenter: %w", err)
}
ms.IonosMachine.Status.Location = location
return location, nil
}
return ms.IonosMachine.Status.Location, nil
}
2 changes: 1 addition & 1 deletion internal/service/cloud/ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (s *ipBlockTestSuite) TestReconcileFailoverIPBlockDeletionSkipped() {
func (s *ipBlockTestSuite) TestReconcileFailoverIPBlockDeletionPendingCreation() {
s.infraMachine.Spec.FailoverIP = ptr.To(infrav1.CloudResourceConfigAuto)

s.mockGetDatacenterLocationByIDCall(exampleDatacenterID).Return(exampleLocation, nil).Twice()
s.mockGetDatacenterLocationByIDCall(exampleDatacenterID).Return(exampleLocation, nil).Once()
s.mockListIPBlocksCall().Return(nil, nil).Once()
s.mockGetIPBlocksRequestsPostCall().Return([]sdk.Request{
s.buildIPBlockRequestWithName(
Expand Down
2 changes: 1 addition & 1 deletion internal/service/cloud/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (s *lanSuite) TestReconcileIPFailoverReserveIPBlock() {
s.infraMachine.SetLabels(map[string]string{clusterv1.MachineDeploymentNameLabel: deploymentLabel})
s.infraMachine.Spec.FailoverIP = ptr.To(infrav1.CloudResourceConfigAuto)

s.mockGetDatacenterLocationByIDCall(exampleDatacenterID).Return(exampleLocation, nil).Times(3)
s.mockGetDatacenterLocationByIDCall(exampleDatacenterID).Return(exampleLocation, nil).Once()
s.mockListIPBlocksCall().Return(nil, nil).Once()
s.mockGetIPBlocksRequestsPostCall().Return(nil, nil).Once()
s.mockReserveIPBlockCall(s.service.failoverIPBlockName(s.machineScope), s.clusterScope.Location()).
Expand Down

0 comments on commit 1ff905a

Please sign in to comment.