Skip to content

Commit d169929

Browse files
authored
Npm debug tools (#817)
* add inital debug tools * export member variables for debug api * add dependencies * update metrics and tests * remove refactor artifacts
1 parent 9a352f2 commit d169929

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+20578
-274
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/docker/libnetwork v0.5.6
1313
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
1414
github.com/google/uuid v1.1.1
15+
github.com/gorilla/mux v1.8.0
1516
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
1617
github.com/hashicorp/golang-lru v0.5.3 // indirect
1718
github.com/imdario/mergo v0.3.8 // indirect
@@ -26,6 +27,7 @@ require (
2627
github.com/spf13/cobra v0.0.5
2728
github.com/spf13/pflag v1.0.5
2829
github.com/spf13/viper v1.3.2
30+
github.com/stretchr/testify v1.7.0
2931
go.opencensus.io v0.22.2 // indirect
3032
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 // indirect
3133
golang.org/x/net v0.0.0-20191112182307-2180aed22343 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyyc
222222
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
223223
github.com/googleapis/gnostic v0.5.1 h1:A8Yhf6EtqTv9RMsU6MQTyrtV1TjWlR6xU9BsZIwuTCM=
224224
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
225+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
226+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
225227
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
226228
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
227229
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
@@ -398,6 +400,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
398400
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
399401
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
400402
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
403+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
404+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
401405
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
402406
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
403407
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -593,6 +597,8 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
593597
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
594598
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
595599
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
600+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
601+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
596602
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
597603
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
598604
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

npm/http/api/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package api
2+
3+
const (
4+
DefaultListeningIP = "0.0.0.0"
5+
DefaultHttpPort = "10091"
6+
NodeMetricsPath = "/node-metrics"
7+
ClusterMetricsPath = "/cluster-metrics"
8+
NPMMgrPath = "/npm/v1/debug/manager"
9+
)
10+
11+
type DescribeIPSetRequest struct {
12+
ipsetname string `json:"name"`
13+
}
14+
15+
type DescribeIPSetResponse struct {
16+
}

npm/http/client/client.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"github.com/Azure/azure-container-networking/npm/http/api"
9+
10+
"github.com/Azure/azure-container-networking/npm"
11+
)
12+
13+
type NPMHttpClient struct {
14+
endpoint string
15+
client *http.Client
16+
}
17+
18+
func NewNPMHttpClient(endpoint string) *NPMHttpClient {
19+
return &NPMHttpClient{
20+
endpoint: endpoint,
21+
client: &http.Client{
22+
Timeout: time.Second * 10,
23+
},
24+
}
25+
}
26+
27+
func (n *NPMHttpClient) GetNpmMgr() (*npm.NetworkPolicyManager, error) {
28+
url := n.endpoint + api.NPMMgrPath
29+
req, err := http.NewRequest(http.MethodGet, url, nil)
30+
if err != nil {
31+
return nil, err
32+
}
33+
req.Header.Set("Content-Type", "application/json")
34+
res, err := n.client.Do(req)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
var ns npm.NetworkPolicyManager
40+
err = json.NewDecoder(res.Body).Decode(&ns)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return &ns, nil
46+
}

npm/http/server/server.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package server
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/http/pprof"
8+
_ "net/http/pprof"
9+
10+
"github.com/Azure/azure-container-networking/log"
11+
12+
"github.com/Azure/azure-container-networking/npm/http/api"
13+
"github.com/Azure/azure-container-networking/npm/metrics"
14+
15+
"github.com/Azure/azure-container-networking/npm"
16+
"github.com/gorilla/mux"
17+
)
18+
19+
var (
20+
DefaultHTTPListeningAddress = fmt.Sprintf("%s:%s", api.DefaultListeningIP, api.DefaultHttpPort)
21+
)
22+
23+
type NPMRestServer struct {
24+
listeningAddress string
25+
server *http.Server
26+
router *mux.Router
27+
}
28+
29+
func (n *NPMRestServer) NPMRestServerListenAndServe(npMgr *npm.NetworkPolicyManager) {
30+
n.router = mux.NewRouter()
31+
32+
//prometheus handlers
33+
n.router.Handle(api.NodeMetricsPath, metrics.GetHandler(true))
34+
n.router.Handle(api.ClusterMetricsPath, metrics.GetHandler(false))
35+
36+
// ACN CLI debug handlerss
37+
n.router.Handle(api.NPMMgrPath, n.GetNpmMgr(npMgr)).Methods(http.MethodGet)
38+
39+
n.router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
40+
n.router.HandleFunc("/debug/pprof/", pprof.Index)
41+
n.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
42+
n.router.HandleFunc("/debug/pprof/profile", pprof.Profile)
43+
n.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
44+
n.router.HandleFunc("/debug/pprof/trace", pprof.Trace)
45+
46+
// use default listening address if none is specified
47+
if n.listeningAddress == "" {
48+
n.listeningAddress = DefaultHTTPListeningAddress
49+
}
50+
51+
srv := &http.Server{
52+
Handler: n.router,
53+
Addr: n.listeningAddress,
54+
}
55+
56+
log.Logf("Starting NPM HTTP API on %s... ", n.listeningAddress)
57+
log.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe())
58+
}
59+
60+
func NewNpmRestServer(listeningAddress string) *NPMRestServer {
61+
return &NPMRestServer{
62+
listeningAddress: listeningAddress,
63+
}
64+
}
65+
66+
func (n *NPMRestServer) GetNpmMgr(npMgr *npm.NetworkPolicyManager) http.Handler {
67+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68+
npMgr.Lock()
69+
err := json.NewEncoder(w).Encode(npMgr)
70+
if err != nil {
71+
http.Error(w, err.Error(), 500)
72+
return
73+
}
74+
npMgr.Unlock()
75+
})
76+
}

npm/http/server/server_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package server
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/Azure/azure-container-networking/npm/http/api"
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/Azure/azure-container-networking/npm"
13+
)
14+
15+
func TestGetNpmMgrHandler(t *testing.T) {
16+
assert := assert.New(t)
17+
npMgr := &npm.NetworkPolicyManager{
18+
NsMap: map[string]*npm.Namespace{
19+
"test": &npm.Namespace{
20+
PodMap: map[string]*npm.NpmPod{
21+
"": &npm.NpmPod{
22+
Name: "testpod",
23+
},
24+
},
25+
},
26+
},
27+
}
28+
n := NewNpmRestServer("")
29+
handler := n.GetNpmMgr(npMgr)
30+
31+
req, err := http.NewRequest(http.MethodGet, api.NPMMgrPath, nil)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
rr := httptest.NewRecorder()
37+
handler.ServeHTTP(rr, req)
38+
39+
if status := rr.Code; status != http.StatusOK {
40+
t.Errorf("handler returned wrong status code: got %v want %v",
41+
status, http.StatusOK)
42+
}
43+
44+
var ns npm.NetworkPolicyManager
45+
err = json.NewDecoder(rr.Body).Decode(&ns)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
assert.Exactly(&ns, npMgr)
51+
}

0 commit comments

Comments
 (0)