factory.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package container
  15. import (
  16. "fmt"
  17. "sync"
  18. "github.com/google/cadvisor/fs"
  19. info "github.com/google/cadvisor/info/v1"
  20. "github.com/google/cadvisor/watcher"
  21. "k8s.io/klog"
  22. )
  23. type ContainerHandlerFactory interface {
  24. // Create a new ContainerHandler using this factory. CanHandleAndAccept() must have returned true.
  25. NewContainerHandler(name string, inHostNamespace bool) (c ContainerHandler, err error)
  26. // Returns whether this factory can handle and accept the specified container.
  27. CanHandleAndAccept(name string) (handle bool, accept bool, err error)
  28. // Name of the factory.
  29. String() string
  30. // Returns debugging information. Map of lines per category.
  31. DebugInfo() map[string][]string
  32. }
  33. // MetricKind represents the kind of metrics that cAdvisor exposes.
  34. type MetricKind string
  35. const (
  36. CpuUsageMetrics MetricKind = "cpu"
  37. ProcessSchedulerMetrics MetricKind = "sched"
  38. PerCpuUsageMetrics MetricKind = "percpu"
  39. MemoryUsageMetrics MetricKind = "memory"
  40. CpuLoadMetrics MetricKind = "cpuLoad"
  41. DiskIOMetrics MetricKind = "diskIO"
  42. DiskUsageMetrics MetricKind = "disk"
  43. NetworkUsageMetrics MetricKind = "network"
  44. NetworkTcpUsageMetrics MetricKind = "tcp"
  45. NetworkUdpUsageMetrics MetricKind = "udp"
  46. AcceleratorUsageMetrics MetricKind = "accelerator"
  47. AppMetrics MetricKind = "app"
  48. ProcessMetrics MetricKind = "process"
  49. )
  50. func (mk MetricKind) String() string {
  51. return string(mk)
  52. }
  53. type MetricSet map[MetricKind]struct{}
  54. func (ms MetricSet) Has(mk MetricKind) bool {
  55. _, exists := ms[mk]
  56. return exists
  57. }
  58. func (ms MetricSet) Add(mk MetricKind) {
  59. ms[mk] = struct{}{}
  60. }
  61. // All registered auth provider plugins.
  62. var pluginsLock sync.Mutex
  63. var plugins = make(map[string]Plugin)
  64. type Plugin interface {
  65. // InitializeFSContext is invoked when populating an fs.Context object for a new manager.
  66. // A returned error here is fatal.
  67. InitializeFSContext(context *fs.Context) error
  68. // Register is invoked when starting a manager. It can optionally return a container watcher.
  69. // A returned error is logged, but is not fatal.
  70. Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error)
  71. }
  72. func RegisterPlugin(name string, plugin Plugin) error {
  73. pluginsLock.Lock()
  74. defer pluginsLock.Unlock()
  75. if _, found := plugins[name]; found {
  76. return fmt.Errorf("Plugin %q was registered twice", name)
  77. }
  78. klog.V(4).Infof("Registered Plugin %q", name)
  79. plugins[name] = plugin
  80. return nil
  81. }
  82. func InitializeFSContext(context *fs.Context) error {
  83. pluginsLock.Lock()
  84. defer pluginsLock.Unlock()
  85. for name, plugin := range plugins {
  86. err := plugin.InitializeFSContext(context)
  87. if err != nil {
  88. klog.V(5).Infof("Initialization of the %s context failed: %v", name, err)
  89. return err
  90. }
  91. }
  92. return nil
  93. }
  94. func InitializePlugins(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) []watcher.ContainerWatcher {
  95. pluginsLock.Lock()
  96. defer pluginsLock.Unlock()
  97. containerWatchers := []watcher.ContainerWatcher{}
  98. for name, plugin := range plugins {
  99. watcher, err := plugin.Register(factory, fsInfo, includedMetrics)
  100. if err != nil {
  101. klog.V(5).Infof("Registration of the %s container factory failed: %v", name, err)
  102. }
  103. if watcher != nil {
  104. containerWatchers = append(containerWatchers, watcher)
  105. }
  106. }
  107. return containerWatchers
  108. }
  109. // TODO(vmarmol): Consider not making this global.
  110. // Global list of factories.
  111. var (
  112. factories = map[watcher.ContainerWatchSource][]ContainerHandlerFactory{}
  113. factoriesLock sync.RWMutex
  114. )
  115. // Register a ContainerHandlerFactory. These should be registered from least general to most general
  116. // as they will be asked in order whether they can handle a particular container.
  117. func RegisterContainerHandlerFactory(factory ContainerHandlerFactory, watchTypes []watcher.ContainerWatchSource) {
  118. factoriesLock.Lock()
  119. defer factoriesLock.Unlock()
  120. for _, watchType := range watchTypes {
  121. factories[watchType] = append(factories[watchType], factory)
  122. }
  123. }
  124. // Returns whether there are any container handler factories registered.
  125. func HasFactories() bool {
  126. factoriesLock.Lock()
  127. defer factoriesLock.Unlock()
  128. return len(factories) != 0
  129. }
  130. // Create a new ContainerHandler for the specified container.
  131. func NewContainerHandler(name string, watchType watcher.ContainerWatchSource, inHostNamespace bool) (ContainerHandler, bool, error) {
  132. factoriesLock.RLock()
  133. defer factoriesLock.RUnlock()
  134. // Create the ContainerHandler with the first factory that supports it.
  135. for _, factory := range factories[watchType] {
  136. canHandle, canAccept, err := factory.CanHandleAndAccept(name)
  137. if err != nil {
  138. klog.V(4).Infof("Error trying to work out if we can handle %s: %v", name, err)
  139. }
  140. if canHandle {
  141. if !canAccept {
  142. klog.V(3).Infof("Factory %q can handle container %q, but ignoring.", factory, name)
  143. return nil, false, nil
  144. }
  145. klog.V(3).Infof("Using factory %q for container %q", factory, name)
  146. handle, err := factory.NewContainerHandler(name, inHostNamespace)
  147. return handle, canAccept, err
  148. } else {
  149. klog.V(4).Infof("Factory %q was unable to handle container %q", factory, name)
  150. }
  151. }
  152. return nil, false, fmt.Errorf("no known factory can handle creation of container")
  153. }
  154. // Clear the known factories.
  155. func ClearContainerHandlerFactories() {
  156. factoriesLock.Lock()
  157. defer factoriesLock.Unlock()
  158. factories = map[watcher.ContainerWatchSource][]ContainerHandlerFactory{}
  159. }
  160. func DebugInfo() map[string][]string {
  161. factoriesLock.RLock()
  162. defer factoriesLock.RUnlock()
  163. // Get debug information for all factories.
  164. out := make(map[string][]string)
  165. for _, factoriesSlice := range factories {
  166. for _, factory := range factoriesSlice {
  167. for k, v := range factory.DebugInfo() {
  168. out[k] = v
  169. }
  170. }
  171. }
  172. return out
  173. }