-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit reworks getAvailableIp with a "simpler" version that will look for the first available IP address in our IP Prefix. There is a couple of ideas behind this: * Make the host IPs reasonably predictable and in within similar subnets, which should simplify ACLs for subnets * The code is not random, but deterministic so we can have tests * The code is a bit more understandable (no bit shift magic)
- Loading branch information
Showing
4 changed files
with
166 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package headscale | ||
|
||
import ( | ||
"gopkg.in/check.v1" | ||
"inet.af/netaddr" | ||
) | ||
|
||
func (s *Suite) TestGetAvailableIp(c *check.C) { | ||
ip, err := h.getAvailableIP() | ||
|
||
c.Assert(err, check.IsNil) | ||
|
||
expected := netaddr.MustParseIP("10.27.0.0") | ||
|
||
c.Assert(ip.String(), check.Equals, expected.String()) | ||
} | ||
|
||
func (s *Suite) TestGetUsedIps(c *check.C) { | ||
ip, err := h.getAvailableIP() | ||
c.Assert(err, check.IsNil) | ||
|
||
n, err := h.CreateNamespace("test_ip") | ||
c.Assert(err, check.IsNil) | ||
|
||
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil) | ||
c.Assert(err, check.IsNil) | ||
|
||
_, err = h.GetMachine("test", "testmachine") | ||
c.Assert(err, check.NotNil) | ||
|
||
m := Machine{ | ||
ID: 0, | ||
MachineKey: "foo", | ||
NodeKey: "bar", | ||
DiscoKey: "faa", | ||
Name: "testmachine", | ||
NamespaceID: n.ID, | ||
Registered: true, | ||
RegisterMethod: "authKey", | ||
AuthKeyID: uint(pak.ID), | ||
IPAddress: ip.String(), | ||
} | ||
h.db.Save(&m) | ||
|
||
ips, err := h.getUsedIPs() | ||
|
||
c.Assert(err, check.IsNil) | ||
|
||
expected := netaddr.MustParseIP("10.27.0.0") | ||
|
||
c.Assert(ips[0], check.Equals, expected) | ||
} | ||
|
||
func (s *Suite) TestGetMultiIp(c *check.C) { | ||
n, err := h.CreateNamespace("test-ip-multi") | ||
c.Assert(err, check.IsNil) | ||
|
||
for i := 1; i <= 350; i++ { | ||
ip, err := h.getAvailableIP() | ||
c.Assert(err, check.IsNil) | ||
|
||
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil) | ||
c.Assert(err, check.IsNil) | ||
|
||
_, err = h.GetMachine("test", "testmachine") | ||
c.Assert(err, check.NotNil) | ||
|
||
m := Machine{ | ||
ID: 0, | ||
MachineKey: "foo", | ||
NodeKey: "bar", | ||
DiscoKey: "faa", | ||
Name: "testmachine", | ||
NamespaceID: n.ID, | ||
Registered: true, | ||
RegisterMethod: "authKey", | ||
AuthKeyID: uint(pak.ID), | ||
IPAddress: ip.String(), | ||
} | ||
h.db.Save(&m) | ||
} | ||
|
||
ips, err := h.getUsedIPs() | ||
|
||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(len(ips), check.Equals, 350) | ||
|
||
c.Assert(ips[0], check.Equals, netaddr.MustParseIP("10.27.0.0")) | ||
c.Assert(ips[9], check.Equals, netaddr.MustParseIP("10.27.0.9")) | ||
c.Assert(ips[300], check.Equals, netaddr.MustParseIP("10.27.1.44")) | ||
|
||
expectedNextIP := netaddr.MustParseIP("10.27.1.94") | ||
nextIP, err := h.getAvailableIP() | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(nextIP.String(), check.Equals, expectedNextIP.String()) | ||
|
||
// If we call get Available again, we should receive | ||
// the same IP, as it has not been reserved. | ||
nextIP2, err := h.getAvailableIP() | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(nextIP2.String(), check.Equals, expectedNextIP.String()) | ||
} |