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.
pull/3502/head
Kyle Havlovitz 7 years ago committed by GitHub
parent eb42fbdccf
commit bfa70a10ca

@ -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")
}
@ -539,15 +550,15 @@ func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error {
watchPlans = append(watchPlans, wp)
}
// Stop the current watches.
for _, wp := range a.watchPlans {
wp.Stop()
// Determine the primary http(s) endpoint.
var netaddr net.Addr
if len(cfg.HTTPAddrs) > 0 {
netaddr = cfg.HTTPAddrs[0]
} else {
netaddr = cfg.HTTPSAddrs[0]
}
a.watchPlans = nil
// deterine the primary http endpoint
addr := cfg.HTTPAddrs[0].String()
if cfg.HTTPAddrs[0].Network() == "unix" {
addr := netaddr.String()
if netaddr.Network() == "unix" {
addr = "unix://" + addr
}

@ -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)
}
}

Loading…
Cancel
Save