Skip to content

Commit

Permalink
Make it easier to disable weave integrations (#1610)
Browse files Browse the repository at this point in the history
* Make it easier to disable weave integrations

* Review feedback

* Make test pass
  • Loading branch information
tomwilkie authored Jun 27, 2016
1 parent 1b32cc9 commit ab3d34b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 52 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
18 changes: 8 additions & 10 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 All @@ -173,7 +171,7 @@ func (c *client) Expose() error {
// Alread exposed!
return nil
}
if err := exec.Command("weave", "expose").Run(); err != nil {
if err := exec.Command("weave", "--local", "expose").Run(); err != nil {
return fmt.Errorf("Error running weave expose: %v", err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion common/weave/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestExpose(t *testing.T) {

psCalled, exposeCalled := false, false
exec.Command = func(name string, args ...string) exec.Cmd {
if args[0] == "expose" {
if len(args) >= 2 && args[1] == "expose" {
exposeCalled = true
return testExec.NewMockCmdString("")
}
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 @@ -81,6 +81,7 @@ type probeFlags struct {
kubernetesAPI string
kubernetesInterval time.Duration

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

weaveEnabled bool
weaveAddr string
weaveHostname string
containerName string
Expand All @@ -116,6 +118,7 @@ func main() {
flags = flags{}
mode string
debug bool
weaveEnabled bool
weaveHostname string
dryRun bool
)
Expand All @@ -124,6 +127,7 @@ func main() {
flag.StringVar(&mode, "mode", "help", "For internal use.")
flag.BoolVar(&debug, "debug", false, "Force debug logging.")
flag.BoolVar(&dryRun, "dry-run", false, "Don't start scope, just parse the arguments. For internal use only.")
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 @@ -203,6 +207,8 @@ func main() {
flags.probe.weaveHostname = weaveHostname
flags.app.weaveHostname = weaveHostname
}
flags.probe.weaveEnabled = weaveEnabled
flags.app.weaveEnabled = weaveEnabled
flags.probe.noApp = *noApp || *probeOnly

// Special case for #1191, check listen address is well formed
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 ab3d34b

Please sign in to comment.