diff --git a/prog/weaver/discovery.go b/prog/weaver/discovery.go index 3331faa3..b298a2d7 100644 --- a/prog/weaver/discovery.go +++ b/prog/weaver/discovery.go @@ -69,8 +69,18 @@ func peerDiscoveryUpdate(discoveryEndpoint, token, peername, nickname string, ad return updateResponse.Addresses, err } -func HandleHTTPPeer(router *mux.Router, alloc *ipam.Allocator) { +func peerDiscoveryDelete(discoveryEndpoint, token, peername string) error { + request := PeerUpdateRequest{Name: peername} + return do("DELETE", discoveryEndpoint, token, request, nil) +} + +func HandleHTTPPeer(router *mux.Router, alloc *ipam.Allocator, discoveryEndpoint, token, peername string) { router.Methods("DELETE").Path("/peer").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if discoveryEndpoint != "" { + if err := peerDiscoveryDelete(discoveryEndpoint, token, peername); err != nil { + Log.Errorf("Error while deleting self from peer discovery: %s", err) + } + } if alloc != nil { alloc.Shutdown() } @@ -79,6 +89,12 @@ func HandleHTTPPeer(router *mux.Router, alloc *ipam.Allocator) { router.Methods("DELETE").Path("/peer/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ident := mux.Vars(r)["id"] + if discoveryEndpoint != "" { + // TODO: deal with this being either a peername or a nickname + if err := peerDiscoveryDelete(discoveryEndpoint, token, ident); err != nil { + Log.Errorf("Error while deleting self from peer discovery: %s", err) + } + } if alloc != nil { transferred := alloc.AdminTakeoverRanges(ident) fmt.Fprintf(w, "%d IPs taken over from %s\n", transferred, ident) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 7e277952..2a6b02e1 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -372,7 +372,7 @@ func main() { } router.HandleHTTP(muxRouter) HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver) - HandleHTTPPeer(muxRouter, allocator) + HandleHTTPPeer(muxRouter, allocator, discoveryEndpoint, token, name.String()) muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver)) http.Handle("/", common.LoggingHTTPHandler(muxRouter)) Log.Println("Listening for HTTP control messages on", httpAddr)