-
Notifications
You must be signed in to change notification settings - Fork 240
/
server.go
76 lines (63 loc) · 2.2 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package server
import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
_ "net/http/pprof"
"github.com/Azure/azure-container-networking/log"
npmconfig "github.com/Azure/azure-container-networking/npm/config"
"github.com/Azure/azure-container-networking/npm/http/api"
"github.com/Azure/azure-container-networking/npm/metrics"
"k8s.io/klog"
"github.com/gorilla/mux"
)
type NPMRestServer struct {
listeningAddress string
router *mux.Router
}
func NPMRestServerListenAndServe(config npmconfig.Config, npmEncoder json.Marshaler) {
rs := NPMRestServer{}
rs.router = mux.NewRouter()
// prometheus handlers
if config.Toggles.EnablePrometheusMetrics {
rs.router.Handle(api.NodeMetricsPath, metrics.GetHandler(metrics.NodeMetrics))
rs.router.Handle(api.ClusterMetricsPath, metrics.GetHandler(metrics.ClusterMetrics))
}
// the nil check is for fan-out npm
if config.Toggles.EnableHTTPDebugAPI && npmEncoder != nil {
// ACN CLI debug handlers
rs.router.Handle(api.NPMMgrPath, rs.npmCacheHandler(npmEncoder)).Methods(http.MethodGet)
}
if config.Toggles.EnablePprof {
rs.router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
rs.router.HandleFunc("/debug/pprof/", pprof.Index)
rs.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
rs.router.HandleFunc("/debug/pprof/profile", pprof.Profile)
rs.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
rs.router.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
// use default listening address if none is specified
if rs.listeningAddress == "" {
rs.listeningAddress = fmt.Sprintf("%s:%d", config.ListeningAddress, config.ListeningPort)
}
srv := &http.Server{
Handler: rs.router,
Addr: rs.listeningAddress,
}
klog.Infof("Starting NPM HTTP API on %s... ", rs.listeningAddress)
klog.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe())
}
func (n *NPMRestServer) npmCacheHandler(npmCacheEncoder json.Marshaler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := json.Marshal(npmCacheEncoder)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
_, err = w.Write(b)
if err != nil {
log.Errorf("failed to write resp: %v", err)
}
})
}