Add methods for listing and fetching container stats

This uses the previously unusued lib/stats.go code to return data
about container stats to the CRI API. Helpers have been built around
filtering based on the OCI API, and CPU stat reporting has been fixed.

No data on filesystem layer usage is returned at this time.

Fixes one-half of #1248

Signed-off-by: Yann Ramin <atrus@stackworks.net>
This commit is contained in:
Yann Ramin 2018-01-07 14:16:46 -08:00
parent ef57cf2810
commit 14c1c70407
4 changed files with 118 additions and 41 deletions

View file

@ -1,9 +1,10 @@
package server
import (
"fmt"
"time"
"github.com/kubernetes-incubator/cri-o/lib"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
@ -15,5 +16,37 @@ func (s *Server) ListContainerStats(ctx context.Context, req *pb.ListContainerSt
recordOperation(operation, time.Now())
recordError(operation, err)
}()
return nil, fmt.Errorf("not implemented")
ctrList, err := s.ContainerServer.ListContainers()
if err != nil {
return nil, err
}
filter := req.GetFilter()
if filter != nil {
cFilter := &pb.ContainerFilter{
Id: req.Filter.Id,
PodSandboxId: req.Filter.PodSandboxId,
LabelSelector: req.Filter.LabelSelector,
}
ctrList, err = s.filterContainerList(cFilter)
if err != nil {
return nil, err
}
}
var allStats []*pb.ContainerStats
for _, container := range ctrList {
stats, err := s.GetContainerStats(container, &lib.ContainerStats{})
if err != nil {
logrus.Warn("unable to get stats for container %s", container.ID())
continue
}
response := buildContainerStats(stats, container)
allStats = append(allStats, response)
}
return &pb.ListContainerStatsResponse{
Stats: allStats,
}, nil
}