Skip to content

Commit 1b07f9f

Browse files
committed
Deduplicate AdoptMachinePorts and AdoptBastionPorts
Both of these methods rely on ReferencedMachineResources and DependentMachineResources, so they can be easily refactored to have a common implementation.
1 parent 801f5ef commit 1b07f9f

File tree

6 files changed

+172
-322
lines changed

6 files changed

+172
-322
lines changed

controllers/openstackcluster_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ func resolveBastionResources(scope *scope.WithLogger, openStackCluster *infrav1.
234234
return true, nil
235235
}
236236

237-
changed, err = compute.ResolveDependentBastionResources(scope, openStackCluster, bastionName(openStackCluster.Name))
237+
changed, err = compute.AdoptDependentMachineResources(scope,
238+
bastionName(openStackCluster.Name),
239+
&openStackCluster.Status.Bastion.ReferencedResources,
240+
&openStackCluster.Status.Bastion.DependentResources)
238241
if err != nil {
239242
return false, err
240243
}

controllers/openstackmachine_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func (r *OpenStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
163163
return reconcile.Result{}, nil
164164
}
165165

166-
// Resolve and store dependent resources
167-
changed, err = compute.ResolveDependentMachineResources(scope, openStackMachine)
166+
// Adopt any existing dependent resources
167+
changed, err = compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
168168
if err != nil {
169169
return reconcile.Result{}, err
170170
}

pkg/cloud/services/compute/dependent_resources.go

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

