Skip to content

Commit 47179b6

Browse files
committed
Set host_routes after plugging the subnet into the router
the previous workflow was - create a subnet with a host route via a fixed ip - create a port with the fixed ip - plug it into the router but in IPv6, fixed ip are denied when automatic addresses are enabled now the code does: - create a subnet - create a port - plug the port into the router - set the ip of the port as a gateway of the subnet
1 parent ec3be4d commit 47179b6

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

pkg/octavia/lb_mgmt_network.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
type NetworkProvisioningSummary struct {
3636
TenantNetworkID string
3737
TenantSubnetID string
38-
TenantRouterPortID string
3938
ProviderNetworkID string
4039
RouterID string
4140
SecurityGroupID string
@@ -48,7 +47,7 @@ type NetworkProvisioningSummary struct {
4847
// status.
4948
//
5049

51-
func findPort(client *gophercloud.ServiceClient, networkID string, ipAddress string, log *logr.Logger) (*ports.Port, error) {
50+
func findPort(client *gophercloud.ServiceClient, networkID string, name string, log *logr.Logger) (*ports.Port, error) {
5251
listOpts := ports.ListOpts{
5352
NetworkID: networkID,
5453
}
@@ -65,18 +64,16 @@ func findPort(client *gophercloud.ServiceClient, networkID string, ipAddress str
6564
}
6665
if len(allPorts) > 0 {
6766
for _, port := range allPorts {
68-
if len(port.FixedIPs) > 0 && port.FixedIPs[0].IPAddress == ipAddress {
67+
if port.Name == name {
6968
return &port, nil
7069
}
7170
}
7271
}
7372
return nil, nil
7473
}
7574

76-
func ensurePort(client *gophercloud.ServiceClient, tenantNetwork *networks.Network, tenantSubnet *subnets.Subnet,
77-
securityGroups *[]string, networkParameters *NetworkParameters, log *logr.Logger) (*ports.Port, error) {
78-
ipAddress := networkParameters.TenantGateway.String()
79-
p, err := findPort(client, tenantNetwork.ID, ipAddress, log)
75+
func ensurePort(client *gophercloud.ServiceClient, tenantNetwork *networks.Network, securityGroups *[]string, log *logr.Logger) (*ports.Port, error) {
76+
p, err := findPort(client, tenantNetwork.ID, LbMgmtRouterPortName, log)
8077
if err != nil {
8178
return nil, err
8279
}
@@ -89,15 +86,9 @@ func ensurePort(client *gophercloud.ServiceClient, tenantNetwork *networks.Netwo
8986
log.Info("Unable to locate port, creating new one")
9087
asu := true
9188
createOpts := ports.CreateOpts{
92-
Name: LbMgmtRouterPortName,
93-
AdminStateUp: &asu,
94-
NetworkID: tenantNetwork.ID,
95-
FixedIPs: []ports.IP{
96-
{
97-
SubnetID: tenantSubnet.ID,
98-
IPAddress: ipAddress,
99-
},
100-
},
89+
Name: LbMgmtRouterPortName,
90+
AdminStateUp: &asu,
91+
NetworkID: tenantNetwork.ID,
10192
SecurityGroups: securityGroups,
10293
}
10394
p, err = ports.Create(client, createOpts).Extract()
@@ -296,13 +287,19 @@ func ensureProvSubnet(
296287
log *logr.Logger,
297288
) (*subnets.Subnet, error) {
298289
gatewayIP := ""
290+
var ipVersion int
291+
if networkParameters.ProviderCIDR.Addr().Is6() {
292+
ipVersion = 6
293+
} else {
294+
ipVersion = 4
295+
}
299296
createOpts := subnets.CreateOpts{
300297
Name: LbProvSubnetName,
301298
Description: LbProvSubnetDescription,
302299
NetworkID: providerNetwork.ID,
303300
TenantID: providerNetwork.TenantID,
304301
CIDR: networkParameters.ProviderCIDR.String(),
305-
IPVersion: gophercloud.IPVersion(4),
302+
IPVersion: gophercloud.IPVersion(ipVersion),
306303
AllocationPools: []subnets.AllocationPool{
307304
{
308305
Start: networkParameters.ProviderAllocationStart.String(),
@@ -311,7 +308,7 @@ func ensureProvSubnet(
311308
},
312309
GatewayIP: &gatewayIP,
313310
}
314-
return ensureSubnet(client, 4, createOpts, log)
311+
return ensureSubnet(client, ipVersion, createOpts, log)
315312
}
316313

317314
func ensureProvNetwork(client *gophercloud.ServiceClient, netDetails *octaviav1.OctaviaLbMgmtNetworks, serviceTenantID string, log *logr.Logger) (
@@ -337,6 +334,31 @@ func ensureProvNetwork(client *gophercloud.ServiceClient, netDetails *octaviav1.
337334
return provNet, nil
338335
}
339336

337+
func ensureLbMgmtSubnetRoutes(
338+
client *gophercloud.ServiceClient,
339+
tenantSubnet *subnets.Subnet,
340+
networkParameters *NetworkParameters,
341+
tenantRouterPort *ports.Port,
342+
) error {
343+
if len(tenantSubnet.HostRoutes) == 0 {
344+
hostRoutes := []subnets.HostRoute{
345+
{
346+
DestinationCIDR: networkParameters.ProviderCIDR.String(),
347+
NextHop: tenantRouterPort.FixedIPs[0].IPAddress,
348+
},
349+
}
350+
updateOpts := subnets.UpdateOpts{
351+
HostRoutes: &hostRoutes,
352+
}
353+
_, err := subnets.Update(client, tenantSubnet.ID, updateOpts).Extract()
354+
if err != nil {
355+
return err
356+
}
357+
}
358+
359+
return nil
360+
}
361+
340362
func ensureLbMgmtSubnet(
341363
client *gophercloud.ServiceClient,
342364
tenantNetwork *networks.Network,
@@ -369,7 +391,6 @@ func ensureLbMgmtSubnet(
369391
},
370392
},
371393
GatewayIP: &gatewayIP,
372-
// TODO(beagles): ipv6 host routes
373394
}
374395
} else {
375396
gatewayIP := LbMgmtSubnetGatewayIP
@@ -386,12 +407,6 @@ func ensureLbMgmtSubnet(
386407
End: networkParameters.TenantAllocationEnd.String(),
387408
},
388409
},
389-
HostRoutes: []subnets.HostRoute{
390-
{
391-
DestinationCIDR: networkParameters.ProviderCIDR.String(),
392-
NextHop: networkParameters.TenantGateway.String(),
393-
},
394-
},
395410
GatewayIP: &gatewayIP,
396411
}
397412
}
@@ -797,7 +812,7 @@ func EnsureAmphoraManagementNetwork(
797812

798813
securityGroups := []string{lbMgmtSecurityGroupID, lbHealthSecurityGroupID}
799814

800-
tenantRouterPort, err := ensurePort(client, tenantNetwork, tenantSubnet, &securityGroups, networkParameters, log)
815+
tenantRouterPort, err := ensurePort(client, tenantNetwork, &securityGroups, log)
801816
if err != nil {
802817
return NetworkProvisioningSummary{}, err
803818
}
@@ -856,25 +871,25 @@ func EnsureAmphoraManagementNetwork(
856871
log.Error(err, "Unable to create router object")
857872
return NetworkProvisioningSummary{}, err
858873
}
859-
}
860-
if tenantRouterPort.DeviceID == "" {
874+
861875
interfaceOpts := routers.AddInterfaceOpts{
862876
PortID: tenantRouterPort.ID,
863877
}
864878
_, err := routers.AddInterface(client, router.ID, interfaceOpts).Extract()
865879
if err != nil {
866880
log.Error(err, fmt.Sprintf("Unable to add interface port %s to router %s", tenantRouterPort.ID, router.ID))
867881
}
868-
} else if tenantRouterPort.DeviceID != router.ID {
869-
return NetworkProvisioningSummary{},
870-
fmt.Errorf("Port %s has unexpected device ID %s and cannot be added to router %s", tenantRouterPort.ID,
871-
tenantRouterPort.DeviceID, router.ID)
882+
}
883+
// Set route on subnet
884+
885+
err = ensureLbMgmtSubnetRoutes(client, tenantSubnet, networkParameters, tenantRouterPort)
886+
if err != nil {
887+
log.Error(err, fmt.Sprintf("Unable to set host routes on subnet %s", tenantSubnet.ID))
872888
}
873889

874890
return NetworkProvisioningSummary{
875891
TenantNetworkID: tenantNetwork.ID,
876892
TenantSubnetID: tenantSubnet.ID,
877-
TenantRouterPortID: tenantRouterPort.ID,
878893
ProviderNetworkID: providerNetwork.ID,
879894
RouterID: router.ID,
880895
SecurityGroupID: lbMgmtSecurityGroupID,

pkg/octavia/network_parameters.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type NetworkParameters struct {
1717
TenantCIDR netip.Prefix
1818
TenantAllocationStart netip.Addr
1919
TenantAllocationEnd netip.Addr
20-
TenantGateway netip.Addr
2120
}
2221

2322
// NADConfig - IPAM parameters of the NAD
@@ -51,17 +50,15 @@ func getConfigFromNAD(
5150
return nadConfig, nil
5251
}
5352

54-
func getRangeAndGatewayFromCIDR(
53+
func getRangeFromCIDR(
5554
cidr netip.Prefix,
56-
) (start netip.Addr, end netip.Addr, gateway netip.Addr) {
55+
) (start netip.Addr, end netip.Addr) {
5756
addr := cidr.Addr()
5857
if addr.Is6() {
5958
addrBytes := addr.As16()
6059
for i := 8; i < 15; i++ {
6160
addrBytes[i] = 0
6261
}
63-
addrBytes[15] = 3
64-
gateway = netip.AddrFrom16(addrBytes)
6562
addrBytes[15] = 5
6663
start = netip.AddrFrom16(addrBytes)
6764
for i := 8; i < 15; i++ {
@@ -72,8 +69,6 @@ func getRangeAndGatewayFromCIDR(
7269
} else {
7370
addrBytes := addr.As4()
7471
addrBytes[2] = 0
75-
addrBytes[3] = 3
76-
gateway = netip.AddrFrom4(addrBytes)
7772
addrBytes[3] = 5
7873
start = netip.AddrFrom4(addrBytes)
7974
addrBytes[2] = 0xff
@@ -124,10 +119,9 @@ func GetNetworkParametersFromNAD(
124119
return nil, fmt.Errorf("the tenant CIDR is /%d, it should be /%d", networkParameters.TenantCIDR.Bits(), bitlen)
125120
}
126121

127-
start, end, gateway := getRangeAndGatewayFromCIDR(networkParameters.TenantCIDR)
122+
start, end := getRangeFromCIDR(networkParameters.TenantCIDR)
128123
networkParameters.TenantAllocationStart = start
129124
networkParameters.TenantAllocationEnd = end
130-
networkParameters.TenantGateway = gateway
131125

132126
return networkParameters, err
133127
}

0 commit comments

Comments
 (0)