Skip to content

Commit

Permalink
Add missing comments for GetNetworkParametersFromNAD
Browse files Browse the repository at this point in the history
  • Loading branch information
gthiemonge committed Jun 10, 2024
1 parent 01db902 commit d6ba3b2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/octavia/network_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ func getConfigFromNAD(
return nadConfig, nil
}

// getRangeFromCIDR - compute a IP address range from a CIDR
func getRangeFromCIDR(
cidr netip.Prefix,
) (start netip.Addr, end netip.Addr) {
// For IPv6, a /64 is expected, if the CIDR is aaaa:bbbb:cccc:dddd::/64,
// the range is aaaa:bbbb:cccc:dddd::5 - aaaa:bbbb:cccc:dddd:ffff:ffff:ffff:fffe
// For IPv4, a /16 is expected, if the CIDR is a.b.0.0/16
// the range is a.b.0.5 - a.b.255.254
// IPs from from 1 to 5 are reserved for later user
addr := cidr.Addr()
if addr.Is6() {
addrBytes := addr.As16()
Expand All @@ -78,6 +84,7 @@ func getRangeFromCIDR(
return
}

// GetNetworkParametersFromNAD - Extract network information from the Network Attachment Definition
func GetNetworkParametersFromNAD(
nad *networkv1.NetworkAttachmentDefinition,
) (*NetworkParameters, error) {
Expand All @@ -89,8 +96,14 @@ func GetNetworkParametersFromNAD(
}

// Provider subnet parameters
// These are the parameters for octavia-provider-net/subnet
networkParameters.ProviderCIDR = nadConfig.IPAM.CIDR

// OpenShift allocates IP addresses from IPAM.RangeStart to IPAM.RangeEnd
// for the pods.
// We're going to use a range of 25 IP addresses that are assigned to
// the Neutron allocation pool, the range starts right after OpenShift
// RangeEnd.
networkParameters.ProviderAllocationStart = nadConfig.IPAM.RangeEnd.Next()
end := networkParameters.ProviderAllocationStart
for i := 0; i < LbProvSubnetPoolSize; i++ {
Expand All @@ -100,14 +113,21 @@ func GetNetworkParametersFromNAD(
end = end.Next()
}
networkParameters.ProviderAllocationEnd = end

// The default gateway of the provider network is the gateway of our route
if len(nadConfig.IPAM.Routes) > 0 {
networkParameters.ProviderGateway = nadConfig.IPAM.Routes[0].Gateway
} else {
return nil, fmt.Errorf("cannot find gateway information in network attachment")
}

// Tenant subnet parameters
// Tenant subnet parameters - parameters for lb-mgmt-net/subnet
// The NAD must contain one route to the Octavia Tenant Management network,
// the gateway is an IP address of the provider network and the destination
// is the CIDR of the Tenant network.
networkParameters.TenantCIDR = nadConfig.IPAM.Routes[0].Destination

// For IPv4, we require a /16 subnet, for IPv6 a /64
var bitlen int
if networkParameters.TenantCIDR.Addr().Is6() {
bitlen = 64
Expand All @@ -119,6 +139,7 @@ func GetNetworkParametersFromNAD(
return nil, fmt.Errorf("the tenant CIDR is /%d, it should be /%d", networkParameters.TenantCIDR.Bits(), bitlen)
}

// Compute an allocation range based on the CIDR
start, end := getRangeFromCIDR(networkParameters.TenantCIDR)
networkParameters.TenantAllocationStart = start
networkParameters.TenantAllocationEnd = end
Expand Down

0 comments on commit d6ba3b2

Please sign in to comment.