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:
@@ -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
|
||||
|
Reference in New Issue
Block a user