Make Rule.Prepend() behave like Rule.Append() regarding uniqueness

This commit is contained in:
Alex Stockinger 2022-07-28 06:16:32 +00:00
parent e7743f4fa8
commit 5f6d6315e5

View File

@ -129,6 +129,15 @@ func NewIPv6Rule(table, chain string, spec ...string) Rule {
} }
func (r *rule) Prepend(client Client) error { func (r *rule) Prepend(client Client) error {
// TODO There's already a PR to implement InsertUnique() in go-iptables. Once that hopefully gets merged this should be replaced.
// https://github.com/coreos/go-iptables/pull/92
exists, err := client.Exists(r.table, r.chain, r.spec...)
if err != nil {
return err
}
if exists {
return nil
}
if err := client.Insert(r.table, r.chain, 1, r.spec...); err != nil { if err := client.Insert(r.table, r.chain, 1, r.spec...); err != nil {
return fmt.Errorf("failed to add iptables rule: %v", err) return fmt.Errorf("failed to add iptables rule: %v", err)
} }