Skip to content

Commit d96246e

Browse files
authored
Move ipset deletion condition check from DeleteSet to DeleteFromSet. (#602)
1 parent ea5c9a7 commit d96246e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

npm/ipsm/ipsm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ func (ipsMgr *IpsetManager) DeleteFromSet(setName, ip string) error {
291291
return err
292292
}
293293

294-
ipsMgr.DeleteSet(setName)
294+
if len(ipsMgr.setMap[setName].elements) == 0 {
295+
ipsMgr.DeleteSet(setName)
296+
}
295297

296298
return nil
297299
}

npm/ipsm/ipsm_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,18 @@ func TestDeleteFromSet(t *testing.T) {
195195
t.Errorf("TestDeleteFromSet failed @ ipsMgr.AddToSet")
196196
}
197197

198+
if len(ipsMgr.setMap["test-set"].elements) != 1 {
199+
t.Errorf("TestDeleteFromSet failed @ ipsMgr.AddToSet")
200+
}
201+
198202
if err := ipsMgr.DeleteFromSet("test-set", "1.2.3.4"); err != nil {
199203
t.Errorf("TestDeleteFromSet failed @ ipsMgr.DeleteFromSet")
200204
}
205+
206+
// After deleting the only entry, "1.2.3.4" from "test-set", "test-set" ipset won't exist
207+
if _, exists := ipsMgr.setMap["test-set"]; exists {
208+
t.Errorf("TestDeleteFromSet failed @ ipsMgr.DeleteFromSet")
209+
}
201210
}
202211

203212
func TestClean(t *testing.T) {

0 commit comments

Comments
 (0)