Skip to content

Commit

Permalink
Merge pull request #1472 from manuelbuil/master
Browse files Browse the repository at this point in the history
Add incrementIP to the ip package
  • Loading branch information
manuelbuil authored Aug 27, 2021
2 parents 0da79c2 + c354899 commit fca1560
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"flag"
"fmt"
"math/big"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/ip/ip6net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions pkg/ip/ip6net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 0 deletions pkg/ip/ipnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions pkg/ip/ipnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit fca1560

Please sign in to comment.