Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
add retries around iptable rule removal (#402)
Browse files Browse the repository at this point in the history
* add retries around iptable rule removal

* add retries

* update log message
  • Loading branch information
eytan-avisror authored May 18, 2020
1 parent f041ea2 commit 92855a4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/kiam/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package main
import (
"fmt"
"strings"
"time"

"github.com/coreos/go-iptables/iptables"
log "github.com/sirupsen/logrus"
)

type rules struct {
Expand Down Expand Up @@ -59,13 +61,32 @@ func (r *rules) ruleSpec() []string {
return rules
}

var (
retryInterval = time.Millisecond * 500
maxAttempts = 30
)

func (r *rules) Remove() error {
ipt, err := iptables.New()
if err != nil {
return err
}

return ipt.Delete("nat", "PREROUTING", r.ruleSpec()...)
var attempt int
for {
if attempt >= maxAttempts {
log.Errorf("failed to remove iptables rule, retries exhausted: %s", err.Error())
break
}
if err := ipt.Delete("nat", "PREROUTING", r.ruleSpec()...); err == nil {
log.Info("iptables rule was successfully removed")
break
}
log.Warnf("failed to remove iptables rule, will retry: %s", err.Error())
time.Sleep(retryInterval)
attempt++
}
return nil
}

func (r *rules) kiamAddress() string {
Expand Down

0 comments on commit 92855a4

Please sign in to comment.