disk_manager.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2015 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 kubelet
  14. import (
  15. "fmt"
  16. "sync"
  17. "time"
  18. "github.com/golang/glog"
  19. cadvisorapi "github.com/google/cadvisor/info/v2"
  20. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  21. )
  22. // Manages policy for diskspace management for disks holding docker images and root fs.
  23. // mb is used to easily convert an int to an mb
  24. const mb = 1024 * 1024
  25. // Implementation is thread-safe.
  26. type diskSpaceManager interface {
  27. // Checks the available disk space
  28. IsRootDiskSpaceAvailable() (bool, error)
  29. IsRuntimeDiskSpaceAvailable() (bool, error)
  30. }
  31. type DiskSpacePolicy struct {
  32. // free disk space threshold for filesystem holding docker images.
  33. DockerFreeDiskMB int
  34. // free disk space threshold for root filesystem. Host volumes are created on root fs.
  35. RootFreeDiskMB int
  36. }
  37. type fsInfo struct {
  38. Usage int64
  39. Capacity int64
  40. Available int64
  41. Timestamp time.Time
  42. }
  43. type realDiskSpaceManager struct {
  44. cadvisor cadvisor.Interface
  45. cachedInfo map[string]fsInfo // cache of filesystem info.
  46. lock sync.Mutex // protecting cachedInfo.
  47. policy DiskSpacePolicy // thresholds. Set at creation time.
  48. }
  49. func (dm *realDiskSpaceManager) getFsInfo(fsType string, f func() (cadvisorapi.FsInfo, error)) (fsInfo, error) {
  50. dm.lock.Lock()
  51. defer dm.lock.Unlock()
  52. fsi := fsInfo{}
  53. if info, ok := dm.cachedInfo[fsType]; ok {
  54. timeLimit := time.Now().Add(-2 * time.Second)
  55. if info.Timestamp.After(timeLimit) {
  56. fsi = info
  57. }
  58. }
  59. if fsi.Timestamp.IsZero() {
  60. fs, err := f()
  61. if err != nil {
  62. return fsInfo{}, err
  63. }
  64. fsi.Timestamp = time.Now()
  65. fsi.Usage = int64(fs.Usage)
  66. fsi.Capacity = int64(fs.Capacity)
  67. fsi.Available = int64(fs.Available)
  68. dm.cachedInfo[fsType] = fsi
  69. }
  70. return fsi, nil
  71. }
  72. func (dm *realDiskSpaceManager) IsRuntimeDiskSpaceAvailable() (bool, error) {
  73. return dm.isSpaceAvailable("runtime", dm.policy.DockerFreeDiskMB, dm.cadvisor.ImagesFsInfo)
  74. }
  75. func (dm *realDiskSpaceManager) IsRootDiskSpaceAvailable() (bool, error) {
  76. return dm.isSpaceAvailable("root", dm.policy.RootFreeDiskMB, dm.cadvisor.RootFsInfo)
  77. }
  78. func (dm *realDiskSpaceManager) isSpaceAvailable(fsType string, threshold int, f func() (cadvisorapi.FsInfo, error)) (bool, error) {
  79. fsInfo, err := dm.getFsInfo(fsType, f)
  80. if err != nil {
  81. return true, fmt.Errorf("failed to get fs info for %q: %v", fsType, err)
  82. }
  83. if fsInfo.Capacity == 0 {
  84. return true, fmt.Errorf("could not determine capacity for %q fs. Info: %+v", fsType, fsInfo)
  85. }
  86. if fsInfo.Available < 0 {
  87. return true, fmt.Errorf("wrong available space for %q: %+v", fsType, fsInfo)
  88. }
  89. if fsInfo.Available < int64(threshold)*mb {
  90. glog.Infof("Running out of space on disk for %q: available %d MB, threshold %d MB", fsType, fsInfo.Available/mb, threshold)
  91. return false, nil
  92. }
  93. return true, nil
  94. }
  95. func validatePolicy(policy DiskSpacePolicy) error {
  96. if policy.DockerFreeDiskMB < 0 {
  97. return fmt.Errorf("free disk space should be non-negative. Invalid value %d for docker disk space threshold.", policy.DockerFreeDiskMB)
  98. }
  99. if policy.RootFreeDiskMB < 0 {
  100. return fmt.Errorf("free disk space should be non-negative. Invalid value %d for root disk space threshold.", policy.RootFreeDiskMB)
  101. }
  102. return nil
  103. }
  104. func newDiskSpaceManager(cadvisorInterface cadvisor.Interface, policy DiskSpacePolicy) (diskSpaceManager, error) {
  105. // validate policy
  106. err := validatePolicy(policy)
  107. if err != nil {
  108. return nil, err
  109. }
  110. dm := &realDiskSpaceManager{
  111. cadvisor: cadvisorInterface,
  112. policy: policy,
  113. cachedInfo: map[string]fsInfo{},
  114. }
  115. return dm, nil
  116. }