Skip to content

Commit 73386ba

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 0752244 commit 73386ba

File tree

5 files changed

+17
-316
lines changed

5 files changed

+17
-316
lines changed

controllers/openstackcluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
137137
return reconcile.Result{}, nil
138138
}
139139

140-
changed, err = compute.ResolveDependentBastionResources(scope, openStackCluster, bastionName(cluster.Name))
140+
changed, err = compute.AdoptDependentMachineResources(scope, bastionName(cluster.Name), &openStackCluster.Status.Bastion.ReferencedResources, &openStackCluster.Status.Bastion.DependentResources)
141141
if err != nil {
142142
return reconcile.Result{}, err
143143
}

controllers/openstackmachine_controller.go

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

161-
// Resolve and store dependent resources
162-
changed, err = compute.ResolveDependentMachineResources(scope, openStackMachine)
161+
// Adopt any existing dependent resources
162+
changed, err = compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
163163
if err != nil {
164164
return reconcile.Result{}, err
165165
}

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
@@ -518,104 +518,36 @@ func (s *Service) IsTrunkExtSupported() (trunknSupported bool, err error) {
518518
return true, nil
519519
}
520520

521-
// AdoptMachinePorts checks if the ports are in ready condition. If not, it'll try to adopt them
522-
// by checking if they exist and if they do, it'll add them to the OpenStackMachine status.
523-
// A port is searched by name and network ID and has to be unique.
524-
// If the port is not found, it'll be ignored because it'll be created after the adoption.
525-
func (s *Service) AdoptMachinePorts(scope *scope.WithLogger, openStackMachine *infrav1.OpenStackMachine, desiredPorts []infrav1.PortOpts) (changed bool, err error) {
521+
// AdoptPorts looks for ports in desiredPorts which were previously created, and adds them to dependentResources.Ports.
522+
// A port matches if it has the same name and network ID as the desired port.
523+
func (s *Service) AdoptPorts(scope *scope.WithLogger, baseName string, desiredPorts []infrav1.PortOpts, dependentResources *infrav1.DependentMachineResources) (changed bool, err error) {
526524
changed = false
527525

528-
// We can skip adoption if the instance is ready because OpenStackMachine is immutable once ready
529-
// or if the ports are already in the status
530-
if openStackMachine.Status.Ready && len(openStackMachine.Status.DependentResources.Ports) == len(desiredPorts) {
531-
scope.Logger().V(5).Info("OpenStackMachine is ready, skipping the adoption of ports")
532-
return changed, nil
533-
}
534-
535-
scope.Logger().Info("Adopting ports for OpenStackMachine", "name", openStackMachine.Name)
536-
537-
// We create ports in order and adopt them in order in PortsStatus.
538-
// This means that if port N doesn't exist we know that ports >N don't exist.
539-
// We can therefore stop searching for ports once we find one that doesn't exist.
540-
for i, port := range desiredPorts {
541-
// check if the port is in status first and if it is, skip it
542-
if i < len(openStackMachine.Status.DependentResources.Ports) {
543-
scope.Logger().V(5).Info("Port already in status, skipping it", "port index", i)
544-
continue
545-
}
546-
547-
portOpts := &desiredPorts[i]
548-
portName := GetPortName(openStackMachine.Name, portOpts, i)
549-
ports, err := s.client.ListPort(ports.ListOpts{
550-
Name: portName,
551-
NetworkID: port.Network.ID,
552-
})
553-
if err != nil {
554-
return changed, fmt.Errorf("searching for existing port for machine %s: %v", openStackMachine.Name, err)
555-
}
556-
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
557-
// and will be created after the adoption
558-
if len(ports) == 0 {
559-
scope.Logger().V(5).Info("Port not found, stopping the adoption of ports", "port index", i)
560-
return changed, nil
561-
}
562-
if len(ports) > 1 {
563-
return changed, fmt.Errorf("found multiple ports with name %s", portName)
564-
}
565-
566-
// The desired port was found, so we add it to the status
567-
scope.Logger().V(5).Info("Port found, adding it to the status", "port index", i)
568-
openStackMachine.Status.DependentResources.Ports = append(openStackMachine.Status.DependentResources.Ports, infrav1.PortStatus{ID: ports[0].ID})
569-
changed = true
570-
}
571-
572-
return changed, nil
573-
}
574-
575-
// AdopteBastionPorts tries to adopt the ports for the bastion instance by checking if they exist and if they do,
576-
// it'll add them to the OpenStackCluster status.
577-
// A port is searched by name and network ID and has to be unique.
578-
// If the port is not found, it'll be ignored because it'll be created after the adoption.
579-
func (s *Service) AdoptBastionPorts(scope *scope.WithLogger, openStackCluster *infrav1.OpenStackCluster, bastionName string) (changed bool, err error) {
580-
changed = false
581-
582-
if openStackCluster.Status.Network == nil {
583-
scope.Logger().V(5).Info("Network status is nil, skipping the adoption of ports")
584-
return changed, nil
585-
}
586-
587-
if openStackCluster.Status.Bastion == nil {
588-
scope.Logger().V(5).Info("Bastion status is nil, initializing it")
589-
openStackCluster.Status.Bastion = &infrav1.BastionStatus{}
590-
}
591-
592-
desiredPorts := openStackCluster.Status.Bastion.ReferencedResources.Ports
593-
594526
// We can skip adoption if the ports are already in the status
595-
if len(desiredPorts) == len(openStackCluster.Status.Bastion.DependentResources.Ports) {
527+
if len(desiredPorts) == len(dependentResources.Ports) {
596528
return changed, nil
597529
}
598530

599-
scope.Logger().Info("Adopting bastion ports for OpenStackCluster", "name", openStackCluster.Name)
531+
scope.Logger().V(5).Info("Adopting ports")
600532

601533
// We create ports in order and adopt them in order in PortsStatus.
602534
// This means that if port N doesn't exist we know that ports >N don't exist.
603535
// We can therefore stop searching for ports once we find one that doesn't exist.
604536
for i, port := range desiredPorts {
605537
// check if the port is in status first and if it is, skip it
606-
if i < len(openStackCluster.Status.Bastion.DependentResources.Ports) {
538+
if i < len(dependentResources.Ports) {
607539
scope.Logger().V(5).Info("Port already in status, skipping it", "port index", i)
608540
continue
609541
}
610542

611543
portOpts := &desiredPorts[i]
612-
portName := GetPortName(bastionName, portOpts, i)
544+
portName := GetPortName(baseName, portOpts, i)
613545
ports, err := s.client.ListPort(ports.ListOpts{
614546
Name: portName,
615547
NetworkID: port.Network.ID,
616548
})
617549
if err != nil {
618-
return changed, fmt.Errorf("searching for existing port for bastion %s: %v", bastionName, err)
550+
return changed, fmt.Errorf("searching for existing port %s in network %s: %v", portName, port.Network.ID, err)
619551
}
620552
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
621553
// and will be created after the adoption
@@ -628,8 +560,9 @@ func (s *Service) AdoptBastionPorts(scope *scope.WithLogger, openStackCluster *i
628560
}
629561

630562
// The desired port was found, so we add it to the status
631-
scope.Logger().V(5).Info("Port found, adding it to the status", "port index", i)
632-
openStackCluster.Status.Bastion.DependentResources.Ports = append(openStackCluster.Status.Bastion.DependentResources.Ports, infrav1.PortStatus{ID: ports[0].ID})
563+
portID := ports[0].ID
564+
scope.Logger().Info("Adopted previously created port which was not in status", "port index", i, "portID", portID)
565+
dependentResources.Ports = append(dependentResources.Ports, infrav1.PortStatus{ID: portID})
633566
changed = true
634567
}
635568

0 commit comments

Comments
 (0)