Skip to content

Commit

Permalink
Merge pull request flannel-io#1582 from rbrtbnfgl/etcd-subnetv6
Browse files Browse the repository at this point in the history
Fixed subnet allocation in case of etcd manager with IPv6
  • Loading branch information
rbrtbnfgl authored and manuelbuil committed Jun 22, 2022
2 parents b31565b + 2654f9d commit ca53979
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
14 changes: 11 additions & 3 deletions dist/functional-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ write_config_etcd() {
flannel_conf="{ \"Network\": \"$FLANNEL_NET\", \"Backend\": { \"Type\": \"${backend}\" } }"
fi

while ! docker run --rm -e ETCDCTL_API=3 $ETCDCTL_IMG etcdctl --endpoints=$etcd_endpt put /coreos.com/network/config "$flannel_conf" >/dev/null
while ! docker run --rm -e ETCDCTL_API=3 $ETCDCTL_IMG etcdctl --endpoints=$etcd_endpt put /coreos.com/network/config "$flannel_conf"
do
sleep 0.1
echo "Retrying write_config_etcd with backend $1"
done
}

Expand All @@ -70,6 +71,7 @@ create_ping_dest() {
for host_num in 1 2; do
while ! docker exec flannel-e2e-test-flannel$host_num ls /run/flannel/subnet.env >/dev/null 2>&1; do
sleep 0.1
echo "Retrying create_ping_dest() with host_num=$host_num"
done

# Use declare to allow the host_num variable to be part of the ping_dest variable name. -g is needed to make it global
Expand Down Expand Up @@ -133,8 +135,11 @@ pings() {

# These perf tests don't actually assert on anything
test_hostgw_perf() {
echo "Inside test_hostgw_perf()"
write_config_etcd host-gw
echo "In test_hostgw_perf(). write_config_etcd done!"
create_ping_dest
echo "In test_hostgw_perf(). Create_ping_dest done!"
perf
}

Expand Down Expand Up @@ -172,9 +177,12 @@ test_wireguard_perf() {

perf() {
# Perf test - run iperf server on flannel1 and client on flannel2
docker rm -f flannel-e2e-test-flannel1-iperf 2>/dev/null
docker run -d --name flannel-e2e-test-flannel1-iperf --net=container:flannel-e2e-test-flannel1 iperf3:latest >/dev/null
docker rm -f flannel-e2e-test-flannel1-iperf
echo "Inside perf(). docker rm done"
docker run -d --name flannel-e2e-test-flannel1-iperf --net=container:flannel-e2e-test-flannel1 iperf3:latest
echo "Inside perf(). docker run -d done"
docker run --rm --net=container:flannel-e2e-test-flannel2 iperf3:latest -c $ping_dest1 -B $ping_dest2
echo "Inside perf(). docker run -rm done"
}

test_multi() {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,4 @@ func ReadIP6CIDRFromSubnetFile(path string, CIDRKey string) ip.IP6Net {
}
return prevCIDR
}

40 changes: 18 additions & 22 deletions subnet/etcd/local_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,8 @@ func (m *LocalManager) allocateSubnet(config *Config, leases []Lease) (ip.IP4Net
log.Infof("Picking ipv6 subnet in range %s ... %s", config.IPv6SubnetMin, config.IPv6SubnetMax)
}

type IPs struct {
ip ip.IP4
ip6 *ip.IP6
}
var availableIPs []IPs
var availableIPs []ip.IP4
var availableIPv6s []*ip.IP6

sn := ip.IP4Net{IP: config.SubnetMin, PrefixLen: config.SubnetLen}
var sn6 ip.IP6Net
Expand All @@ -216,38 +213,37 @@ func (m *LocalManager) allocateSubnet(config *Config, leases []Lease) (ip.IP4Net

OuterLoop:
for ; sn.IP <= config.SubnetMax && len(availableIPs) < 100; sn = sn.Next() {
if !sn6.Empty() {
if sn6.IP.Cmp(config.IPv6SubnetMax) >= 0 {
break OuterLoop
}
}
for _, l := range leases {
if sn.Overlaps(l.Subnet) {
continue OuterLoop
}
if !sn6.Empty() && sn6.Overlaps(l.IPv6Subnet) {
continue OuterLoop
}
}
availableIPs = append(availableIPs, sn.IP)
}

if !sn6.Empty() {
availableIPs = append(availableIPs, IPs{ip: sn.IP, ip6: sn6.IP})
sn6 = sn6.Next()
} else {
availableIPs = append(availableIPs, IPs{ip: sn.IP, ip6: nil})
if !sn6.Empty() {
OuterLoopv6:
for ; sn6.IP.Cmp(config.IPv6SubnetMax) <= 0 && len(availableIPv6s) < 100; sn6 = sn6.Next() {
for _, l := range leases {
if sn6.Overlaps(l.IPv6Subnet) {
continue OuterLoopv6
}
}
availableIPv6s = append(availableIPv6s, sn6.IP)
}
}

if len(availableIPs) == 0 {
if len(availableIPs) == 0 || (!sn6.Empty() && len(availableIPv6s) == 0) {
return ip.IP4Net{}, ip.IP6Net{}, errors.New("out of subnets")
} else {
i := randInt(0, len(availableIPs))
ipnet := ip.IP4Net{IP: availableIPs[i].ip, PrefixLen: config.SubnetLen}
ipnet := ip.IP4Net{IP: availableIPs[i], PrefixLen: config.SubnetLen}

if availableIPs[i].ip6 == nil {
if sn6.Empty() {
return ipnet, ip.IP6Net{}, nil
}
return ipnet, ip.IP6Net{IP: availableIPs[i].ip6, PrefixLen: config.IPv6SubnetLen}, nil
i = randInt(0, len(availableIPv6s))
return ipnet, ip.IP6Net{IP: availableIPv6s[i], PrefixLen: config.IPv6SubnetLen}, nil
}
}

Expand Down

0 comments on commit ca53979

Please sign in to comment.