cmd/kg,pkg: add --resync-period flag

This commit introduces a new `--resync-period` flag to control how often
the Kilo controllers should reconcile.

Signed-off-by: Lucas Servén Marín <lserven@gmail.com>
This commit is contained in:
Lucas Servén Marín
2021-02-28 18:33:01 +01:00
parent c060bf24e2
commit 8dbbc636b5
8 changed files with 58 additions and 68 deletions

View File

@@ -198,10 +198,11 @@ func chainToString(table, chain string) string {
// Controller is able to reconcile a given set of iptables rules.
type Controller struct {
v4 Client
v6 Client
errors chan error
logger log.Logger
v4 Client
v6 Client
errors chan error
logger log.Logger
resyncPeriod time.Duration
sync.Mutex
rules []Rule
@@ -218,6 +219,13 @@ func WithLogger(logger log.Logger) ControllerOption {
}
}
// WithResyncPeriod modifies how often the controller reconciles.
func WithResyncPeriod(resyncPeriod time.Duration) ControllerOption {
return func(c *Controller) {
c.resyncPeriod = resyncPeriod
}
}
// WithClients adds iptables clients to the controller.
func WithClients(v4, v6 Client) ControllerOption {
return func(c *Controller) {
@@ -266,16 +274,18 @@ func (c *Controller) Run(stop <-chan struct{}) (<-chan error, error) {
c.subscribed = true
c.Unlock()
go func() {
t := time.NewTimer(c.resyncPeriod)
defer close(c.errors)
for {
select {
case <-time.After(30 * time.Second):
case <-t.C:
if err := c.reconcile(); err != nil {
nonBlockingSend(c.errors, fmt.Errorf("failed to reconcile rules: %v", err))
}
t.Reset(c.resyncPeriod)
case <-stop:
return
}
if err := c.reconcile(); err != nil {
nonBlockingSend(c.errors, fmt.Errorf("failed to reconcile rules: %v", err))
}
}
}()
return c.errors, nil