25-
func ResolveDependentMachineResources(scope *scope.WithLogger, openStackMachine *infrav1.OpenStackMachine) (changed bool, err error) {
26-
changed = false
27-
28-
networkingService, err := networking.NewService(scope)
29-
if err != nil {
30-
return changed, err
31-
}
32-
33-
return networkingService.AdoptMachinePorts(scope, openStackMachine, openStackMachine.Status.ReferencedResources.Ports)
34-
}
35-
36-
func ResolveDependentBastionResources(scope *scope.WithLogger, openStackCluster *infrav1.OpenStackCluster, bastionName string) (changed bool, err error) {
37-
changed = false
38-
25+
func AdoptDependentMachineResources(scope *scope.WithLogger, baseName string, referencedResources *infrav1.ReferencedMachineResources, dependentResources *infrav1.DependentMachineResources) (bool, error) {
3926
networkingService, err := networking.NewService(scope)
4027
if err != nil {
41-
return changed, err
28+
return false, err
4229
}
4330

44-
return networkingService.AdoptBastionPorts(scope, openStackCluster, bastionName)
31+
return networkingService.AdoptPorts(scope, baseName, referencedResources.Ports, dependentResources)
4532
}

pkg/cloud/services/compute/dependent_resources_test.go

Lines changed: 0 additions & 219 deletions
This file was deleted.

pkg/cloud/services/networking/port.go

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -569,104 +569,36 @@ func (s *Service) IsTrunkExtSupported() (trunknSupported bool, err error) {
569569
return true, nil
570570
}
571571

572-
// AdoptMachinePorts checks if the ports are in ready condition. If not, it'll try to adopt them
573-
// by checking if they exist and if they do, it'll add them to the OpenStackMachine status.
574-
// A port is searched by name and network ID and has to be unique.
575-
// If the port is not found, it'll be ignored because it'll be created after the adoption.
576-
func (s *Service) AdoptMachinePorts(scope *scope.WithLogger, openStackMachine *infrav1.OpenStackMachine, desiredPorts []infrav1.PortOpts) (changed bool, err error) {
572+
// AdoptPorts looks for ports in desiredPorts which were previously created, and adds them to dependentResources.Ports.
573+
// 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) {
577575
changed = false
578576

579-
// We can skip adoption if the instance is ready because OpenStackMachine is immutable once ready
580-
// or if the ports are already in the status
581-
if openStackMachine.Status.Ready && len(openStackMachine.Status.DependentResources.Ports) == len(desiredPorts) {
582-
scope.Logger().V(5).Info("OpenStackMachine is ready, skipping the adoption of ports")
583-
return changed, nil
584-
}
585-
586-
scope.Logger().Info("Adopting ports for OpenStackMachine", "name", openStackMachine.Name)
587-
588-
// We create ports in order and adopt them in order in PortsStatus.
589-
// This means that if port N doesn't exist we know that ports >N don't exist.
590-
// We can therefore stop searching for ports once we find one that doesn't exist.
591-
for i, port := range desiredPorts {
592-
// check if the port is in status first and if it is, skip it
593-
if i < len(openStackMachine.Status.DependentResources.Ports) {
594-
scope.Logger().V(5).Info("Port already in status, skipping it", "port index", i)
595-
continue
596-
}
597-
598-
portOpts := &desiredPorts[i]
599-
portName := GetPortName(openStackMachine.Name, portOpts, i)
600-
ports, err := s.client.ListPort(ports.ListOpts{
601-
Name: portName,
602-
NetworkID: port.Network.ID,
603-
})
604-
if err != nil {
605-
return changed, fmt.Errorf("searching for existing port for machine %s: %v", openStackMachine.Name, err)
606-
}
607-
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
608-
// and will be created after the adoption
609-
if len(ports) == 0 {
610-
scope.Logger().V(5).Info("Port not found, stopping the adoption of ports", "port index", i)
611-
return changed, nil
612-
}
613-
if len(ports) > 1 {
614-
return changed, fmt.Errorf("found multiple ports with name %s", portName)
615-
}
616-
617-
// The desired port was found, so we add it to the status
618-
scope.Logger().V(5).Info("Port found, adding it to the status", "port index", i)
619-
openStackMachine.Status.DependentResources.Ports = append(openStackMachine.Status.DependentResources.Ports, infrav1.PortStatus{ID: ports[0].ID})
620-
changed = true
621-
}
622-
623-
return changed, nil
624-
}
625-
626-
// AdopteBastionPorts tries to adopt the ports for the bastion instance by checking if they exist and if they do,
627-
// it'll add them to the OpenStackCluster status.
628-
// A port is searched by name and network ID and has to be unique.
629-
// If the port is not found, it'll be ignored because it'll be created after the adoption.
630-
func (s *Service) AdoptBastionPorts(scope *scope.WithLogger, openStackCluster *infrav1.OpenStackCluster, bastionName string) (changed bool, err error) {
631-
changed = false
632-
633-
if openStackCluster.Status.Network == nil {
634-
scope.Logger().V(5).Info("Network status is nil, skipping the adoption of ports")
635-
return changed, nil
636-
}
637-
638-
if openStackCluster.Status.Bastion == nil {
639-
scope.Logger().V(5).Info("Bastion status is nil, initializing it")
640-
openStackCluster.Status.Bastion = &infrav1.BastionStatus{}
641-
}
642-
643-
desiredPorts := openStackCluster.Status.Bastion.ReferencedResources.Ports
644-
645577
// We can skip adoption if the ports are already in the status
646-
if len(desiredPorts) == len(openStackCluster.Status.Bastion.DependentResources.Ports) {
578+
if len(desiredPorts) == len(dependentResources.Ports) {
647579
return changed, nil
648580
}
649581

650-
scope.Logger().Info("Adopting bastion ports for OpenStackCluster", "name", openStackCluster.Name)
582+
scope.Logger().V(5).Info("Adopting ports")
651583

652584
// We create ports in order and adopt them in order in PortsStatus.
653585
// This means that if port N doesn't exist we know that ports >N don't exist.
654586
// We can therefore stop searching for ports once we find one that doesn't exist.
655587
for i, port := range desiredPorts {
656588
// check if the port is in status first and if it is, skip it
657-
if i < len(openStackCluster.Status.Bastion.DependentResources.Ports) {
589+
if i < len(dependentResources.Ports) {
658590
scope.Logger().V(5).Info("Port already in status, skipping it", "port index", i)
659591
continue
660592
}
661593

662594
portOpts := &desiredPorts[i]
663-
portName := GetPortName(bastionName, portOpts, i)
595+
portName := GetPortName(baseName, portOpts, i)
664596
ports, err := s.client.ListPort(ports.ListOpts{
665597
Name: portName,
666598
NetworkID: port.Network.ID,
667599
})
668600
if err != nil {
669-
return changed, fmt.Errorf("searching for existing port for bastion %s: %v", bastionName, err)
601+
return changed, fmt.Errorf("searching for existing port %s in network %s: %v", portName, port.Network.ID, err)
670602
}
671603
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
672604
// and will be created after the adoption
@@ -679,8 +611,9 @@ func (s *Service) AdoptBastionPorts(scope *scope.WithLogger, openStackCluster *i
679611
}
680612

681613
// The desired port was found, so we add it to the status
682-
scope.Logger().V(5).Info("Port found, adding it to the status", "port index", i)
683-
openStackCluster.Status.Bastion.DependentResources.Ports = append(openStackCluster.Status.Bastion.DependentResources.Ports, infrav1.PortStatus{ID: ports[0].ID})
614+
portID := ports[0].ID
615+
scope.Logger().Info("Adopted previously created port which was not in status", "port index", i, "portID", portID)
616+
dependentResources.Ports = append(dependentResources.Ports, infrav1.PortStatus{ID: portID})
684617
changed = true
685618
}
686619

0 commit comments

Comments
 (0)