Skip to content

Commit ad00c64

Browse files
committed
Don't re-reconcile when adopting resources
Unlike referenced resources, which specify a future intent, adoption only affects resources which have already been created. Successive executions will produce the same output, so there is no need to re-reconcile if we found orphaned resources. Also we seem to be hitting this a lot in practise, most likely due to the controller-runtime read-after-write cache inconsistency issue.
1 parent 567469e commit ad00c64

File tree

5 files changed

+11
-22
lines changed

5 files changed

+11
-22
lines changed

controllers/openstackcluster_controller.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,13 @@ func resolveBastionResources(scope *scope.WithLogger, openStackCluster *infrav1.
231231
return true, nil
232232
}
233233

234-
changed, err = compute.AdoptDependentMachineResources(scope,
234+
err = compute.AdoptDependentMachineResources(scope,
235235
bastionName(openStackCluster.Name),
236236
&openStackCluster.Status.Bastion.ReferencedResources,
237237
&openStackCluster.Status.Bastion.DependentResources)
238238
if err != nil {
239239
return false, err
240240
}
241-
if changed {
242-
// If the dependent resources have changed, we need to update the OpenStackCluster status now.
243-
return true, nil
244-
}
245241
}
246242
return false, nil
247243
}

controllers/openstackmachine_controller.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,10 @@ func (r *OpenStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
161161
}
162162

163163
// Adopt any existing dependent resources
164-
changed, err = compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
164+
err = compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
165165
if err != nil {
166166
return reconcile.Result{}, err
167167
}
168-
if changed {
169-
// If the dependent resources have changed, we need to update the OpenStackMachine status now.
170-
return reconcile.Result{}, nil
171-
}
172168

173169
// Handle deleted machines
174170
if !openStackMachine.DeletionTimestamp.IsZero() {

pkg/cloud/services/compute/dependent_resources.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
2323
)
2424

25-
func AdoptDependentMachineResources(scope *scope.WithLogger, baseName string, referencedResources *infrav1.ReferencedMachineResources, dependentResources *infrav1.DependentMachineResources) (bool, error) {
25+
func AdoptDependentMachineResources(scope *scope.WithLogger, baseName string, referencedResources *infrav1.ReferencedMachineResources, dependentResources *infrav1.DependentMachineResources) error {
2626
networkingService, err := networking.NewService(scope)
2727
if err != nil {
28-
return false, err
28+
return err
2929
}
3030

3131
return networkingService.AdoptPorts(scope, baseName, referencedResources.Ports, dependentResources)

pkg/cloud/services/networking/port.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,10 @@ func (s *Service) IsTrunkExtSupported() (trunknSupported bool, err error) {
571571

572572
// AdoptPorts looks for ports in desiredPorts which were previously created, and adds them to dependentResources.Ports.
573573
// A port matches if it has the same name and network ID as the desired port.
574-
func (s *Service) AdoptPorts(scope *scope.WithLogger, baseName string, desiredPorts []infrav1.PortOpts, dependentResources *infrav1.DependentMachineResources) (changed bool, err error) {
575-
changed = false
576-
574+
func (s *Service) AdoptPorts(scope *scope.WithLogger, baseName string, desiredPorts []infrav1.PortOpts, dependentResources *infrav1.DependentMachineResources) error {
577575
// We can skip adoption if the ports are already in the status
578576
if len(desiredPorts) == len(dependentResources.Ports) {
579-
return changed, nil
577+
return nil
580578
}
581579

582580
scope.Logger().V(5).Info("Adopting ports")
@@ -598,24 +596,23 @@ func (s *Service) AdoptPorts(scope *scope.WithLogger, baseName string, desiredPo
598596
NetworkID: port.Network.ID,
599597
})
600598
if err != nil {
601-
return changed, fmt.Errorf("searching for existing port %s in network %s: %v", portName, port.Network.ID, err)
599+
return fmt.Errorf("searching for existing port %s in network %s: %v", portName, port.Network.ID, err)
602600
}
603601
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
604602
// and will be created after the adoption
605603
if len(ports) == 0 {
606604
scope.Logger().V(5).Info("Port not found, stopping the adoption of ports", "port index", i)
607-
return changed, nil
605+
return nil
608606
}
609607
if len(ports) > 1 {
610-
return changed, fmt.Errorf("found multiple ports with name %s", portName)
608+
return fmt.Errorf("found multiple ports with name %s", portName)
611609
}
612610

613611
// The desired port was found, so we add it to the status
614612
portID := ports[0].ID
615613
scope.Logger().Info("Adopted previously created port which was not in status", "port index", i, "portID", portID)
616614
dependentResources.Ports = append(dependentResources.Ports, infrav1.PortStatus{ID: portID})
617-
changed = true
618615
}
619616

620-
return changed, nil
617+
return nil
621618
}

pkg/cloud/services/networking/port_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ func Test_AdoptPorts(t *testing.T) {
971971
client: mockClient,
972972
}
973973

974-
_, err := s.AdoptPorts(scope.NewWithLogger(mockScopeFactory, log),
974+
err := s.AdoptPorts(scope.NewWithLogger(mockScopeFactory, log),
975975
"test-machine",
976976
tt.desiredPorts, &tt.dependentResources)
977977
if tt.wantErr {

0 commit comments

Comments
 (0)