From 1ff905a3cc6e32df37086509fb425a801b0a55f2 Mon Sep 17 00:00:00 2001 From: Jonas Riedel Date: Fri, 12 Jul 2024 13:12:13 +0200 Subject: [PATCH] fix: use location from machine status if present --- internal/service/cloud/ipblock.go | 26 ++++++++++++++++++++------ internal/service/cloud/ipblock_test.go | 2 +- internal/service/cloud/network_test.go | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/internal/service/cloud/ipblock.go b/internal/service/cloud/ipblock.go index 12b485c7..a8df3e7e 100644 --- a/internal/service/cloud/ipblock.go +++ b/internal/service/cloud/ipblock.go @@ -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) { @@ -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 @@ -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, @@ -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 +} diff --git a/internal/service/cloud/ipblock_test.go b/internal/service/cloud/ipblock_test.go index f5c2eb41..28dbcce4 100644 --- a/internal/service/cloud/ipblock_test.go +++ b/internal/service/cloud/ipblock_test.go @@ -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( diff --git a/internal/service/cloud/network_test.go b/internal/service/cloud/network_test.go index 01766eef..2eab32cd 100644 --- a/internal/service/cloud/network_test.go +++ b/internal/service/cloud/network_test.go @@ -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()).