@@ -83,6 +83,7 @@ const (
8383type IPSet struct {
8484 ipSetPath * string
8585 Sets map [string ]* Set
86+ isIpv6 bool
8687}
8788
8889// Set reprensent a ipset set entry.
@@ -146,14 +147,15 @@ func (ipset *IPSet) runWithStdin(stdin *bytes.Buffer, args ...string) (string, e
146147}
147148
148149// NewIPSet create a new IPSet with ipSetPath initialized.
149- func NewIPSet () (* IPSet , error ) {
150+ func NewIPSet (isIpv6 bool ) (* IPSet , error ) {
150151 ipSetPath , err := getIPSetPath ()
151152 if err != nil {
152153 return nil , err
153154 }
154155 ipSet := & IPSet {
155156 ipSetPath : ipSetPath ,
156157 Sets : make (map [string ]* Set ),
158+ isIpv6 : isIpv6 ,
157159 }
158160 return ipSet , nil
159161}
@@ -180,13 +182,22 @@ func (ipset *IPSet) Create(setName string, createOptions ...string) (*Set, error
180182
181183 // Create set if missing from the system
182184 if ! setIsActive {
183- _ , err := ipset .run (append ([]string {"create" , "-exist" , setName },
184- createOptions ... )... )
185- if err != nil {
186- return nil , fmt .Errorf ("Failed to create ipset set on system: %s" , err )
185+ if ipset .isIpv6 {
186+ // Add "family inet6" option and a "inet6:" prefix for IPv6 sets.
187+ args := []string {"create" , "-exist" , ipset .Sets [setName ].name ()}
188+ args = append (args , createOptions ... )
189+ args = append (args , "family" , "inet6" )
190+ if _ , err := ipset .run (args ... ); err != nil {
191+ return nil , fmt .Errorf ("Failed to create ipset set on system: %s" , err )
192+ }
193+ } else {
194+ _ , err := ipset .run (append ([]string {"create" , "-exist" , setName },
195+ createOptions ... )... )
196+ if err != nil {
197+ return nil , fmt .Errorf ("Failed to create ipset set on system: %s" , err )
198+ }
187199 }
188200 }
189-
190201 return ipset .Sets [setName ], nil
191202}
192203
@@ -215,7 +226,7 @@ func (set *Set) Add(addOptions ...string) (*Entry, error) {
215226 Options : addOptions ,
216227 }
217228 set .Entries = append (set .Entries , entry )
218- _ , err := set .Parent .run (append ([]string {"add" , "-exist" , entry .Set .Name }, addOptions ... )... )
229+ _ , err := set .Parent .run (append ([]string {"add" , "-exist" , entry .Set .name () }, addOptions ... )... )
219230 if err != nil {
220231 return nil , err
221232 }
@@ -225,7 +236,7 @@ func (set *Set) Add(addOptions ...string) (*Entry, error) {
225236// Del an entry from a set. If the -exist option is specified and the entry is
226237// not in the set (maybe already expired), then the command is ignored.
227238func (entry * Entry ) Del () error {
228- _ , err := entry .Set .Parent .run (append ([]string {"del" , entry .Set .Name }, entry .Options ... )... )
239+ _ , err := entry .Set .Parent .run (append ([]string {"del" , entry .Set .name () }, entry .Options ... )... )
229240 if err != nil {
230241 return err
231242 }
@@ -236,7 +247,7 @@ func (entry *Entry) Del() error {
236247// Test wether an entry is in a set or not. Exit status number is zero if the
237248// tested entry is in the set and nonzero if it is missing from the set.
238249func (set * Set ) Test (testOptions ... string ) (bool , error ) {
239- _ , err := set .Parent .run (append ([]string {"test" , set .Name }, testOptions ... )... )
250+ _ , err := set .Parent .run (append ([]string {"test" , set .name () }, testOptions ... )... )
240251 if err != nil {
241252 return false , err
242253 }
@@ -246,13 +257,12 @@ func (set *Set) Test(testOptions ...string) (bool, error) {
246257// Destroy the specified set or all the sets if none is given. If the set has
247258// got reference(s), nothing is done and no set destroyed.
248259func (set * Set ) Destroy () error {
249- _ , err := set .Parent .run ("destroy" , set .Name )
260+ _ , err := set .Parent .run ("destroy" , set .name () )
250261 if err != nil {
251262 return err
252263 }
253264
254265 delete (set .Parent .Sets , set .Name )
255-
256266 return nil
257267}
258268
@@ -287,7 +297,7 @@ func (ipset *IPSet) DestroyAllWithin() error {
287297
288298// IsActive checks if a set exists on the system with the same name.
289299func (set * Set ) IsActive () (bool , error ) {
290- _ , err := set .Parent .run ("list" , set .Name )
300+ _ , err := set .Parent .run ("list" , set .name () )
291301 if err != nil {
292302 if strings .Contains (err .Error (), "name does not exist" ) {
293303 return false , nil
@@ -297,6 +307,14 @@ func (set *Set) IsActive() (bool, error) {
297307 return true , nil
298308}
299309
310+ func (set * Set ) name () string {
311+ if set .Parent .isIpv6 {
312+ return "inet6:" + set .Name
313+ } else {
314+ return set .Name
315+ }
316+ }
317+
300318// Parse ipset save stdout.
301319// ex:
302320// create KUBE-DST-3YNVZWWGX3UQQ4VQ hash:ip family inet hashsize 1024 maxelem 65536 timeout 0
@@ -398,7 +416,10 @@ func (ipset *IPSet) Get(setName string) *Set {
398416
399417// Rename a set. Set identified by SETNAME-TO must not exist.
400418func (set * Set ) Rename (newName string ) error {
401- _ , err := set .Parent .run ("rename" , set .Name , newName )
419+ if set .Parent .isIpv6 {
420+ newName = "ipv6:" + newName
421+ }
422+ _ , err := set .Parent .run ("rename" , set .name (), newName )
402423 if err != nil {
403424 return err
404425 }
@@ -409,7 +430,7 @@ func (set *Set) Rename(newName string) error {
409430// sets. The referred sets must exist and compatible type of sets can be
410431// swapped only.
411432func (set * Set ) Swap (setTo * Set ) error {
412- _ , err := set .Parent .run ("swap" , set .Name , setTo .Name )
433+ _ , err := set .Parent .run ("swap" , set .name () , setTo .name () )
413434 if err != nil {
414435 return err
415436 }
0 commit comments