Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
2b5a10f8
Commit
2b5a10f8
authored
Dec 23, 2016
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a ForEach() to IP allocator
parent
f75bed86
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
allocator.go
pkg/registry/core/service/ipallocator/allocator.go
+9
-0
allocator_test.go
pkg/registry/core/service/ipallocator/allocator_test.go
+37
-0
No files found.
pkg/registry/core/service/ipallocator/allocator.go
View file @
2b5a10f8
...
...
@@ -32,6 +32,7 @@ type Interface interface {
Allocate
(
net
.
IP
)
error
AllocateNext
()
(
net
.
IP
,
error
)
Release
(
net
.
IP
)
error
ForEach
(
func
(
net
.
IP
))
}
var
(
...
...
@@ -146,6 +147,14 @@ func (r *Range) Release(ip net.IP) error {
return
r
.
alloc
.
Release
(
offset
)
}
// ForEach calls the provided function for each allocated IP.
func
(
r
*
Range
)
ForEach
(
fn
func
(
net
.
IP
))
{
r
.
alloc
.
ForEach
(
func
(
offset
int
)
{
ip
,
_
:=
GetIndexedIP
(
r
.
net
,
offset
+
1
)
// +1 because Range doesn't store IP 0
fn
(
ip
)
})
}
// Has returns true if the provided IP is already allocated and a call
// to Allocate(ip) would fail with ErrAllocated.
func
(
r
*
Range
)
Has
(
ip
net
.
IP
)
bool
{
...
...
pkg/registry/core/service/ipallocator/allocator_test.go
View file @
2b5a10f8
...
...
@@ -167,6 +167,43 @@ func TestRangeSize(t *testing.T) {
}
}
func
TestForEach
(
t
*
testing
.
T
)
{
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
"192.168.1.0/24"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
testCases
:=
[]
sets
.
String
{
sets
.
NewString
(),
sets
.
NewString
(
"192.168.1.1"
),
sets
.
NewString
(
"192.168.1.1"
,
"192.168.1.254"
),
sets
.
NewString
(
"192.168.1.1"
,
"192.168.1.128"
,
"192.168.1.254"
),
}
for
i
,
tc
:=
range
testCases
{
r
:=
NewCIDRRange
(
cidr
)
for
ips
:=
range
tc
{
ip
:=
net
.
ParseIP
(
ips
)
if
err
:=
r
.
Allocate
(
ip
);
err
!=
nil
{
t
.
Errorf
(
"[%d] error allocating IP %v: %v"
,
i
,
ip
,
err
)
}
if
!
r
.
Has
(
ip
)
{
t
.
Errorf
(
"[%d] expected IP %v allocated"
,
i
,
ip
)
}
}
calls
:=
sets
.
NewString
()
r
.
ForEach
(
func
(
ip
net
.
IP
)
{
calls
.
Insert
(
ip
.
String
())
})
if
len
(
calls
)
!=
len
(
tc
)
{
t
.
Errorf
(
"[%d] expected %d calls, got %d"
,
i
,
len
(
tc
),
len
(
calls
))
}
if
!
calls
.
Equal
(
tc
)
{
t
.
Errorf
(
"[%d] expected calls to equal testcase: %v vs %v"
,
i
,
calls
.
List
(),
tc
.
List
())
}
}
}
func
TestSnapshot
(
t
*
testing
.
T
)
{
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
"192.168.1.0/24"
)
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment