@@ -518,104 +518,36 @@ func (s *Service) IsTrunkExtSupported() (trunknSupported bool, err error) {
518
518
return true , nil
519
519
}
520
520
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 ) {
526
524
changed = false
527
525
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
-
594
526
// 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 ) {
596
528
return changed , nil
597
529
}
598
530
599
- scope .Logger ().Info ("Adopting bastion ports for OpenStackCluster" , "name" , openStackCluster . Name )
531
+ scope .Logger ().V ( 5 ). Info ("Adopting ports" )
600
532
601
533
// We create ports in order and adopt them in order in PortsStatus.
602
534
// This means that if port N doesn't exist we know that ports >N don't exist.
603
535
// We can therefore stop searching for ports once we find one that doesn't exist.
604
536
for i , port := range desiredPorts {
605
537
// 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 ) {
607
539
scope .Logger ().V (5 ).Info ("Port already in status, skipping it" , "port index" , i )
608
540
continue
609
541
}
610
542
611
543
portOpts := & desiredPorts [i ]
612
- portName := GetPortName (bastionName , portOpts , i )
544
+ portName := GetPortName (baseName , portOpts , i )
613
545
ports , err := s .client .ListPort (ports.ListOpts {
614
546
Name : portName ,
615
547
NetworkID : port .Network .ID ,
616
548
})
617
549
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 )
619
551
}
620
552
// if the port is not found, we stop the adoption of ports since the rest of the ports will not be found either
621
553
// and will be created after the adoption
@@ -628,8 +560,9 @@ func (s *Service) AdoptBastionPorts(scope *scope.WithLogger, openStackCluster *i
628
560
}
629
561
630
562
// 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 })
633
566
changed = true
634
567
}
635
568
0 commit comments