cadvisor_linux.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // +build cgo,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. func New(imageFsInfoProvider ImageFsInfoProvider, rootPath string, cgroupRoots []string, usingLegacyStats bool) (Interface, error) {
  72. sysFs := sysfs.NewRealSysFs()
  73. includedMetrics := cadvisormetrics.MetricSet{
  74. cadvisormetrics.CpuUsageMetrics: struct{}{},
  75. cadvisormetrics.MemoryUsageMetrics: struct{}{},
  76. cadvisormetrics.CpuLoadMetrics: struct{}{},
  77. cadvisormetrics.DiskIOMetrics: struct{}{},
  78. cadvisormetrics.NetworkUsageMetrics: struct{}{},
  79. cadvisormetrics.AcceleratorUsageMetrics: struct{}{},
  80. cadvisormetrics.AppMetrics: struct{}{},
  81. }
  82. if usingLegacyStats {
  83. includedMetrics[cadvisormetrics.DiskUsageMetrics] = struct{}{}
  84. }
  85. // Create and start the cAdvisor container manager.
  86. m, err := manager.New(memory.New(statsCacheDuration, nil), sysFs, maxHousekeepingInterval, allowDynamicHousekeeping, includedMetrics, http.DefaultClient, cgroupRoots)
  87. if err != nil {
  88. return nil, err
  89. }
  90. if _, err := os.Stat(rootPath); err != nil {
  91. if os.IsNotExist(err) {
  92. if err := os.MkdirAll(path.Clean(rootPath), 0750); err != nil {
  93. return nil, fmt.Errorf("error creating root directory %q: %v", rootPath, err)
  94. }
  95. } else {
  96. return nil, fmt.Errorf("failed to Stat %q: %v", rootPath, err)
  97. }
  98. }
  99. cadvisorClient := &cadvisorClient{
  100. imageFsInfoProvider: imageFsInfoProvider,
  101. rootPath: rootPath,
  102. Manager: m,
  103. }
  104. return cadvisorClient, nil
  105. }
  106. func (cc *cadvisorClient) Start() error {
  107. return cc.Manager.Start()
  108. }
  109. func (cc *cadvisorClient) ContainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (*cadvisorapi.ContainerInfo, error) {
  110. return cc.GetContainerInfo(name, req)
  111. }
  112. func (cc *cadvisorClient) ContainerInfoV2(name string, options cadvisorapiv2.RequestOptions) (map[string]cadvisorapiv2.ContainerInfo, error) {
  113. return cc.GetContainerInfoV2(name, options)
  114. }
  115. func (cc *cadvisorClient) VersionInfo() (*cadvisorapi.VersionInfo, error) {
  116. return cc.GetVersionInfo()
  117. }
  118. func (cc *cadvisorClient) SubcontainerInfo(name string, req *cadvisorapi.ContainerInfoRequest) (map[string]*cadvisorapi.ContainerInfo, error) {
  119. infos, err := cc.SubcontainersInfo(name, req)
  120. if err != nil && len(infos) == 0 {
  121. return nil, err
  122. }
  123. result := make(map[string]*cadvisorapi.ContainerInfo, len(infos))
  124. for _, info := range infos {
  125. result[info.Name] = info
  126. }
  127. return result, err
  128. }
  129. func (cc *cadvisorClient) MachineInfo() (*cadvisorapi.MachineInfo, error) {
  130. return cc.GetMachineInfo()
  131. }
  132. func (cc *cadvisorClient) ImagesFsInfo() (cadvisorapiv2.FsInfo, error) {
  133. label, err := cc.imageFsInfoProvider.ImageFsInfoLabel()
  134. if err != nil {
  135. return cadvisorapiv2.FsInfo{}, err
  136. }
  137. return cc.getFsInfo(label)
  138. }
  139. func (cc *cadvisorClient) RootFsInfo() (cadvisorapiv2.FsInfo, error) {
  140. return cc.GetDirFsInfo(cc.rootPath)
  141. }
  142. func (cc *cadvisorClient) getFsInfo(label string) (cadvisorapiv2.FsInfo, error) {
  143. res, err := cc.GetFsInfo(label)
  144. if err != nil {
  145. return cadvisorapiv2.FsInfo{}, err
  146. }
  147. if len(res) == 0 {
  148. return cadvisorapiv2.FsInfo{}, fmt.Errorf("failed to find information for the filesystem labeled %q", label)
  149. }
  150. // TODO(vmarmol): Handle this better when a label has more than one image filesystem.
  151. if len(res) > 1 {
  152. klog.Warningf("More than one filesystem labeled %q: %#v. Only using the first one", label, res)
  153. }
  154. return res[0], nil
  155. }
  156. func (cc *cadvisorClient) WatchEvents(request *events.Request) (*events.EventChannel, error) {
  157. return cc.WatchForEvents(request)
  158. }