2020-03-27 12:26:39 -06:00
|
|
|
package nebula
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
import (
|
2022-03-14 11:35:13 -06:00
|
|
|
"sync/atomic"
|
2021-11-03 19:54:04 -06:00
|
|
|
"time"
|
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
)
|
2020-03-27 12:26:39 -06:00
|
|
|
|
|
|
|
type Punchy struct {
|
2023-03-31 14:45:05 -06:00
|
|
|
punch atomic.Bool
|
|
|
|
respond atomic.Bool
|
|
|
|
delay atomic.Int64
|
|
|
|
respondDelay atomic.Int64
|
|
|
|
punchEverything atomic.Bool
|
|
|
|
l *logrus.Logger
|
2020-03-27 12:26:39 -06:00
|
|
|
}
|
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
func NewPunchyFromConfig(l *logrus.Logger, c *config.C) *Punchy {
|
|
|
|
p := &Punchy{l: l}
|
2020-03-27 12:26:39 -06:00
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
p.reload(c, true)
|
|
|
|
c.RegisterReloadCallback(func(c *config.C) {
|
|
|
|
p.reload(c, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Punchy) reload(c *config.C, initial bool) {
|
|
|
|
if initial {
|
|
|
|
var yes bool
|
|
|
|
if c.IsSet("punchy.punch") {
|
|
|
|
yes = c.GetBool("punchy.punch", false)
|
|
|
|
} else {
|
|
|
|
// Deprecated fallback
|
|
|
|
yes = c.GetBool("punchy", false)
|
|
|
|
}
|
|
|
|
|
2022-10-31 11:37:41 -06:00
|
|
|
p.punch.Store(yes)
|
2023-03-31 14:45:05 -06:00
|
|
|
if yes {
|
|
|
|
p.l.Info("punchy enabled")
|
|
|
|
} else {
|
|
|
|
p.l.Info("punchy disabled")
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
} else if c.HasChanged("punchy.punch") || c.HasChanged("punchy") {
|
|
|
|
//TODO: it should be relatively easy to support this, just need to be able to cancel the goroutine and boot it up from here
|
|
|
|
p.l.Warn("Changing punchy.punch with reload is not supported, ignoring.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if initial || c.HasChanged("punchy.respond") || c.HasChanged("punch_back") {
|
|
|
|
var yes bool
|
|
|
|
if c.IsSet("punchy.respond") {
|
|
|
|
yes = c.GetBool("punchy.respond", false)
|
|
|
|
} else {
|
|
|
|
// Deprecated fallback
|
|
|
|
yes = c.GetBool("punch_back", false)
|
|
|
|
}
|
|
|
|
|
2022-10-31 11:37:41 -06:00
|
|
|
p.respond.Store(yes)
|
2022-03-14 11:35:13 -06:00
|
|
|
|
|
|
|
if !initial {
|
|
|
|
p.l.Infof("punchy.respond changed to %v", p.GetRespond())
|
|
|
|
}
|
2020-03-27 12:26:39 -06:00
|
|
|
}
|
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
//NOTE: this will not apply to any in progress operations, only the next one
|
|
|
|
if initial || c.HasChanged("punchy.delay") {
|
2022-10-31 11:37:41 -06:00
|
|
|
p.delay.Store((int64)(c.GetDuration("punchy.delay", time.Second)))
|
2022-03-14 11:35:13 -06:00
|
|
|
if !initial {
|
|
|
|
p.l.Infof("punchy.delay changed to %s", p.GetDelay())
|
|
|
|
}
|
2020-03-27 12:26:39 -06:00
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
|
|
|
|
if initial || c.HasChanged("punchy.target_all_remotes") {
|
2023-05-04 14:16:37 -06:00
|
|
|
p.punchEverything.Store(c.GetBool("punchy.target_all_remotes", false))
|
2023-03-31 14:45:05 -06:00
|
|
|
if !initial {
|
|
|
|
p.l.WithField("target_all_remotes", p.GetTargetEverything()).Info("punchy.target_all_remotes changed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:32:35 -06:00
|
|
|
if initial || c.HasChanged("punchy.respond_delay") {
|
|
|
|
p.respondDelay.Store((int64)(c.GetDuration("punchy.respond_delay", 5*time.Second)))
|
|
|
|
if !initial {
|
|
|
|
p.l.Infof("punchy.respond_delay changed to %s", p.GetRespondDelay())
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 11:35:13 -06:00
|
|
|
}
|
2020-03-27 12:26:39 -06:00
|
|
|
|
2022-03-14 11:35:13 -06:00
|
|
|
func (p *Punchy) GetPunch() bool {
|
2022-10-31 11:37:41 -06:00
|
|
|
return p.punch.Load()
|
2022-03-14 11:35:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Punchy) GetRespond() bool {
|
2022-10-31 11:37:41 -06:00
|
|
|
return p.respond.Load()
|
2022-03-14 11:35:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Punchy) GetDelay() time.Duration {
|
2022-10-31 11:37:41 -06:00
|
|
|
return (time.Duration)(p.delay.Load())
|
2020-03-27 12:26:39 -06:00
|
|
|
}
|
2023-03-29 13:32:35 -06:00
|
|
|
|
|
|
|
func (p *Punchy) GetRespondDelay() time.Duration {
|
|
|
|
return (time.Duration)(p.respondDelay.Load())
|
|
|
|
}
|
2023-03-31 14:45:05 -06:00
|
|
|
|
|
|
|
func (p *Punchy) GetTargetEverything() bool {
|
|
|
|
return p.punchEverything.Load()
|
|
|
|
}
|