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

@ -4,10 +4,32 @@ import (
"fmt"
"time"
"github.com/kubernetes-incubator/cri-o/lib"
"github.com/kubernetes-incubator/cri-o/oci"
"golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
func buildContainerStats(stats *lib.ContainerStats, container *oci.Container) *pb.ContainerStats {
return &pb.ContainerStats{
Attributes: &pb.ContainerAttributes{
Id: container.ID(),
Metadata: container.Metadata(),
Labels: container.Labels(),
Annotations: container.Annotations(),
},
Cpu: &pb.CpuUsage{
Timestamp: stats.SystemNano,
UsageCoreNanoSeconds: &pb.UInt64Value{Value: stats.CPUNano},
},
Memory: &pb.MemoryUsage{
Timestamp: stats.SystemNano,
WorkingSetBytes: &pb.UInt64Value{Value: stats.MemUsage},
},
WritableLayer: nil,
}
}
// ContainerStats returns stats of the container. If the container does not
// exist, the call returns an error.
func (s *Server) ContainerStats(ctx context.Context, req *pb.ContainerStatsRequest) (resp *pb.ContainerStatsResponse, err error) {
@ -16,5 +38,16 @@ func (s *Server) ContainerStats(ctx context.Context, req *pb.ContainerStatsReque
recordOperation(operation, time.Now())
recordError(operation, err)
}()
return nil, fmt.Errorf("not implemented")
container := s.GetContainer(req.ContainerId)
if container == nil {
return nil, fmt.Errorf("invalid container")
}
stats, err := s.GetContainerStats(container, &lib.ContainerStats{})
if err != nil {
return nil, err
}
return &pb.ContainerStatsResponse{Stats: buildContainerStats(stats, container)}, nil
}