Skip to content

Commit

Permalink
Make it easier to disable weave integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Jun 24, 2016
1 parent 29133e5 commit 9717e55
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
2 changes: 1 addition & 1 deletion common/backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (b *backoff) Start() {

if shouldLog {
if err != nil {
log.Errorf("Error %s, backing off %s: %s",
log.Warnf("Error %s, backing off %s: %s",
b.msg, backoff, err)
} else {
log.Infof("Success %s", b.msg)
Expand Down
16 changes: 7 additions & 9 deletions common/weave/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"regexp"
"strconv"

log "github.com/Sirupsen/logrus"
"github.com/ugorji/go/codec"

"github.com/weaveworks/scope/common/exec"
Expand Down Expand Up @@ -132,11 +131,6 @@ func (c *client) PS() (map[string]PSEntry, error) {
if err := cmd.Start(); err != nil {
return nil, err
}
defer func() {
if err := cmd.Wait(); err != nil {
log.Errorf("'weave ps' cmd failed: %v", err)
}
}()

psEntriesByPrefix := map[string]PSEntry{}
scanner := bufio.NewScanner(out)
Expand All @@ -156,10 +150,14 @@ func (c *client) PS() (map[string]PSEntry, error) {
IPs: ips,
}
}
if err := scanner.Err(); err != nil {
return nil, err
scannerErr := scanner.Err()
cmdErr := cmd.Wait()
if cmdErr != nil {
return nil, cmdErr
}
if scannerErr != nil {
return nil, scannerErr
}

return psEntriesByPrefix, nil
}

Expand Down
61 changes: 25 additions & 36 deletions probe/overlay/weave.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Weave struct {
statusCache weave.Status
psCache map[string]weave.PSEntry

backoff backoff.Interface
backoff backoff.Interface
psBackoff backoff.Interface
}

// NewWeave returns a new Weave tagger based on the Weave router at
Expand All @@ -53,9 +54,14 @@ func NewWeave(hostID string, client weave.Client) *Weave {
psCache: map[string]weave.PSEntry{},
}

w.backoff = backoff.New(w.collect, "collecting weave info")
w.backoff = backoff.New(w.status, "collecting weave status")
w.backoff.SetInitialBackoff(5 * time.Second)
go w.backoff.Start()

w.psBackoff = backoff.New(w.ps, "collecting weave ps")
w.psBackoff.SetInitialBackoff(10 * time.Second)
go w.psBackoff.Start()

return w
}

Expand All @@ -65,52 +71,35 @@ func (*Weave) Name() string { return "Weave" }
// Stop gathering weave ps output.
func (w *Weave) Stop() {
w.backoff.Stop()
w.psBackoff.Stop()
}

func (w *Weave) collect() (done bool, err error) {
// If we fail to get info from weave
// we should wipe away stale data
defer func() {
if err != nil {
w.mtx.Lock()
defer w.mtx.Unlock()
w.statusCache = weave.Status{}
w.psCache = map[string]weave.PSEntry{}
}
}()

if err = w.ps(); err != nil {
return
}
if err = w.status(); err != nil {
return
}

return
}

func (w *Weave) ps() error {
func (w *Weave) ps() (bool, error) {
psEntriesByPrefix, err := w.client.PS()
if err != nil {
return err
}

w.mtx.Lock()
defer w.mtx.Unlock()
w.psCache = psEntriesByPrefix
return nil
}

func (w *Weave) status() error {
status, err := w.client.Status()
if err != nil {
return err
w.psCache = map[string]weave.PSEntry{}
} else {
w.psCache = psEntriesByPrefix
}
return false, err
}

func (w *Weave) status() (bool, error) {
status, err := w.client.Status()

w.mtx.Lock()
defer w.mtx.Unlock()
w.statusCache = status
return nil

if err != nil {
w.statusCache = weave.Status{}
} else {
w.statusCache = status
}
return false, err
}

// Tag implements Tagger.
Expand Down
5 changes: 2 additions & 3 deletions prog/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ func appMain(flags appFlags) {
}
})

// If user supplied a weave router address, periodically try and register
// out IP address in WeaveDNS.
if flags.weaveAddr != "" {
// Periodically try and register out IP address in WeaveDNS.
if flags.weaveEnabled {
weave, err := newWeavePublisher(
flags.dockerEndpoint, flags.weaveAddr,
flags.weaveHostname, flags.containerName)
Expand Down
6 changes: 6 additions & 0 deletions prog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type probeFlags struct {
kubernetesAPI string
kubernetesInterval time.Duration

weaveEnabled bool
weaveAddr string
weaveHostname string
}
Expand All @@ -91,6 +92,7 @@ type appFlags struct {
logPrefix string
logHTTP bool

weaveEnabled bool
weaveAddr string
weaveHostname string
containerName string
Expand All @@ -115,12 +117,14 @@ func main() {
flags = flags{}
mode string
debug bool
weaveEnabled bool
weaveHostname string
)

// Flags that apply to both probe and app
flag.StringVar(&mode, "mode", "help", "For internal use.")
flag.BoolVar(&debug, "debug", false, "Force debug logging.")
flag.BoolVar(&weaveEnabled, "weave", true, "Enable Weave Net integrations.")
flag.StringVar(&weaveHostname, "weave.hostname", "", "Hostname to advertise/lookup in WeaveDNS")

// We need to know how to parse them, but they are mainly interpreted by the entrypoint script.
Expand Down Expand Up @@ -200,6 +204,8 @@ func main() {
flags.probe.weaveHostname = weaveHostname
flags.app.weaveHostname = weaveHostname
}
flags.probe.weaveEnabled = weaveEnabled
flags.app.weaveEnabled = weaveEnabled
flags.probe.noApp = *noApp || *probeOnly

switch mode {
Expand Down
2 changes: 1 addition & 1 deletion prog/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func probeMain(flags probeFlags) {
}
}

if flags.weaveAddr != "" {
if flags.weaveEnabled {
client := weave.NewClient(sanitize.URL("http://", 6784, "")(flags.weaveAddr))
weave := overlay.NewWeave(hostID, client)
defer weave.Stop()
Expand Down

0 comments on commit 9717e55

Please sign in to comment.