From 9717e553005c303b4be44b0d9d7aa39a7a37e2f1 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Fri, 24 Jun 2016 09:48:05 +0100 Subject: [PATCH] Make it easier to disable weave integrations --- common/backoff/backoff.go | 2 +- common/weave/client.go | 16 +++++----- probe/overlay/weave.go | 61 ++++++++++++++++----------------------- prog/app.go | 5 ++-- prog/main.go | 6 ++++ prog/probe.go | 2 +- 6 files changed, 42 insertions(+), 50 deletions(-) diff --git a/common/backoff/backoff.go b/common/backoff/backoff.go index 5b7b5005a4..94f5ad6fbf 100644 --- a/common/backoff/backoff.go +++ b/common/backoff/backoff.go @@ -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) diff --git a/common/weave/client.go b/common/weave/client.go index 9835e17c9b..148e5e1425 100644 --- a/common/weave/client.go +++ b/common/weave/client.go @@ -10,7 +10,6 @@ import ( "regexp" "strconv" - log "github.com/Sirupsen/logrus" "github.com/ugorji/go/codec" "github.com/weaveworks/scope/common/exec" @@ -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) @@ -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 } diff --git a/probe/overlay/weave.go b/probe/overlay/weave.go index 3f27e85bd7..913117cd85 100644 --- a/probe/overlay/weave.go +++ b/probe/overlay/weave.go @@ -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 @@ -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 } @@ -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. diff --git a/prog/app.go b/prog/app.go index 536a547550..faf8952774 100644 --- a/prog/app.go +++ b/prog/app.go @@ -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) diff --git a/prog/main.go b/prog/main.go index 41859f0095..a3a7d6631e 100644 --- a/prog/main.go +++ b/prog/main.go @@ -80,6 +80,7 @@ type probeFlags struct { kubernetesAPI string kubernetesInterval time.Duration + weaveEnabled bool weaveAddr string weaveHostname string } @@ -91,6 +92,7 @@ type appFlags struct { logPrefix string logHTTP bool + weaveEnabled bool weaveAddr string weaveHostname string containerName string @@ -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. @@ -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 { diff --git a/prog/probe.go b/prog/probe.go index 2ffa4da8e4..43b58a1b98 100644 --- a/prog/probe.go +++ b/prog/probe.go @@ -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()