Skip to content

Commit

Permalink
Modify OpenStackCluster.Spec.Network API
Browse files Browse the repository at this point in the history
For the BYO scenario, when the OpenStackCluster.Spec.Network
is not specified the query to OpenStack will return all the
Networks available. To avoid that, if any Subnets were specified
under OpenStackCluster.Spec.Subnets this can used to identify
which Network to use.
  • Loading branch information
MaysaMacedo committed Jan 19, 2024
1 parent 05571b9 commit b347263
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
44 changes: 27 additions & 17 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,14 @@ func reconcileNetworkComponents(scope scope.Scope, cluster *clusterv1.Cluster, o
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to find any network"))
return fmt.Errorf("failed to find any network")
}
if len(networkList) > 1 {
handleUpdateOSCError(openStackCluster, fmt.Errorf("found multiple networks (result: %v)", networkList))
return fmt.Errorf("found multiple networks (result: %v)", networkList)
}
if openStackCluster.Status.Network == nil {
openStackCluster.Status.Network = &infrav1.NetworkStatusWithSubnets{}
}
openStackCluster.Status.Network.ID = networkList[0].ID
openStackCluster.Status.Network.Name = networkList[0].Name
openStackCluster.Status.Network.Tags = networkList[0].Tags
if len(networkList) == 1 {
networkingService.ConvertOpenStackNetworkToCAPONetwork(openStackCluster, networkList[0])
}

subnets, err := filterSubnets(networkingService, openStackCluster)
subnets, networkID, err := filterSubnetsAndNetwork(networkingService, openStackCluster)
if err != nil {
return err
}
Expand All @@ -526,6 +522,15 @@ func reconcileNetworkComponents(scope scope.Scope, cluster *clusterv1.Cluster, o
return err
}
openStackCluster.Status.Network.Subnets = subnets

if len(networkList) > 1 {
netOpts.ID = networkID
network, err := networkingService.GetNetworksByFilter(netOpts)
if err != nil {
return err
}
networkingService.ConvertOpenStackNetworkToCAPONetwork(openStackCluster, network[0])
}
} else {
err := networkingService.ReconcileNetwork(openStackCluster, clusterName)
if err != nil {
Expand Down Expand Up @@ -673,15 +678,19 @@ func handleUpdateOSCError(openstackCluster *infrav1.OpenStackCluster, message er
openstackCluster.Status.FailureMessage = pointer.String(message.Error())
}

// filterSubnets retrieves the subnets based on the Subnet filters specified on OpenstackCluster.
func filterSubnets(networkingService *networking.Service, openStackCluster *infrav1.OpenStackCluster) ([]infrav1.Subnet, error) {
// filterSubnetsAndNetwork retrieves the subnets based on the Subnet filters specified on OpenstackCluster.
func filterSubnetsAndNetwork(networkingService *networking.Service, openStackCluster *infrav1.OpenStackCluster) ([]infrav1.Subnet, string, error) {
var subnets []infrav1.Subnet
openStackClusterSubnets := openStackCluster.Spec.Subnets
if openStackCluster.Status.Network == nil {
return nil, nil
networkID := ""
if openStackCluster.Status.Network != nil {
networkID = openStackCluster.Status.Network.ID
}
networkID := openStackCluster.Status.Network.ID

if len(openStackClusterSubnets) == 0 {
if networkID == "" {
return nil, networkID, nil
}
empty := &infrav1.SubnetFilter{}
listOpt := empty.ToListOpt()
listOpt.NetworkID = networkID
Expand All @@ -691,10 +700,10 @@ func filterSubnets(networkingService *networking.Service, openStackCluster *infr
if errors.Is(err, networking.ErrFilterMatch) {
handleUpdateOSCError(openStackCluster, err)
}
return nil, err
return nil, networkID, err
}
if len(filteredSubnets) > 2 {
return nil, fmt.Errorf("more than two subnets found in the Network. Specify the subnets in the OpenStackCluster.Spec instead")
return nil, networkID, fmt.Errorf("more than two subnets found in the Network. Specify the subnets in the OpenStackCluster.Spec instead")
}
for subnet := range filteredSubnets {
subnets = networkingService.ConvertOpenStackSubnetToCAPOSubnet(subnets, &filteredSubnets[subnet])
Expand All @@ -707,10 +716,11 @@ func filterSubnets(networkingService *networking.Service, openStackCluster *infr
if errors.Is(err, networking.ErrFilterMatch) {
handleUpdateOSCError(openStackCluster, err)
}
return nil, err
return nil, networkID, err
}
subnets = networkingService.ConvertOpenStackSubnetToCAPOSubnet(subnets, filteredSubnet)
networkID = filteredSubnet.NetworkID
}
}
return subnets, nil
return subnets, networkID, nil
}
8 changes: 8 additions & 0 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,11 @@ func (s *Service) ConvertOpenStackSubnetToCAPOSubnet(subnets []infrav1.Subnet, f
})
return subnets
}

// ConvertOpenStackNetworkToCAPONetwork converts an OpenStack network to a capo network.
// It returns the converted subnet.
func (s *Service) ConvertOpenStackNetworkToCAPONetwork(openStackCluster *infrav1.OpenStackCluster, network networks.Network) {
openStackCluster.Status.Network.ID = network.ID
openStackCluster.Status.Network.Name = network.Name
openStackCluster.Status.Network.Tags = network.Tags
}

0 comments on commit b347263

Please sign in to comment.