From 176448ede568e5d4586cb963c710fee07bfcea9c Mon Sep 17 00:00:00 2001 From: Joseph Phillips Date: Fri, 2 Feb 2024 10:10:19 +0100 Subject: [PATCH] Modifies ProviderAddress.ToSpaceAddress to fall back to detecting the address space from a CIDR if one was supplied, and the space name is empty. This will all machine addresses to be decorated with spaces, facilitating use of configuration for juju-ha-space and juju-ctrl-space on the manual provider and others that support spaces in Juju, but not in the provider (MAAS). --- core/network/address.go | 13 ++++++++++++- core/network/address_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/core/network/address.go b/core/network/address.go index 84db2597191..638e71ac86d 100644 --- a/core/network/address.go +++ b/core/network/address.go @@ -503,7 +503,7 @@ func (pas ProviderAddresses) Values() []string { } // ToSpaceAddresses transforms the ProviderAddresses to SpaceAddresses by using -// the input lookup for conversion of space name to space ID. +// the input lookup to get a space ID from the name or the CIDR. func (pas ProviderAddresses) ToSpaceAddresses(lookup SpaceLookup) (SpaceAddresses, error) { if pas == nil { return nil, nil @@ -520,13 +520,24 @@ func (pas ProviderAddresses) ToSpaceAddresses(lookup SpaceLookup) (SpaceAddresse sas := make(SpaceAddresses, len(pas)) for i, pa := range pas { sas[i] = SpaceAddress{MachineAddress: pa.MachineAddress} + + // If the provider explicitly sets the space, i.e. MAAS, prefer the name. if pa.SpaceName != "" { info := spaceInfos.GetByName(string(pa.SpaceName)) if info == nil { return nil, errors.NotFoundf("space with name %q", pa.SpaceName) } sas[i].SpaceID = info.ID + continue + } + + // Otherwise attempt to look up the CIDR. + sInfo, err := spaceInfos.InferSpaceFromCIDRAndSubnetID(pa.CIDR, string(pa.ProviderSubnetID)) + if err != nil { + logger.Debugf("no matching subnet for CIDR %q and provider ID %q", pa.CIDR, pa.ProviderSubnetID) + continue } + sas[i].SpaceID = sInfo.ID } return sas, nil } diff --git a/core/network/address_test.go b/core/network/address_test.go index 7ab45b4e5a1..d5661489bcc 100644 --- a/core/network/address_test.go +++ b/core/network/address_test.go @@ -880,10 +880,21 @@ func (s stubLookup) AllSpaceInfos() (network.SpaceInfos, error) { return network.SpaceInfos{ {ID: "1", Name: "space-one", ProviderId: "p1"}, {ID: "2", Name: "space-two"}, + { + ID: "6", + Name: "space-six", + Subnets: network.SubnetInfos{ + { + ID: "666", + CIDR: "10.0.0.0/24", + ProviderId: "61", + }, + }, + }, }, nil } -func (s *AddressSuite) TestProviderAddressesToSpaceAddresses(c *gc.C) { +func (s *AddressSuite) TestProviderAddressesToSpaceAddressesByName(c *gc.C) { // Check success. addrs := network.ProviderAddresses{ network.NewMachineAddress("1.2.3.4").AsProviderAddress(network.WithSpaceName("space-one")), @@ -905,6 +916,21 @@ func (s *AddressSuite) TestProviderAddressesToSpaceAddresses(c *gc.C) { c.Assert(err, jc.Satisfies, errors.IsNotFound) } +func (s *AddressSuite) TestProviderAddressesToSpaceAddressesBySubnet(c *gc.C) { + // Check success. + addrs := network.ProviderAddresses{ + network.NewMachineAddress( + "10.0.0.6", + network.WithCIDR("10.0.0.0/24"), + ).AsProviderAddress(network.WithProviderSubnetID("61")), + } + + res, err := addrs.ToSpaceAddresses(stubLookup{}) + c.Assert(err, jc.ErrorIsNil) + c.Assert(res, gc.HasLen, 1) + c.Check(res[0].SpaceID, gc.Equals, "6") +} + func (s *AddressSuite) TestSpaceAddressesToProviderAddresses(c *gc.C) { // Check success. addrs := network.NewSpaceAddresses("1.2.3.4", "2.3.4.5", "3.4.5.6")