From bf0b637311d819879f38e2b4b20564e6318830ef Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 16 Jul 2021 12:08:13 +0200 Subject: [PATCH] pass node hostname and subnet to graph handler --- cmd/kg/handlers.go | 12 ++++-------- cmd/kg/main.go | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/cmd/kg/handlers.go b/cmd/kg/handlers.go index 7f864a1..e3392d4 100644 --- a/cmd/kg/handlers.go +++ b/cmd/kg/handlers.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "mime" + "net" "net/http" "os" "os/exec" @@ -29,6 +30,8 @@ import ( type graphHandler struct { mesh *mesh.Mesh granularity mesh.Granularity + hostname *string + subnet *net.IPNet } 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 } - var hostname string - subnet := mesh.DefaultKiloSubnet nodes := make(map[string]*mesh.Node) for _, n := range ns { if n.Ready() { 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 { http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError) return @@ -66,7 +62,7 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 { http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError) return diff --git a/cmd/kg/main.go b/cmd/kg/main.go index 743e7af..3ac6ae4 100644 --- a/cmd/kg/main.go +++ b/cmd/kg/main.go @@ -197,7 +197,7 @@ func Main() error { // Run the HTTP server. mux := http.NewServeMux() 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{})) l, err := net.Listen("tcp", *listen) if err != nil {