Skip to content

Commit 3debbfa

Browse files
ryarnyahbzub
authored andcommitted
Cleanup ipset create function + restore (#177)
* Cleanup ipset create function + restore. Fix #176 Switch from array to map to simplify set use * Add sets initialization on create
1 parent c125778 commit 3debbfa

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

app/controllers/network_routes_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,9 @@ func deletePodSubnetIpSet() error {
676676
if err != nil {
677677
return err
678678
}
679-
ipset.Sets = append(ipset.Sets, &utils.Set{
679+
ipset.Sets[podSubnetIpSetName] = &utils.Set{
680680
Name: podSubnetIpSetName,
681-
})
681+
}
682682
err = ipset.Destroy()
683683
if err != nil {
684684
return errors.New("Failure deleting Pod egress ipset: " + err.Error())

utils/ipset.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type IPSet struct {
8484
// ipset bianry path.
8585
ipSetPath *string
8686
// Sets maintainted by ipset.
87-
Sets []*Set
87+
Sets map[string]*Set
8888
}
8989

9090
// Set reprensent a ipset set entry.
@@ -161,6 +161,7 @@ func NewIPSet() (*IPSet, error) {
161161
}
162162
ipSet := &IPSet{
163163
ipSetPath: ipSetPath,
164+
Sets: make(map[string]*Set),
164165
}
165166
return ipSet, nil
166167
}
@@ -170,8 +171,9 @@ func (ipSet *IPSet) Create(setName string, createOptions ...string) (*Set, error
170171
set := &Set{
171172
Name: setName,
172173
Options: createOptions,
174+
IPSet: ipSet,
173175
}
174-
ipSet.Sets = append(ipSet.Sets, set)
176+
ipSet.Sets[setName] = set
175177
_, err := ipSet.run(append([]string{"create", "-exist", set.Name}, createOptions...)...)
176178
if err != nil {
177179
return nil, err
@@ -234,20 +236,20 @@ func (set *IPSet) Destroy() error {
234236
// ex:
235237
// create KUBE-DST-3YNVZWWGX3UQQ4VQ hash:ip family inet hashsize 1024 maxelem 65536 timeout 0
236238
// add KUBE-DST-3YNVZWWGX3UQQ4VQ 100.96.1.6 timeout 0
237-
func parseIPSetSave(ipSet *IPSet, result string) []*Set {
238-
sets := make([]*Set, 0)
239+
func parseIPSetSave(ipSet *IPSet, result string) map[string]*Set {
240+
sets := make(map[string]*Set)
239241
// Save is always in order
240242
lines := strings.Split(result, "\n")
241243
for _, line := range lines {
242244
content := strings.Split(line, " ")
243245
if content[0] == "create" {
244-
sets = append(sets, &Set{
246+
sets[content[1]] = &Set{
245247
IPSet: ipSet,
246248
Name: content[1],
247249
Options: content[2:],
248-
})
250+
}
249251
} else if content[0] == "add" {
250-
set := sets[len(sets)-1]
252+
set := sets[content[1]]
251253
set.Entries = append(set.Entries, &Entry{
252254
Set: set,
253255
Options: content[2:],
@@ -265,15 +267,16 @@ func parseIPSetSave(ipSet *IPSet, result string) []*Set {
265267
func buildIPSetRestore(ipSet *IPSet) string {
266268
ipSetRestore := ""
267269
for _, set := range ipSet.Sets {
268-
ipSetRestore += fmt.Sprintf("create %s %v\n", set.Name, set.Options)
270+
ipSetRestore += fmt.Sprintf("create %s %s\n", set.Name, strings.Join(set.Options[:], " "))
269271
for _, entry := range set.Entries {
270-
ipSetRestore += fmt.Sprintf("add %s %v\n", set.Name, entry.Options)
272+
ipSetRestore += fmt.Sprintf("add %s %s\n", set.Name, strings.Join(entry.Options[:], " "))
271273
}
272274
}
273275
return ipSetRestore
274276
}
275277

276278
// Save the given set, or all sets if none is given to stdout in a format that restore can read. The option -file can be used to specify a filename instead of stdout.
279+
// save "ipset save" command output to ipset.sets.
277280
func (set *IPSet) Save() error {
278281
stdout, err := set.run("save")
279282
if err != nil {
@@ -284,6 +287,7 @@ func (set *IPSet) Save() error {
284287
}
285288

286289
// Restore a saved session generated by save. The saved session can be fed from stdin or the option -file can be used to specify a filename instead of stdin. Please note, existing sets and elements are not erased by restore unless specified so in the restore file. All commands are allowed in restore mode except list, help, version, interactive mode and restore itself.
290+
// Send formated ipset.sets into stdin of "ipset restore" command.
287291
func (set *IPSet) Restore() error {
288292
stdin := bytes.NewBufferString(buildIPSetRestore(set))
289293
_, err := set.runWithStdin(stdin, "restore", "-exist")
@@ -313,12 +317,7 @@ func (set *IPSet) Flush() error {
313317

314318
// Get Set by Name.
315319
func (ipset *IPSet) Get(setName string) *Set {
316-
for _, set := range ipset.Sets {
317-
if set.Name == setName {
318-
return set
319-
}
320-
}
321-
return nil
320+
return ipset.Sets[setName]
322321
}
323322

324323
// Rename a set. Set identified by SETNAME-TO must not exist.
@@ -354,7 +353,7 @@ func (set *Set) Refresh(entries []string, extraOptions ...string) error {
354353
Options: append([]string{entry}, extraOptions...),
355354
})
356355
}
357-
set.IPSet.Sets = append(set.IPSet.Sets, s)
356+
set.IPSet.Sets[tempName] = s
358357
err := set.IPSet.Restore()
359358
if err != nil {
360359
return err
@@ -370,5 +369,9 @@ func (set *Set) Refresh(entries []string, extraOptions ...string) error {
370369
return err
371370
}
372371

372+
s.Name = set.Name
373+
set.IPSet.Sets[set.Name] = s
374+
delete(set.IPSet.Sets, tempName)
375+
373376
return nil
374377
}

0 commit comments

Comments
 (0)