Browse Source

Merge pull request #1472 from manuelbuil/master

Add incrementIP to the ip package
Manuel Buil 3 years ago
parent
commit
fca1560c91
5 changed files with 24 additions and 5 deletions
  1. 4 5
      main.go
  2. 5 0
      pkg/ip/ip6net.go
  3. 5 0
      pkg/ip/ip6net_test.go
  4. 5 0
      pkg/ip/ipnet.go
  5. 5 0
      pkg/ip/ipnet_test.go

+ 4 - 5
main.go

@@ -18,7 +18,6 @@ import (
 	"errors"
 	"flag"
 	"fmt"
-	"math/big"
 	"net"
 	"net/http"
 	"os"
@@ -754,17 +753,17 @@ func WriteSubnetFile(path string, config *subnet.Config, ipMasq bool, bn backend
 	}
 	if config.EnableIPv4 {
 		nw := config.Network
-		// Write out the first usable IP by incrementing
-		// sn.IP by one
 		sn := bn.Lease().Subnet
-		sn.IP += 1
+		// Write out the first usable IP by incrementing sn.IP by one
+		sn.IncrementIP()
 		fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
 		fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
 	}
 	if config.EnableIPv6 {
 		ip6Nw := config.IPv6Network
 		ip6Sn := bn.Lease().IPv6Subnet
-		ip6Sn.IP = (*ip.IP6)(big.NewInt(0).Add((*big.Int)(ip6Sn.IP), big.NewInt(1)))
+		// Write out the first usable IP by incrementing ip6Sn.IP by one
+		ip6Sn.IncrementIP()
 		fmt.Fprintf(f, "FLANNEL_IPV6_NETWORK=%s\n", ip6Nw)
 		fmt.Fprintf(f, "FLANNEL_IPV6_SUBNET=%s\n", ip6Sn)
 	}

+ 5 - 0
pkg/ip/ip6net.go

@@ -142,6 +142,11 @@ func (n IP6Net) Next() IP6Net {
 	}
 }
 
+// IncrementIP() increments the IP of IP6Net CIDR by 1
+func (n *IP6Net) IncrementIP() {
+	n.IP = (*IP6)(big.NewInt(0).Add((*big.Int)(n.IP), big.NewInt(1)))
+}
+
 func FromIP6Net(n *net.IPNet) IP6Net {
 	prefixLen, _ := n.Mask.Size()
 	return IP6Net{

+ 5 - 0
pkg/ip/ip6net_test.go

@@ -110,4 +110,9 @@ func TestIP6Net(t *testing.T) {
 	} else if string(j) != `"fc00:1::/64"` {
 		t.Error("Marshal of IP6Net failed with unexpected value: ", j)
 	}
+
+	n1.IncrementIP()
+	if n1.String() != "fc00:1::1/64" {
+		t.Error("IncrementIP() failed")
+	}
 }

+ 5 - 0
pkg/ip/ipnet.go

@@ -127,6 +127,11 @@ func (n IP4Net) Next() IP4Net {
 	}
 }
 
+// IncrementIP() increments the IP of IP4Net CIDR by 1
+func (n *IP4Net) IncrementIP() {
+	n.IP++
+}
+
 func FromIPNet(n *net.IPNet) IP4Net {
 	prefixLen, _ := n.Mask.Size()
 	return IP4Net{

+ 5 - 0
pkg/ip/ipnet_test.go

@@ -118,4 +118,9 @@ func TestIP4Net(t *testing.T) {
 	} else if string(j) != `"1.2.3.0/24"` {
 		t.Error("Marshal of IP4Net failed with unexpected value: ", j)
 	}
+
+	n1.IncrementIP()
+	if n1.String() != "1.2.3.1/24" {
+		t.Error("IncrementIP() failed")
+	}
 }