mirror of https://github.com/k3s-io/k3s
refactor ipset interface AddEntry()
parent
a5c57521df
commit
9e9e264964
|
@ -123,7 +123,7 @@ func (set *IPSet) syncIPSetEntries() {
|
||||||
}
|
}
|
||||||
// Create active entries
|
// Create active entries
|
||||||
for _, entry := range set.activeEntries.Difference(currentIPSetEntries).List() {
|
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)
|
glog.Errorf("Failed to add entry: %v to ip set: %s, error: %v", entry, set.Name, err)
|
||||||
} else {
|
} else {
|
||||||
glog.V(3).Infof("Successfully add entry: %v to ip set: %s", entry, set.Name)
|
glog.V(3).Infof("Successfully add entry: %v to ip set: %s", entry, set.Name)
|
||||||
|
|
|
@ -34,10 +34,10 @@ type Interface interface {
|
||||||
DestroySet(set string) error
|
DestroySet(set string) error
|
||||||
// DestroyAllSets deletes all sets.
|
// DestroyAllSets deletes all sets.
|
||||||
DestroyAllSets() error
|
DestroyAllSets() error
|
||||||
// CreateSet creates a new set, it will ignore error when the set already exists if ignoreExistErr=true.
|
// CreateSet creates a new set. It will ignore error when the set already exists if ignoreExistErr=true.
|
||||||
CreateSet(set *IPSet, ignoreExistErr bool) error
|
CreateSet(set *IPSet, ignoreExistErr bool) error
|
||||||
// AddEntry adds a new entry to the named set.
|
// 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 string, ignoreExistErr bool) error
|
AddEntry(entry string, set *IPSet, ignoreExistErr bool) error
|
||||||
// DelEntry deletes one entry from the named set
|
// DelEntry deletes one entry from the named set
|
||||||
DelEntry(entry string, set string) error
|
DelEntry(entry string, set string) error
|
||||||
// Test test if an entry exists in the named set
|
// Test test if an entry exists in the named set
|
||||||
|
@ -48,6 +48,7 @@ type Interface interface {
|
||||||
ListSets() ([]string, error)
|
ListSets() ([]string, error)
|
||||||
// GetVersion returns the "X.Y" version string for ipset.
|
// GetVersion returns the "X.Y" version string for ipset.
|
||||||
GetVersion() (string, error)
|
GetVersion() (string, error)
|
||||||
|
// TODO: add comment message for ipset
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPSetCmd represents the ipset util. We use ipset command for ipset execute.
|
// 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.
|
// AddEntry adds a new entry to the named set.
|
||||||
// If the -exist option is specified, ipset ignores the error otherwise raised when
|
// If the -exist option is specified, ipset ignores the error otherwise raised when
|
||||||
// the same set (setname and create parameters are identical) already exists.
|
// the same set (setname and create parameters are identical) already exists.
|
||||||
func (runner *runner) AddEntry(entry string, set string, ignoreExistErr bool) error {
|
func (runner *runner) AddEntry(entry string, set *IPSet, ignoreExistErr bool) error {
|
||||||
args := []string{"add", set, entry}
|
args := []string{"add", set.Name, entry}
|
||||||
if ignoreExistErr {
|
if ignoreExistErr {
|
||||||
args = append(args, "-exist")
|
args = append(args, "-exist")
|
||||||
}
|
}
|
||||||
|
|
|
@ -233,6 +233,10 @@ func TestAddEntry(t *testing.T) {
|
||||||
SetType: HashIPPort,
|
SetType: HashIPPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testSet := &IPSet{
|
||||||
|
Name: "FOOBAR",
|
||||||
|
}
|
||||||
|
|
||||||
fcmd := fakeexec.FakeCmd{
|
fcmd := fakeexec.FakeCmd{
|
||||||
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
|
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
|
||||||
// Success
|
// Success
|
||||||
|
@ -254,7 +258,7 @@ func TestAddEntry(t *testing.T) {
|
||||||
}
|
}
|
||||||
runner := New(&fexec)
|
runner := New(&fexec)
|
||||||
// Create with ignoreExistErr = false, expect success
|
// Create with ignoreExistErr = false, expect success
|
||||||
err := runner.AddEntry(testEntry.String(), "FOOBAR", false)
|
err := runner.AddEntry(testEntry.String(), testSet, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
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])
|
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0])
|
||||||
}
|
}
|
||||||
// Create with ignoreExistErr = true, expect success
|
// Create with ignoreExistErr = true, expect success
|
||||||
err = runner.AddEntry(testEntry.String(), "FOOBAR", true)
|
err = runner.AddEntry(testEntry.String(), testSet, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected success, got %v", err)
|
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])
|
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[1])
|
||||||
}
|
}
|
||||||
// Create with ignoreExistErr = false, expect failure
|
// Create with ignoreExistErr = false, expect failure
|
||||||
err = runner.AddEntry(testEntry.String(), "FOOBAR", false)
|
err = runner.AddEntry(testEntry.String(), testSet, false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure, got nil")
|
t.Errorf("expected failure, got nil")
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,15 +93,15 @@ func (f *FakeIPSet) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddEntry is part of interface.
|
// AddEntry is part of interface.
|
||||||
func (f *FakeIPSet) AddEntry(entry string, set string, ignoreExistErr bool) error {
|
func (f *FakeIPSet) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error {
|
||||||
if f.Entries[set].Has(entry) {
|
if f.Entries[set.Name].Has(entry) {
|
||||||
if !ignoreExistErr {
|
if !ignoreExistErr {
|
||||||
// already exists
|
// already exists
|
||||||
return fmt.Errorf("Element cannot be added to the set: it's already added")
|
return fmt.Errorf("Element cannot be added to the set: it's already added")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
f.Entries[set].Insert(entry)
|
f.Entries[set.Name].Insert(entry)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue