Merge pull request #586 from rjnagal/docker

Add handling of docker container to 2.0 stats API.
This commit is contained in:
Victor Marmol 2015-03-13 12:03:50 -07:00
commit 7d27bf2f6f

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,
} }
cont, err := m.GetContainerInfo(name, &query) switch sr.IdType {
if err != nil { case typeName:
return fmt.Errorf("failed to get container %q: %v", name, err) cont, err := m.GetContainerInfo(name, &query)
if err != nil {
return fmt.Errorf("failed to get container %q: %v", name, err)
}
contStats := convertStats(cont)
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)
} }
contStats := convertStats(cont)
return writeResult(contStats, w)
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,15 +447,22 @@ 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 {
return sr, fmt.Errorf("unknown 'type' %q for container name %q", idType, id) if !supportedTypes[idType] {
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 {