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
9e9e2649
Commit
9e9e2649
authored
Dec 18, 2017
by
m1093782566
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor ipset interface AddEntry()
parent
a5c57521
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
12 deletions
+17
-12
ipset.go
pkg/proxy/ipvs/ipset.go
+1
-1
ipset.go
pkg/util/ipset/ipset.go
+6
-5
ipset_test.go
pkg/util/ipset/ipset_test.go
+7
-3
fake.go
pkg/util/ipset/testing/fake.go
+3
-3
No files found.
pkg/proxy/ipvs/ipset.go
View file @
9e9e2649
...
...
@@ -123,7 +123,7 @@ func (set *IPSet) syncIPSetEntries() {
}
// Create active entries
for
_
,
entry
:=
range
set
.
activeEntries
.
Difference
(
currentIPSetEntries
)
.
List
()
{
if
err
:=
set
.
handle
.
AddEntry
(
entry
,
set
.
Name
,
true
);
err
!=
nil
{
if
err
:=
set
.
handle
.
AddEntry
(
entry
,
&
set
.
IPSet
,
true
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to add entry: %v to ip set: %s, error: %v"
,
entry
,
set
.
Name
,
err
)
}
else
{
glog
.
V
(
3
)
.
Infof
(
"Successfully add entry: %v to ip set: %s"
,
entry
,
set
.
Name
)
...
...
pkg/util/ipset/ipset.go
View file @
9e9e2649
...
...
@@ -34,10 +34,10 @@ type Interface interface {
DestroySet
(
set
string
)
error
// DestroyAllSets deletes all sets.
DestroyAllSets
()
error
// CreateSet creates a new set
, i
t will ignore error when the set already exists if ignoreExistErr=true.
// CreateSet creates a new set
. I
t will ignore error when the set already exists if ignoreExistErr=true.
CreateSet
(
set
*
IPSet
,
ignoreExistErr
bool
)
error
// AddEntry adds a new entry to the named set.
AddEntry
(
entry
string
,
set
string
,
ignoreExistErr
bool
)
error
// AddEntry adds a new entry to the named set.
It will ignore error when the entry already exists if ignoreExistErr=true.
AddEntry
(
entry
string
,
set
*
IPSet
,
ignoreExistErr
bool
)
error
// DelEntry deletes one entry from the named set
DelEntry
(
entry
string
,
set
string
)
error
// Test test if an entry exists in the named set
...
...
@@ -48,6 +48,7 @@ type Interface interface {
ListSets
()
([]
string
,
error
)
// GetVersion returns the "X.Y" version string for ipset.
GetVersion
()
(
string
,
error
)
// TODO: add comment message for ipset
}
// IPSetCmd represents the ipset util. We use ipset command for ipset execute.
...
...
@@ -198,8 +199,8 @@ func (runner *runner) createSet(set *IPSet, ignoreExistErr bool) error {
// AddEntry adds a new entry to the named set.
// If the -exist option is specified, ipset ignores the error otherwise raised when
// the same set (setname and create parameters are identical) already exists.
func
(
runner
*
runner
)
AddEntry
(
entry
string
,
set
string
,
ignoreExistErr
bool
)
error
{
args
:=
[]
string
{
"add"
,
set
,
entry
}
func
(
runner
*
runner
)
AddEntry
(
entry
string
,
set
*
IPSet
,
ignoreExistErr
bool
)
error
{
args
:=
[]
string
{
"add"
,
set
.
Name
,
entry
}
if
ignoreExistErr
{
args
=
append
(
args
,
"-exist"
)
}
...
...
pkg/util/ipset/ipset_test.go
View file @
9e9e2649
...
...
@@ -233,6 +233,10 @@ func TestAddEntry(t *testing.T) {
SetType
:
HashIPPort
,
}
testSet
:=
&
IPSet
{
Name
:
"FOOBAR"
,
}
fcmd
:=
fakeexec
.
FakeCmd
{
CombinedOutputScript
:
[]
fakeexec
.
FakeCombinedOutputAction
{
// Success
...
...
@@ -254,7 +258,7 @@ func TestAddEntry(t *testing.T) {
}
runner
:=
New
(
&
fexec
)
// Create with ignoreExistErr = false, expect success
err
:=
runner
.
AddEntry
(
testEntry
.
String
(),
"FOOBAR"
,
false
)
err
:=
runner
.
AddEntry
(
testEntry
.
String
(),
testSet
,
false
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
...
...
@@ -265,7 +269,7 @@ func TestAddEntry(t *testing.T) {
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
0
])
}
// Create with ignoreExistErr = true, expect success
err
=
runner
.
AddEntry
(
testEntry
.
String
(),
"FOOBAR"
,
true
)
err
=
runner
.
AddEntry
(
testEntry
.
String
(),
testSet
,
true
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
...
...
@@ -276,7 +280,7 @@ func TestAddEntry(t *testing.T) {
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
1
])
}
// Create with ignoreExistErr = false, expect failure
err
=
runner
.
AddEntry
(
testEntry
.
String
(),
"FOOBAR"
,
false
)
err
=
runner
.
AddEntry
(
testEntry
.
String
(),
testSet
,
false
)
if
err
==
nil
{
t
.
Errorf
(
"expected failure, got nil"
)
}
...
...
pkg/util/ipset/testing/fake.go
View file @
9e9e2649
...
...
@@ -93,15 +93,15 @@ func (f *FakeIPSet) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error {
}
// AddEntry is part of interface.
func
(
f
*
FakeIPSet
)
AddEntry
(
entry
string
,
set
string
,
ignoreExistErr
bool
)
error
{
if
f
.
Entries
[
set
]
.
Has
(
entry
)
{
func
(
f
*
FakeIPSet
)
AddEntry
(
entry
string
,
set
*
ipset
.
IPSet
,
ignoreExistErr
bool
)
error
{
if
f
.
Entries
[
set
.
Name
]
.
Has
(
entry
)
{
if
!
ignoreExistErr
{
// already exists
return
fmt
.
Errorf
(
"Element cannot be added to the set: it's already added"
)
}
return
nil
}
f
.
Entries
[
set
]
.
Insert
(
entry
)
f
.
Entries
[
set
.
Name
]
.
Insert
(
entry
)
return
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