2017-08-04 11:13:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
2017-08-04 11:13:19 +00:00
|
|
|
|
2018-01-07 22:16:46 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/lib"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-04 11:13:19 +00:00
|
|
|
"golang.org/x/net/context"
|
2018-02-12 20:13:07 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2017-08-04 11:13:19 +00:00
|
|
|
)
|
|
|
|
|
2018-01-07 22:16:46 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:13:19 +00:00
|
|
|
// ContainerStats returns stats of the container. If the container does not
|
|
|
|
// exist, the call returns an error.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) ContainerStats(ctx context.Context, req *pb.ContainerStatsRequest) (resp *pb.ContainerStatsResponse, err error) {
|
|
|
|
const operation = "container_stats"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
2018-01-07 22:16:46 +00:00
|
|
|
|
|
|
|
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
|
2017-08-04 11:13:19 +00:00
|
|
|
}
|