pass node hostname and subnet to graph handler

This commit is contained in:
Steffen Vogel 2021-07-16 12:08:13 +02:00
parent 02b1c5223b
commit bf0b637311
2 changed files with 5 additions and 9 deletions

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"io" "io"
"mime" "mime"
"net"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -29,6 +30,8 @@ import (
type graphHandler struct { type graphHandler struct {
mesh *mesh.Mesh mesh *mesh.Mesh
granularity mesh.Granularity granularity mesh.Granularity
hostname *string
subnet *net.IPNet
} }
func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -43,19 +46,12 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var hostname string
subnet := mesh.DefaultKiloSubnet
nodes := make(map[string]*mesh.Node) nodes := make(map[string]*mesh.Node)
for _, n := range ns { for _, n := range ns {
if n.Ready() { if n.Ready() {
nodes[n.Name] = n nodes[n.Name] = n
hostname = n.Name
}
if n.WireGuardIP != nil {
subnet = n.WireGuardIP
} }
} }
subnet.IP = subnet.IP.Mask(subnet.Mask)
if len(nodes) == 0 { if len(nodes) == 0 {
http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError) http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError)
return return
@ -66,7 +62,7 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
peers[p.Name] = p peers[p.Name] = p
} }
} }
topo, err := mesh.NewTopology(nodes, peers, h.granularity, hostname, 0, []byte{}, subnet, nodes[hostname].PersistentKeepalive, nil) topo, err := mesh.NewTopology(nodes, peers, h.granularity, *h.hostname, 0, []byte{}, h.subnet, nodes[*h.hostname].PersistentKeepalive, nil)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError)
return return

View File

@ -197,7 +197,7 @@ func Main() error {
// Run the HTTP server. // Run the HTTP server.
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/health", &healthHandler{}) mux.Handle("/health", &healthHandler{})
mux.Handle("/graph", &graphHandler{m, gr}) mux.Handle("/graph", &graphHandler{m, gr, hostname, s})
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{})) mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
l, err := net.Listen("tcp", *listen) l, err := net.Listen("tcp", *listen)
if err != nil { if err != nil {