@@ -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