Skip to content

Commit

Permalink
Always filter cluster subnets by cluster network ID
Browse files Browse the repository at this point in the history
Conflicts:
  controllers/openstackcluster_controller_test.go

  Unable to backport test as it depends on ScopeFactory.

(cherry picked from commit bbe2472)
  • Loading branch information
mdbooth committed Jun 1, 2023
1 parent 4cb7f12 commit 1f39166
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func reconcileNetworkComponents(scope *scope.Scope, cluster *clusterv1.Cluster,
openStackCluster.Status.Network.Name = networkList[0].Name
openStackCluster.Status.Network.Tags = networkList[0].Tags

subnet, err := networkingService.GetSubnetByFilter(&openStackCluster.Spec.Subnet)
subnet, err := networkingService.GetNetworkSubnetByFilter(openStackCluster.Status.Network.ID, &openStackCluster.Spec.Subnet)
if err != nil {
err = fmt.Errorf("failed to find subnet: %w", err)

Expand Down
21 changes: 18 additions & 3 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,31 @@ func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Su
// GetSubnetByFilter gets a single subnet specified by the given SubnetFilter.
// It returns an ErrFilterMatch if no or multiple subnets are found.
func (s *Service) GetSubnetByFilter(filter *infrav1.SubnetFilter) (*subnets.Subnet, error) {
return s.getSubnetByFilter(filter.ToListOpt())
}

// GetNetworkSubnetByFilter gets a single subnet of the given network, specified by the given SubnetFilter.
// It returns an ErrFilterMatch if no or multiple subnets are found.
func (s *Service) GetNetworkSubnetByFilter(networkID string, filter *infrav1.SubnetFilter) (*subnets.Subnet, error) {
listOpt := filter.ToListOpt()
listOpt.NetworkID = networkID

return s.getSubnetByFilter(listOpt)
}

// getSubnetByFilter gets a single subnet specified by the given gophercloud ListOpts.
// It returns an ErrFilterMatch if no or multiple subnets are found.
func (s *Service) getSubnetByFilter(listOpts subnets.ListOpts) (*subnets.Subnet, error) {
// If the ID is set, we can just get the subnet by ID.
if filter.ID != "" {
subnet, err := s.client.GetSubnet(filter.ID)
if listOpts.ID != "" {
subnet, err := s.client.GetSubnet(listOpts.ID)
if capoerrors.IsNotFound(err) {
return nil, ErrNoMatches
}
return subnet, err
}

subnets, err := s.GetSubnetsByFilter(filter.ToListOpt())
subnets, err := s.GetSubnetsByFilter(listOpts)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1f39166

Please sign in to comment.