Merge pull request #265 from vmarmol/client
Adding Subcontainers to cAdvisor client.
This commit is contained in:
commit
22a55636f4
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/cadvisor/info"
|
"github.com/google/cadvisor/info"
|
||||||
@ -32,18 +33,14 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new client with the specified base URL.
|
// NewClient returns a new client with the specified base URL.
|
||||||
// TODO(cAdvisor): Currently the error result is always nil.
|
func NewClient(url string) (*Client, error) {
|
||||||
func NewClient(URL string) (*Client, error) {
|
if !strings.HasSuffix(url, "/") {
|
||||||
return &Client{
|
url += "/"
|
||||||
baseUrl: strings.Join([]string{
|
}
|
||||||
URL,
|
|
||||||
"api/v1.0",
|
|
||||||
}, "/"),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Client) machineInfoUrl() string {
|
return &Client{
|
||||||
return strings.Join([]string{self.baseUrl, "machine"}, "/")
|
baseUrl: fmt.Sprintf("%sapi/v1.1/", url),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MachineInfo returns the JSON machine information for this client.
|
// MachineInfo returns the JSON machine information for this client.
|
||||||
@ -59,11 +56,40 @@ func (self *Client) MachineInfo() (minfo *info.MachineInfo, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Client) containerInfoUrl(name string) string {
|
// ContainerInfo returns the JSON container information for the specified
|
||||||
if name[0] == '/' {
|
// container and request.
|
||||||
name = name[1:]
|
func (self *Client) ContainerInfo(name string, query *info.ContainerInfoRequest) (cinfo *info.ContainerInfo, err error) {
|
||||||
|
u := self.containerInfoUrl(name)
|
||||||
|
ret := new(info.ContainerInfo)
|
||||||
|
if err = self.httpGetJsonData(ret, query, u, fmt.Sprintf("container info for %q", name)); err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return strings.Join([]string{self.baseUrl, "containers", name}, "/")
|
cinfo = ret
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the information about all subcontainers (recursive) of the specified container (including itself).
|
||||||
|
func (self *Client) SubcontainersInfo(name string, query *info.ContainerInfoRequest) ([]info.ContainerInfo, error) {
|
||||||
|
var response []info.ContainerInfo
|
||||||
|
url := self.subcontainersInfoUrl(name)
|
||||||
|
err := self.httpGetJsonData(&response, query, url, fmt.Sprintf("subcontainers container info for %q", name))
|
||||||
|
if err != nil {
|
||||||
|
return []info.ContainerInfo{}, err
|
||||||
|
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Client) machineInfoUrl() string {
|
||||||
|
return self.baseUrl + path.Join("machine")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Client) containerInfoUrl(name string) string {
|
||||||
|
return self.baseUrl + path.Join("containers", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Client) subcontainersInfoUrl(name string) string {
|
||||||
|
return self.baseUrl + path.Join("subcontainers", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName string) error {
|
func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName string) error {
|
||||||
@ -95,16 +121,3 @@ func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName st
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainerInfo returns the JSON container information for the specified
|
|
||||||
// container and request.
|
|
||||||
func (self *Client) ContainerInfo(name string,
|
|
||||||
query *info.ContainerInfoRequest) (cinfo *info.ContainerInfo, err error) {
|
|
||||||
u := self.containerInfoUrl(name)
|
|
||||||
ret := new(info.ContainerInfo)
|
|
||||||
if err = self.httpGetJsonData(ret, query, u, fmt.Sprintf("container info for %v", name)); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cinfo = ret
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -56,7 +57,7 @@ func cadvisorTestClient(path string, expectedPostObj, expectedPostObjEmpty, repl
|
|||||||
}
|
}
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
encoder.Encode(replyObj)
|
encoder.Encode(replyObj)
|
||||||
} else if r.URL.Path == "/api/v1.0/machine" {
|
} else if r.URL.Path == "/api/v1.1/machine" {
|
||||||
fmt.Fprint(w, `{"num_cores":8,"memory_capacity":31625871360}`)
|
fmt.Fprint(w, `{"num_cores":8,"memory_capacity":31625871360}`)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
@ -78,7 +79,7 @@ func TestGetMachineinfo(t *testing.T) {
|
|||||||
NumCores: 8,
|
NumCores: 8,
|
||||||
MemoryCapacity: 31625871360,
|
MemoryCapacity: 31625871360,
|
||||||
}
|
}
|
||||||
client, server, err := cadvisorTestClient("/api/v1.0/machine", nil, nil, minfo, t)
|
client, server, err := cadvisorTestClient("/api/v1.1/machine", nil, nil, minfo, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get a client %v", err)
|
t.Fatalf("unable to get a client %v", err)
|
||||||
}
|
}
|
||||||
@ -100,7 +101,7 @@ func TestGetContainerInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
containerName := "/some/container"
|
containerName := "/some/container"
|
||||||
cinfo := itest.GenerateRandomContainerInfo(containerName, 4, query, 1*time.Second)
|
cinfo := itest.GenerateRandomContainerInfo(containerName, 4, query, 1*time.Second)
|
||||||
client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.0/containers%v", containerName), query, &info.ContainerInfoRequest{}, cinfo, t)
|
client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.1/containers%v", containerName), query, &info.ContainerInfoRequest{}, cinfo, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get a client %v", err)
|
t.Fatalf("unable to get a client %v", err)
|
||||||
}
|
}
|
||||||
@ -114,3 +115,40 @@ func TestGetContainerInfo(t *testing.T) {
|
|||||||
t.Error("received unexpected ContainerInfo")
|
t.Error("received unexpected ContainerInfo")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetSubcontainersInfo(t *testing.T) {
|
||||||
|
query := &info.ContainerInfoRequest{
|
||||||
|
NumStats: 3,
|
||||||
|
}
|
||||||
|
containerName := "/some/container"
|
||||||
|
cinfo := itest.GenerateRandomContainerInfo(containerName, 4, query, 1*time.Second)
|
||||||
|
cinfo1 := itest.GenerateRandomContainerInfo(path.Join(containerName, "sub1"), 4, query, 1*time.Second)
|
||||||
|
cinfo2 := itest.GenerateRandomContainerInfo(path.Join(containerName, "sub2"), 4, query, 1*time.Second)
|
||||||
|
response := []info.ContainerInfo{
|
||||||
|
*cinfo,
|
||||||
|
*cinfo1,
|
||||||
|
*cinfo2,
|
||||||
|
}
|
||||||
|
client, server, err := cadvisorTestClient(fmt.Sprintf("/api/v1.1/subcontainers%v", containerName), query, &info.ContainerInfoRequest{}, response, t)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to get a client %v", err)
|
||||||
|
}
|
||||||
|
defer server.Close()
|
||||||
|
returned, err := client.SubcontainersInfo(containerName, query)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(returned) != 3 {
|
||||||
|
t.Errorf("unexpected number of results: got %d, expected 3", len(returned))
|
||||||
|
}
|
||||||
|
if !returned[0].Eq(cinfo) {
|
||||||
|
t.Error("received unexpected ContainerInfo")
|
||||||
|
}
|
||||||
|
if !returned[1].Eq(cinfo1) {
|
||||||
|
t.Error("received unexpected ContainerInfo")
|
||||||
|
}
|
||||||
|
if !returned[2].Eq(cinfo2) {
|
||||||
|
t.Error("received unexpected ContainerInfo")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user