docker_image_linux.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package dockershim
  15. import (
  16. "context"
  17. "os"
  18. "path/filepath"
  19. "time"
  20. "k8s.io/klog"
  21. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  22. )
  23. // ImageFsInfo returns information of the filesystem that is used to store images.
  24. func (ds *dockerService) ImageFsInfo(_ context.Context, _ *runtimeapi.ImageFsInfoRequest) (*runtimeapi.ImageFsInfoResponse, error) {
  25. info, err := ds.client.Info()
  26. if err != nil {
  27. klog.Errorf("Failed to get docker info: %v", err)
  28. return nil, err
  29. }
  30. bytes, inodes, err := dirSize(filepath.Join(info.DockerRootDir, "image"))
  31. if err != nil {
  32. return nil, err
  33. }
  34. return &runtimeapi.ImageFsInfoResponse{
  35. ImageFilesystems: []*runtimeapi.FilesystemUsage{
  36. {
  37. Timestamp: time.Now().Unix(),
  38. FsId: &runtimeapi.FilesystemIdentifier{
  39. Mountpoint: info.DockerRootDir,
  40. },
  41. UsedBytes: &runtimeapi.UInt64Value{
  42. Value: uint64(bytes),
  43. },
  44. InodesUsed: &runtimeapi.UInt64Value{
  45. Value: uint64(inodes),
  46. },
  47. },
  48. },
  49. }, nil
  50. }
  51. func dirSize(path string) (int64, int64, error) {
  52. bytes := int64(0)
  53. inodes := int64(0)
  54. err := filepath.Walk(path, func(dir string, info os.FileInfo, err error) error {
  55. if err != nil {
  56. return err
  57. }
  58. inodes += 1
  59. if !info.IsDir() {
  60. bytes += info.Size()
  61. }
  62. return nil
  63. })
  64. return bytes, inodes, err
  65. }