Add handling of docker container to 2.0 stats API.

Next: Apply similar handling to spec and summary endpoints.
This commit is contained in:
Rohit Jnagal 2015-03-13 18:15:32 +00:00
parent 14d56d3eda
commit 7a6f5ddaaf

View File

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/google/cadvisor/events" "github.com/google/cadvisor/events"
@ -36,6 +37,8 @@ const (
specApi = "spec" specApi = "spec"
eventsApi = "events" eventsApi = "events"
storageApi = "storage" storageApi = "storage"
typeName = "name"
typeDocker = "docker"
) )
// Interface for a cAdvisor API version // Interface for a cAdvisor API version
@ -326,12 +329,38 @@ func (self *version2_0) HandleRequest(requestType string, request []string, m ma
query := info.ContainerInfoRequest{ query := info.ContainerInfoRequest{
NumStats: sr.Count, NumStats: sr.Count,
} }
switch sr.IdType {
case typeName:
cont, err := m.GetContainerInfo(name, &query) cont, err := m.GetContainerInfo(name, &query)
if err != nil { if err != nil {
return fmt.Errorf("failed to get container %q: %v", name, err) return fmt.Errorf("failed to get container %q: %v", name, err)
} }
contStats := convertStats(cont) contStats := convertStats(cont)
return writeResult(contStats, w) return writeResult(contStats, w)
case typeDocker:
contStats := make(map[string][]v2.ContainerStats, 0)
if name == "/" {
// special case: get all docker containers.
// TODO(rjnagal): require recursive=true to be set?
containers, err := m.AllDockerContainers(&query)
if err != nil {
return fmt.Errorf("failed to get all docker containers: %v", err)
}
for name, cont := range containers {
contStats[name] = convertStats(&cont)
}
} else {
name = strings.TrimPrefix(name, "/")
cont, err := m.DockerContainer(name, &query)
if err != nil {
return fmt.Errorf("failed to get Docker container %q with error: %v", name, err)
}
contStats[cont.Name] = convertStats(&cont)
}
return writeResult(contStats, w)
default:
return fmt.Errorf("unknown id type %q for container name %q", sr.IdType, name)
}
case specApi: case specApi:
containerName := getContainerName(request) containerName := getContainerName(request)
glog.V(2).Infof("Api - Spec(%v)", containerName) glog.V(2).Infof("Api - Spec(%v)", containerName)
@ -418,16 +447,23 @@ func convertStats(cont *info.ContainerInfo) []v2.ContainerStats {
} }
func getStatsRequest(id string, r *http.Request) (v2.StatsRequest, error) { func getStatsRequest(id string, r *http.Request) (v2.StatsRequest, error) {
supportedTypes := map[string]bool{
typeName: true,
typeDocker: true,
}
// fill in the defaults. // fill in the defaults.
sr := v2.StatsRequest{ sr := v2.StatsRequest{
IdType: "name", IdType: typeName,
Count: 64, Count: 64,
Recursive: false, Recursive: false,
} }
idType := r.URL.Query().Get("type") idType := r.URL.Query().Get("type")
if len(idType) != 0 && idType != "name" { if len(idType) != 0 {
if !supportedTypes[idType] {
return sr, fmt.Errorf("unknown 'type' %q for container name %q", idType, id) return sr, fmt.Errorf("unknown 'type' %q for container name %q", idType, id)
} }
sr.IdType = idType
}
count := r.URL.Query().Get("count") count := r.URL.Query().Get("count")
if len(count) != 0 { if len(count) != 0 {
n, err := strconv.ParseUint(count, 10, 32) n, err := strconv.ParseUint(count, 10, 32)