cadvisor_linux.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // +build linux
  2. /*
  3. Copyright 2015 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 cadvisor
  15. import (
  16. "flag"
  17. "fmt"
  18. "net/http"
  19. "os"
  20. "path"
  21. "time"
  22. // Register supported container handlers.
  23. _ "github.com/google/cadvisor/container/containerd/install"
  24. _ "github.com/google/cadvisor/container/crio/install"
  25. _ "github.com/google/cadvisor/container/docker/install"
  26. _ "github.com/google/cadvisor/container/systemd/install"
  27. // Register cloud info providers.
  28. // TODO(#76660): Remove this once the cAdvisor endpoints are removed.
  29. _ "github.com/google/cadvisor/utils/cloudinfo/aws"
  30. _ "github.com/google/cadvisor/utils/cloudinfo/azure"
  31. _ "github.com/google/cadvisor/utils/cloudinfo/gce"
  32. "github.com/google/cadvisor/cache/memory"
  33. cadvisormetrics "github.com/google/cadvisor/container"
  34. "github.com/google/cadvisor/events"
  35. cadvisorapi "github.com/google/cadvisor/info/v1"
  36. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  37. "github.com/google/cadvisor/manager"
  38. "github.com/google/cadvisor/utils/sysfs"
  39. "k8s.io/klog"
  40. )
  41. type cadvisorClient struct {
  42. imageFsInfoProvider ImageFsInfoProvider
  43. rootPath string
  44. manager.Manager
  45. }
  46. var _ Interface = new(cadvisorClient)
  47. // TODO(vmarmol): Make configurable.
  48. // The amount of time for which to keep stats in memory.
  49. const statsCacheDuration = 2 * time.Minute
  50. const maxHousekeepingInterval = 15 * time.Second
  51. const defaultHousekeepingInterval = 10 * time.Second
  52. const allowDynamicHousekeeping = true
  53. func init() {
  54. // Override cAdvisor flag defaults.
  55. flagOverrides := map[string]string{
  56. // Override the default cAdvisor housekeeping interval.
  57. "housekeeping_interval": defaultHousekeepingInterval.String(),
  58. // Disable event storage by default.
  59. "event_storage_event_limit": "default=0",
  60. "event_storage_age_limit": "default=0",
  61. }
  62. for name, defaultValue := range flagOverrides {
  63. if f := flag.Lookup(name); f != nil {
  64. f.DefValue = defaultValue
  65. f.Value.Set(defaultValue)
  66. } else {
  67. klog.Errorf("Expected cAdvisor flag %q not found", name)
  68. }
  69. }
  70. }
  71. // New creates a new cAdvisor Interface for linux systems.
  72. func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupRoots []string, usingLegacyStats bool) (Interface, error) {
  73. sysFs := sysfs.NewRealSysFs()
  74. includedMetrics := cadvisormetrics.MetricSet{
  75. cadvisormetrics.CpuUsageMetrics: struct{}{},
  76. cadvisormetrics.MemoryUsageMetrics: struct{}{},
  77. cadvisormetrics.CpuLoadMetrics: struct{}{},
  78. cadvisormetrics.DiskIOMetrics: struct{}{},
  79. cadvisormetrics.NetworkUsageMetrics: struct{}{},
  80. cadvisormetrics.AcceleratorUsageMetrics: struct{}{},
  81. cadvisormetrics.AppMetrics: struct{}{},
  82. cadvisormetrics.ProcessMetrics: struct{}{},
  83. }
  84. if usingLegacyStats {
  85. includedMetrics[cadvisormetrics.DiskUsageMetrics] = struct{}{}
  86. }
  87. // Create the cAdvisor container manager.
  88. m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, includedMetrics, http.DefaultClient, cgroupRoots)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if _, err := os.Stat(rootPath); err != nil {
  93. if os.IsNotExist(err) {
  94. if err := os.MkdirAll(path.Clean(rootPath), 0750); err != nil {
  95. return nil, fmt.Errorf("error creating root directory %q: %v", rootPath, err)
  96. }
  97. } else {
  98. return nil, fmt.Errorf("failed to Stat %q: %v", rootPath, err)
  99. }
  100. }
  101. return &cadvisorClient{
  102. imageFsInfoProvider: imageFsInfoProvider,
  103. rootPath: rootPath,
  104. Manager: m,
  105. }, nil
  106. }
  107. func (cc *cadvisorClient) Start() error {
  108. return cc.Manager.Start()
  109. }
  110. func (cc *cadvisorClient) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  111. return cc.GetContainerInfo(name, req)
  112. }
  113. func (cc *cadvisorClient) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
  114. return cc.GetContainerInfoV2(name, options)
  115. }
  116. func (cc *cadvisorClient) VersionInfo() (*cadvisorapi.VersionInfo, error) {
  117. return cc.GetVersionInfo()
  118. }
  119. func (cc *cadvisorClient) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
  120. infos, err := cc.SubcontainersInfo(name, req)
  121. if err != nil && len(infos) == 0 {
  122. return nil, err
  123. }
  124. result := make(map[string]*cadvisorapi.ContainerInfo, len(infos))
  125. for _, info := range infos {
  126. result[info.Name] = info
  127. }
  128. return result, err
  129. }
  130. func (cc *cadvisorClient) MachineInfo() (*cadvisorapi.MachineInfo, error) {
  131. return cc.GetMachineInfo()
  132. }
  133. func (cc *cadvisorClient) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
  134. label, err := cc.imageFsInfoProvider.ImageFsInfoLabel()
  135. if err != nil {
  136. return cadvisorapiv2.FsInfo{}, err
  137. }
  138. return cc.getFsInfo(label)
  139. }
  140. func (cc *cadvisorClient) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
  141. return cc.GetDirFsInfo(cc.rootPath)
  142. }
  143. func (cc *cadvisorClient) getFsInfo(label string) (cadvisorapiv2.FsInfo, error) {
  144. res, err := cc.GetFsInfo(label)
  145. if err != nil {
  146. return cadvisorapiv2.FsInfo{}, err
  147. }
  148. if len(res) == 0 {
  149. return cadvisorapiv2.FsInfo{}, fmt.Errorf("failed to find information for the filesystem labeled %q", label)
  150. }
  151. // TODO(vmarmol): Handle this better when a label has more than one image filesystem.
  152. if len(res) > 1 {
  153. klog.Warningf("More than one filesystem labeled %q: %#v. Only using the first one", label, res)
  154. }
  155. return res[0], nil
  156. }
  157. func (cc *cadvisorClient) WatchEvents(request *events.Request) (*events.EventChannel, error) {
  158. return cc.WatchForEvents(request)
  159. }