Skip to content

Commit

Permalink
Fix watch error when http & https are disabled
Browse files Browse the repository at this point in the history
Remove an error in watch reloading that happens when http and https
are both disabled, and use an https address for running watches if
no http addresses are present.

Fixes #3425.
  • Loading branch information
kyhavlov committed Sep 25, 2017
1 parent 8bc8c25 commit e54161d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
35 changes: 24 additions & 11 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,19 @@ func (a *Agent) serveHTTP(l net.Listener, srv *HTTPServer) error {
// reloadWatches stops any existing watch plans and attempts to load the given
// set of watches.
func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error {
// Stop the current watches.
for _, wp := range a.watchPlans {
wp.Stop()
}
a.watchPlans = nil

// Return if there are no watches now.
if len(cfg.Watches) == 0 {
return nil
}

// Watches use the API to talk to this agent, so that must be enabled.
if len(cfg.HTTPAddrs) == 0 {
if len(cfg.HTTPAddrs) == 0 && len(cfg.HTTPSAddrs) == 0 {
return fmt.Errorf("watch plans require an HTTP or HTTPS endpoint")
}

Expand All @@ -539,16 +550,18 @@ func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error {
watchPlans = append(watchPlans, wp)
}

// Stop the current watches.
for _, wp := range a.watchPlans {
wp.Stop()
}
a.watchPlans = nil

// deterine the primary http endpoint
addr := cfg.HTTPAddrs[0].String()
if cfg.HTTPAddrs[0].Network() == "unix" {
addr = "unix://" + addr
// Determine the primary http(s) endpoint.
var addr string
if len(cfg.HTTPAddrs) > 0 {
addr = cfg.HTTPAddrs[0].String()
if cfg.HTTPAddrs[0].Network() == "unix" {
addr = "unix://" + addr
}
} else {
addr = cfg.HTTPSAddrs[0].String()
if cfg.HTTPSAddrs[0].Network() == "unix" {
addr = "unix://" + addr
}
}

// Fire off a goroutine for each new watch plan.
Expand Down
46 changes: 46 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1918,3 +1918,49 @@ func TestAgent_GetCoordinate(t *testing.T) {
check(true)
check(false)
}

func TestAgent_reloadWatches(t *testing.T) {
t.Parallel()
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()

// Normal watch with http addr set, should succeed
newConf := *a.config
newConf.Watches = []map[string]interface{}{
{
"type": "key",
"key": "asdf",
"handler": "ls",
},
}
if err := a.reloadWatches(&newConf); err != nil {
t.Fatalf("bad: %s", err)
}

// Should still succeed with only HTTPS addresses
newConf.HTTPSAddrs = newConf.HTTPAddrs
newConf.HTTPAddrs = make([]net.Addr, 0)
newConf.Watches = []map[string]interface{}{
{
"type": "key",
"key": "asdf",
"handler": "ls",
},
}
if err := a.reloadWatches(&newConf); err != nil {
t.Fatalf("bad: %s", err)
}

// Should fail to reload with no http or https addrs
newConf.HTTPSAddrs = make([]net.Addr, 0)
newConf.Watches = []map[string]interface{}{
{
"type": "key",
"key": "asdf",
"handler": "ls",
},
}
if err := a.reloadWatches(&newConf); err == nil || !strings.Contains(err.Error(), "watch plans require an HTTP or HTTPS endpoint") {
t.Fatalf("bad: %s", err)
}
}

0 comments on commit e54161d

Please sign in to comment.