Fix watch error when http & https are disabled (#3493)

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.
This commit is contained in:
Kyle Havlovitz 2017-09-26 13:47:27 -07:00 committed by GitHub
parent eb42fbdccf
commit bfa70a10ca
2 changed files with 66 additions and 9 deletions

View File

@ -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 // reloadWatches stops any existing watch plans and attempts to load the given
// set of watches. // set of watches.
func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error { 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. // 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") return fmt.Errorf("watch plans require an HTTP or HTTPS endpoint")
} }
@ -539,15 +550,15 @@ func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error {
watchPlans = append(watchPlans, wp) watchPlans = append(watchPlans, wp)
} }
// Stop the current watches. // Determine the primary http(s) endpoint.
for _, wp := range a.watchPlans { var netaddr net.Addr
wp.Stop() if len(cfg.HTTPAddrs) > 0 {
netaddr = cfg.HTTPAddrs[0]
} else {
netaddr = cfg.HTTPSAddrs[0]
} }
a.watchPlans = nil addr := netaddr.String()
if netaddr.Network() == "unix" {
// deterine the primary http endpoint
addr := cfg.HTTPAddrs[0].String()
if cfg.HTTPAddrs[0].Network() == "unix" {
addr = "unix://" + addr addr = "unix://" + addr
} }

View File

@ -1918,3 +1918,49 @@ func TestAgent_GetCoordinate(t *testing.T) {
check(true) check(true)
check(false) 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)
}
}