Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subnet allocator to mark assigned subnet dynamically #18999

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/network/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type OsdnMaster struct {
kClient kclientset.Interface
networkClient networkclient.Interface
networkInfo *common.NetworkInfo
subnetAllocatorList []*netutils.SubnetAllocator
subnetAllocatorList []*SubnetAllocator
vnids *masterVNIDMap

kubeInformers kinternalinformers.SharedInformerFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package netutils
package master

import (
"encoding/binary"
"fmt"
"net"
"sync"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var ErrSubnetAllocatorFull = fmt.Errorf("No subnets available.")
Expand All @@ -23,14 +26,14 @@ type SubnetAllocator struct {
func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*SubnetAllocator, error) {
_, netIP, err := net.ParseCIDR(network)
if err != nil {
return nil, fmt.Errorf("Failed to parse network address: %q", network)
return nil, fmt.Errorf("failed to parse network address: %q", network)
}

netMaskSize, _ := netIP.Mask.Size()
if hostBits == 0 {
return nil, fmt.Errorf("Host capacity cannot be zero.")
return nil, fmt.Errorf("host capacity cannot be zero.")
} else if hostBits > (32 - uint32(netMaskSize)) {
return nil, fmt.Errorf("Subnet capacity cannot be larger than number of networks available.")
return nil, fmt.Errorf("subnet capacity cannot be larger than number of networks available.")
}
subnetBits := 32 - uint32(netMaskSize) - hostBits

Expand Down Expand Up @@ -61,29 +64,41 @@ func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*Subne
rightMask = 0
}

amap := make(map[string]bool)
for _, netStr := range inUse {
_, nIp, err := net.ParseCIDR(netStr)
if err != nil {
fmt.Println("Failed to parse network address: ", netStr)
continue
}
if !netIP.Contains(nIp.IP) {
fmt.Println("Provided subnet doesn't belong to network: ", nIp)
continue
}
amap[nIp.String()] = true
}
return &SubnetAllocator{
sa := &SubnetAllocator{
network: netIP,
hostBits: hostBits,
leftShift: leftShift,
leftMask: leftMask,
rightShift: rightShift,
rightMask: rightMask,
next: 0,
allocMap: amap,
}, nil
allocMap: make(map[string]bool),
}
for _, netStr := range inUse {
_, ipNet, err := net.ParseCIDR(netStr)
if err != nil {
utilruntime.HandleError(fmt.Errorf("failed to parse network address: %s", netStr))
continue
}
if err = sa.AllocateNetwork(ipNet); err != nil {
utilruntime.HandleError(err)
continue
}
}
return sa, nil
}

func (sna *SubnetAllocator) AllocateNetwork(ipNet *net.IPNet) error {
sna.mutex.Lock()
defer sna.mutex.Unlock()

if !sna.network.Contains(ipNet.IP) {
return fmt.Errorf("provided subnet doesn't belong to network: %v", ipNet)
}
if !sna.allocMap[ipNet.String()] {
sna.allocMap[ipNet.String()] = true
}
return nil
}

func (sna *SubnetAllocator) GetNetwork() (*net.IPNet, error) {
Expand Down Expand Up @@ -120,16 +135,26 @@ func (sna *SubnetAllocator) GetNetwork() (*net.IPNet, error) {
func (sna *SubnetAllocator) ReleaseNetwork(ipnet *net.IPNet) error {
sna.mutex.Lock()
defer sna.mutex.Unlock()

if !sna.network.Contains(ipnet.IP) {
return fmt.Errorf("Provided subnet %v doesn't belong to the network %v.", ipnet, sna.network)
return fmt.Errorf("provided subnet %v doesn't belong to the network %v.", ipnet, sna.network)
}

ipnetStr := ipnet.String()
if !sna.allocMap[ipnetStr] {
return fmt.Errorf("Provided subnet %v is already available.", ipnet)
return fmt.Errorf("provided subnet %v is already available.", ipnet)
} else {
sna.allocMap[ipnetStr] = false
}
return nil
}

sna.allocMap[ipnetStr] = false
func IPToUint32(ip net.IP) uint32 {
return binary.BigEndian.Uint32(ip.To4())
}

return nil
func Uint32ToIP(u uint32) net.IP {
ip := make([]byte, 4)
binary.BigEndian.PutUint32(ip, u)
return net.IPv4(ip[0], ip[1], ip[2], ip[3])
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package netutils
package master

import (
"fmt"
Expand Down Expand Up @@ -205,6 +205,59 @@ func TestAllocateSubnetInUse(t *testing.T) {
}
}

func TestAllocateNetwork(t *testing.T) {
sna, err := NewSubnetAllocator("10.1.0.0/16", 14, nil)
if err != nil {
t.Fatal("Failed to initialize IP allocator: ", err)
}

allocSubnets := make([]*net.IPNet, 4)
for i := 0; i < 4; i++ {
if allocSubnets[i], err = sna.GetNetwork(); err != nil {
t.Fatal("Failed to get network: ", err)
}
}

if sn, err := sna.GetNetwork(); err == nil {
t.Fatalf("Unexpectedly succeeded in getting network (sn=%s)", sn.String())
}
if err := sna.ReleaseNetwork(allocSubnets[2]); err != nil {
t.Fatalf("Failed to release the subnet (allocSubnets[2]=%s): %v", allocSubnets[2].String(), err)
}
for i := 0; i < 2; i++ {
if err := sna.AllocateNetwork(allocSubnets[2]); err != nil {
t.Fatalf("Failed to allocate the subnet (allocSubnets[2]=%s): %v", allocSubnets[2].String(), err)
}
}
if sn, err := sna.GetNetwork(); err == nil {
t.Fatalf("Unexpectedly succeeded in getting network (sn=%s)", sn.String())
}

// Test subnet does not belong to network
var sn *net.IPNet
_, sn, err = net.ParseCIDR("10.2.3.4/24")
if err != nil {
t.Fatal("Failed to parse given network: ", err)
}
if err := sna.AllocateNetwork(sn); err == nil {
t.Fatalf("Unexpectedly succeeded in allocating subnet that doesn't belong to network (sn=%s)", sn.String())
}

// Test AllocateNetwork usage in NewSubnetAllocator
var subnetStrs []string
for _, sn := range allocSubnets {
subnetStrs = append(subnetStrs, sn.String())
}
var sa *SubnetAllocator
sa, err = NewSubnetAllocator("10.1.0.0/16", 14, subnetStrs)
if err != nil {
t.Fatal("Failed to initialize IP allocator: ", err)
}
if sn, err = sa.GetNetwork(); err == nil {
t.Fatalf("Unexpectedly succeeded in getting network (sn=%s)", sn.String())
}
}

func TestAllocateReleaseSubnet(t *testing.T) {
sna, err := NewSubnetAllocator("10.1.0.0/16", 14, nil)
if err != nil {
Expand Down Expand Up @@ -248,3 +301,19 @@ func TestAllocateReleaseSubnet(t *testing.T) {
t.Fatalf("Unexpectedly succeeded in getting network (sn=%s)", sn.String())
}
}

func TestConversion(t *testing.T) {
ip := net.ParseIP("10.1.2.3")
if ip == nil {
t.Fatal("Failed to parse IP")
}

u := IPToUint32(ip)
t.Log(u)
ip2 := Uint32ToIP(u)
t.Log(ip2)

if !ip2.Equal(ip) {
t.Fatal("Conversion back and forth failed")
}
}
7 changes: 3 additions & 4 deletions pkg/network/master/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/openshift/origin/pkg/network"
networkapi "github.com/openshift/origin/pkg/network/apis/network"
"github.com/openshift/origin/pkg/network/common"
"github.com/openshift/origin/pkg/util/netutils"
)

func (master *OsdnMaster) SubnetStartMaster(clusterNetworks []common.ClusterNetwork) error {
Expand Down Expand Up @@ -44,9 +43,9 @@ func (master *OsdnMaster) SubnetStartMaster(clusterNetworks []common.ClusterNetw
}
}
}
var subnetAllocatorList []*netutils.SubnetAllocator
var subnetAllocatorList []*SubnetAllocator
for _, cn := range clusterNetworks {
subnetAllocator, err := netutils.NewSubnetAllocator(cn.ClusterCIDR.String(), cn.HostSubnetLength, subrange[cn])
subnetAllocator, err := NewSubnetAllocator(cn.ClusterCIDR.String(), cn.HostSubnetLength, subrange[cn])
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +143,7 @@ func (master *OsdnMaster) addNode(nodeName string, nodeUID string, nodeIP string
}
for _, possibleSubnet := range master.subnetAllocatorList {
sn, err := possibleSubnet.GetNetwork()
if err == netutils.ErrSubnetAllocatorFull {
if err == ErrSubnetAllocatorFull {
// Current subnet exhausted, check the next one
continue
} else if err != nil {
Expand Down
11 changes: 0 additions & 11 deletions pkg/util/netutils/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package netutils

import (
"encoding/binary"
"fmt"
"net"

Expand All @@ -13,16 +12,6 @@ import (
var localHosts []string = []string{"127.0.0.1", "::1", "localhost"}
var localSubnets []string = []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7", "fe80::/10"}

func IPToUint32(ip net.IP) uint32 {
return binary.BigEndian.Uint32(ip.To4())
}

func Uint32ToIP(u uint32) net.IP {
ip := make([]byte, 4)
binary.BigEndian.PutUint32(ip, u)
return net.IPv4(ip[0], ip[1], ip[2], ip[3])
}

// Generate the default gateway IP Address for a subnet
func GenerateDefaultGateway(sna *net.IPNet) net.IP {
ip := sna.IP.To4()
Expand Down
31 changes: 3 additions & 28 deletions pkg/util/netutils/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,12 @@ import (
"testing"
)

func TestConversion(t *testing.T) {
ip := net.ParseIP("10.1.2.3")
if ip == nil {
t.Fatal("Failed to parse IP")
}

u := IPToUint32(ip)
t.Log(u)
ip2 := Uint32ToIP(u)
t.Log(ip2)

if !ip2.Equal(ip) {
t.Fatal("Conversion back and forth failed")
}
}

func TestGenerateGateway(t *testing.T) {
sna, err := NewSubnetAllocator("10.1.0.0/16", 8, nil)
_, ipNet, err := net.ParseCIDR("10.1.0.0/24")
if err != nil {
t.Fatal("Failed to initialize IP allocator: ", err)
t.Fatal(err)
}

sn, err := sna.GetNetwork()
if err != nil {
t.Fatal("Failed to get network: ", err)
}
if sn.String() != "10.1.0.0/24" {
t.Fatalf("Did not get expected subnet (sn=%s)", sn.String())
}

gatewayIP := GenerateDefaultGateway(sn)
gatewayIP := GenerateDefaultGateway(ipNet)
if gatewayIP.String() != "10.1.0.1" {
t.Fatalf("Did not get expected gateway IP Address (gatewayIP=%s)", gatewayIP.String())
}
Expand Down