Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/docker/libnetwork v0.5.6
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.8.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.8 // indirect
Expand All @@ -26,6 +27,7 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.7.0
go.opencensus.io v0.22.2 // indirect
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 // indirect
golang.org/x/net v0.0.0-20191112182307-2180aed22343 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyyc
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/googleapis/gnostic v0.5.1 h1:A8Yhf6EtqTv9RMsU6MQTyrtV1TjWlR6xU9BsZIwuTCM=
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
Expand Down Expand Up @@ -398,6 +400,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -593,6 +597,8 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
16 changes: 16 additions & 0 deletions npm/http/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package api

const (
DefaultListeningIP = "0.0.0.0"
DefaultHttpPort = "10091"
NodeMetricsPath = "/node-metrics"
ClusterMetricsPath = "/cluster-metrics"
NPMMgrPath = "/npm/v1/debug/manager"
)

type DescribeIPSetRequest struct {
ipsetname string `json:"name"`
}

type DescribeIPSetResponse struct {
}
46 changes: 46 additions & 0 deletions npm/http/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

import (
"encoding/json"
"net/http"
"time"

"github.com/Azure/azure-container-networking/npm/http/api"

"github.com/Azure/azure-container-networking/npm"
)

type NPMHttpClient struct {
endpoint string
client *http.Client
}

func NewNPMHttpClient(endpoint string) *NPMHttpClient {
return &NPMHttpClient{
endpoint: endpoint,
client: &http.Client{
Timeout: time.Second * 10,
},
}
}

func (n *NPMHttpClient) GetNpmMgr() (*npm.NetworkPolicyManager, error) {
url := n.endpoint + api.NPMMgrPath
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := n.client.Do(req)
if err != nil {
return nil, err
}

var ns npm.NetworkPolicyManager
err = json.NewDecoder(res.Body).Decode(&ns)
if err != nil {
return nil, err
}

return &ns, nil
}
76 changes: 76 additions & 0 deletions npm/http/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package server

import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
_ "net/http/pprof"

"github.com/Azure/azure-container-networking/log"

"github.com/Azure/azure-container-networking/npm/http/api"
"github.com/Azure/azure-container-networking/npm/metrics"

"github.com/Azure/azure-container-networking/npm"
"github.com/gorilla/mux"
)

var (
DefaultHTTPListeningAddress = fmt.Sprintf("%s:%s", api.DefaultListeningIP, api.DefaultHttpPort)
)

type NPMRestServer struct {
listeningAddress string
server *http.Server
router *mux.Router
}

func (n *NPMRestServer) NPMRestServerListenAndServe(npMgr *npm.NetworkPolicyManager) {
n.router = mux.NewRouter()

//prometheus handlers
n.router.Handle(api.NodeMetricsPath, metrics.GetHandler(true))
n.router.Handle(api.ClusterMetricsPath, metrics.GetHandler(false))

// ACN CLI debug handlerss
n.router.Handle(api.NPMMgrPath, n.GetNpmMgr(npMgr)).Methods(http.MethodGet)

n.router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
n.router.HandleFunc("/debug/pprof/", pprof.Index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move these to APIs

n.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
n.router.HandleFunc("/debug/pprof/profile", pprof.Profile)
n.router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
n.router.HandleFunc("/debug/pprof/trace", pprof.Trace)

// use default listening address if none is specified
if n.listeningAddress == "" {
n.listeningAddress = DefaultHTTPListeningAddress
}

srv := &http.Server{
Handler: n.router,
Addr: n.listeningAddress,
}

log.Logf("Starting NPM HTTP API on %s... ", n.listeningAddress)
log.Errorf("Failed to start NPM HTTP Server with error: %+v", srv.ListenAndServe())
}

func NewNpmRestServer(listeningAddress string) *NPMRestServer {
return &NPMRestServer{
listeningAddress: listeningAddress,
}
}

func (n *NPMRestServer) GetNpmMgr(npMgr *npm.NetworkPolicyManager) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
npMgr.Lock()
err := json.NewEncoder(w).Encode(npMgr)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
npMgr.Unlock()
})
}
51 changes: 51 additions & 0 deletions npm/http/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package server

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/Azure/azure-container-networking/npm/http/api"
"github.com/stretchr/testify/assert"

"github.com/Azure/azure-container-networking/npm"
)

func TestGetNpmMgrHandler(t *testing.T) {
assert := assert.New(t)
npMgr := &npm.NetworkPolicyManager{
NsMap: map[string]*npm.Namespace{
"test": &npm.Namespace{
PodMap: map[string]*npm.NpmPod{
"": &npm.NpmPod{
Name: "testpod",
},
},
},
},
}
n := NewNpmRestServer("")
handler := n.GetNpmMgr(npMgr)

req, err := http.NewRequest(http.MethodGet, api.NPMMgrPath, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

var ns npm.NetworkPolicyManager
err = json.NewDecoder(rr.Body).Decode(&ns)
if err != nil {
t.Fatal(err)
}

assert.Exactly(&ns, npMgr)
}
Loading