docker_stats.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dockershim
  14. import (
  15. "context"
  16. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  17. )
  18. // ContainerStats returns stats for a container stats request based on container id.
  19. func (ds *dockerService) ContainerStats(_ context.Context, r *runtimeapi.ContainerStatsRequest) (*runtimeapi.ContainerStatsResponse, error) {
  20. stats, err := ds.getContainerStats(r.ContainerId)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &runtimeapi.ContainerStatsResponse{Stats: stats}, nil
  25. }
  26. // ListContainerStats returns stats for a list container stats request based on a filter.
  27. func (ds *dockerService) ListContainerStats(ctx context.Context, r *runtimeapi.ListContainerStatsRequest) (*runtimeapi.ListContainerStatsResponse, error) {
  28. containerStatsFilter := r.GetFilter()
  29. filter := &runtimeapi.ContainerFilter{}
  30. if containerStatsFilter != nil {
  31. filter.Id = containerStatsFilter.Id
  32. filter.PodSandboxId = containerStatsFilter.PodSandboxId
  33. filter.LabelSelector = containerStatsFilter.LabelSelector
  34. }
  35. listResp, err := ds.ListContainers(ctx, &runtimeapi.ListContainersRequest{Filter: filter})
  36. if err != nil {
  37. return nil, err
  38. }
  39. var stats []*runtimeapi.ContainerStats
  40. for _, container := range listResp.Containers {
  41. containerStats, err := ds.getContainerStats(container.Id)
  42. if err != nil {
  43. return nil, err
  44. }
  45. stats = append(stats, containerStats)
  46. }
  47. return &runtimeapi.ListContainerStatsResponse{Stats: stats}, nil
  48. }