factory.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. "sort"
  18. "strings"
  19. "sync"
  20. "github.com/google/cadvisor/fs"
  21. info "github.com/google/cadvisor/info/v1"
  22. "github.com/google/cadvisor/watcher"
  23. "k8s.io/klog/v2"
  24. )
  25. type ContainerHandlerFactory interface {
  26. // Create a new ContainerHandler using this factory. CanHandleAndAccept() must have returned true.
  27. NewContainerHandler(name string, metadataEnvAllowList []string, inHostNamespace bool) (c ContainerHandler, err error)
  28. // Returns whether this factory can handle and accept the specified container.
  29. CanHandleAndAccept(name string) (handle bool, accept bool, err error)
  30. // Name of the factory.
  31. String() string
  32. // Returns debugging information. Map of lines per category.
  33. DebugInfo() map[string][]string
  34. }
  35. // MetricKind represents the kind of metrics that cAdvisor exposes.
  36. type MetricKind string
  37. const (
  38. CpuUsageMetrics MetricKind = "cpu"
  39. ProcessSchedulerMetrics MetricKind = "sched"
  40. PerCpuUsageMetrics MetricKind = "percpu"
  41. MemoryUsageMetrics MetricKind = "memory"
  42. MemoryNumaMetrics MetricKind = "memory_numa"
  43. CpuLoadMetrics MetricKind = "cpuLoad"
  44. DiskIOMetrics MetricKind = "diskIO"
  45. DiskUsageMetrics MetricKind = "disk"
  46. NetworkUsageMetrics MetricKind = "network"
  47. NetworkTcpUsageMetrics MetricKind = "tcp"
  48. NetworkAdvancedTcpUsageMetrics MetricKind = "advtcp"
  49. NetworkUdpUsageMetrics MetricKind = "udp"
  50. AcceleratorUsageMetrics MetricKind = "accelerator"
  51. AppMetrics MetricKind = "app"
  52. ProcessMetrics MetricKind = "process"
  53. HugetlbUsageMetrics MetricKind = "hugetlb"
  54. PerfMetrics MetricKind = "perf_event"
  55. ReferencedMemoryMetrics MetricKind = "referenced_memory"
  56. CPUTopologyMetrics MetricKind = "cpu_topology"
  57. ResctrlMetrics MetricKind = "resctrl"
  58. CPUSetMetrics MetricKind = "cpuset"
  59. OOMMetrics MetricKind = "oom_event"
  60. )
  61. // AllMetrics represents all kinds of metrics that cAdvisor supported.
  62. var AllMetrics = MetricSet{
  63. CpuUsageMetrics: struct{}{},
  64. ProcessSchedulerMetrics: struct{}{},
  65. PerCpuUsageMetrics: struct{}{},
  66. MemoryUsageMetrics: struct{}{},
  67. MemoryNumaMetrics: struct{}{},
  68. CpuLoadMetrics: struct{}{},
  69. DiskIOMetrics: struct{}{},
  70. AcceleratorUsageMetrics: struct{}{},
  71. DiskUsageMetrics: struct{}{},
  72. NetworkUsageMetrics: struct{}{},
  73. NetworkTcpUsageMetrics: struct{}{},
  74. NetworkAdvancedTcpUsageMetrics: struct{}{},
  75. NetworkUdpUsageMetrics: struct{}{},
  76. ProcessMetrics: struct{}{},
  77. AppMetrics: struct{}{},
  78. HugetlbUsageMetrics: struct{}{},
  79. PerfMetrics: struct{}{},
  80. ReferencedMemoryMetrics: struct{}{},
  81. CPUTopologyMetrics: struct{}{},
  82. ResctrlMetrics: struct{}{},
  83. CPUSetMetrics: struct{}{},
  84. OOMMetrics: struct{}{},
  85. }
  86. func (mk MetricKind) String() string {
  87. return string(mk)
  88. }
  89. type MetricSet map[MetricKind]struct{}
  90. func (ms MetricSet) Has(mk MetricKind) bool {
  91. _, exists := ms[mk]
  92. return exists
  93. }
  94. func (ms MetricSet) add(mk MetricKind) {
  95. ms[mk] = struct{}{}
  96. }
  97. func (ms MetricSet) String() string {
  98. values := make([]string, 0, len(ms))
  99. for metric := range ms {
  100. values = append(values, string(metric))
  101. }
  102. sort.Strings(values)
  103. return strings.Join(values, ",")
  104. }
  105. // Not thread-safe, exported only for https://pkg.go.dev/flag#Value
  106. func (ms *MetricSet) Set(value string) error {
  107. *ms = MetricSet{}
  108. if value == "" {
  109. return nil
  110. }
  111. for _, metric := range strings.Split(value, ",") {
  112. if AllMetrics.Has(MetricKind(metric)) {
  113. (*ms).add(MetricKind(metric))
  114. } else {
  115. return fmt.Errorf("unsupported metric %q specified", metric)
  116. }
  117. }
  118. return nil
  119. }
  120. func (ms MetricSet) Difference(ms1 MetricSet) MetricSet {
  121. result := MetricSet{}
  122. for kind := range ms {
  123. if !ms1.Has(kind) {
  124. result.add(kind)
  125. }
  126. }
  127. return result
  128. }
  129. func (ms MetricSet) Append(ms1 MetricSet) MetricSet {
  130. result := ms
  131. for kind := range ms1 {
  132. if !ms.Has(kind) {
  133. result.add(kind)
  134. }
  135. }
  136. return result
  137. }
  138. // All registered auth provider plugins.
  139. var pluginsLock sync.Mutex
  140. var plugins = make(map[string]Plugin)
  141. type Plugin interface {
  142. // InitializeFSContext is invoked when populating an fs.Context object for a new manager.
  143. // A returned error here is fatal.
  144. InitializeFSContext(context *fs.Context) error
  145. // Register is invoked when starting a manager. It can optionally return a container watcher.
  146. // A returned error is logged, but is not fatal.
  147. Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error)
  148. }
  149. func RegisterPlugin(name string, plugin Plugin) error {
  150. pluginsLock.Lock()
  151. defer pluginsLock.Unlock()
  152. if _, found := plugins[name]; found {
  153. return fmt.Errorf("Plugin %q was registered twice", name)
  154. }
  155. klog.V(4).Infof("Registered Plugin %q", name)
  156. plugins[name] = plugin
  157. return nil
  158. }
  159. func InitializeFSContext(context *fs.Context) error {
  160. pluginsLock.Lock()
  161. defer pluginsLock.Unlock()
  162. for name, plugin := range plugins {
  163. err := plugin.InitializeFSContext(context)
  164. if err != nil {
  165. klog.V(5).Infof("Initialization of the %s context failed: %v", name, err)
  166. return err
  167. }
  168. }
  169. return nil
  170. }
  171. func InitializePlugins(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) []watcher.ContainerWatcher {
  172. pluginsLock.Lock()
  173. defer pluginsLock.Unlock()
  174. containerWatchers := []watcher.ContainerWatcher{}
  175. for name, plugin := range plugins {
  176. watcher, err := plugin.Register(factory, fsInfo, includedMetrics)
  177. if err != nil {
  178. klog.V(5).Infof("Registration of the %s container factory failed: %v", name, err)
  179. }
  180. if watcher != nil {
  181. containerWatchers = append(containerWatchers, watcher)
  182. }
  183. }
  184. return containerWatchers
  185. }
  186. // TODO(vmarmol): Consider not making this global.
  187. // Global list of factories.
  188. var (
  189. factories = map[watcher.ContainerWatchSource][]ContainerHandlerFactory{}
  190. factoriesLock sync.RWMutex
  191. )
  192. // Register a ContainerHandlerFactory. These should be registered from least general to most general
  193. // as they will be asked in order whether they can handle a particular container.
  194. func RegisterContainerHandlerFactory(factory ContainerHandlerFactory, watchTypes []watcher.ContainerWatchSource) {
  195. factoriesLock.Lock()
  196. defer factoriesLock.Unlock()
  197. for _, watchType := range watchTypes {
  198. factories[watchType] = append(factories[watchType], factory)
  199. }
  200. }
  201. // Returns whether there are any container handler factories registered.
  202. func HasFactories() bool {
  203. factoriesLock.Lock()
  204. defer factoriesLock.Unlock()
  205. return len(factories) != 0
  206. }
  207. // Create a new ContainerHandler for the specified container.
  208. func NewContainerHandler(name string, watchType watcher.ContainerWatchSource, metadataEnvAllowList []string, inHostNamespace bool) (ContainerHandler, bool, error) {
  209. factoriesLock.RLock()
  210. defer factoriesLock.RUnlock()
  211. // Create the ContainerHandler with the first factory that supports it.
  212. // Note that since RawContainerHandler can support a wide range of paths,
  213. // it's evaluated last just to make sure if any other ContainerHandler
  214. // can support it.
  215. for _, factory := range GetReorderedFactoryList(watchType) {
  216. canHandle, canAccept, err := factory.CanHandleAndAccept(name)
  217. if err != nil {
  218. klog.V(4).Infof("Error trying to work out if we can handle %s: %v", name, err)
  219. }
  220. if canHandle {
  221. if !canAccept {
  222. klog.V(3).Infof("Factory %q can handle container %q, but ignoring.", factory, name)
  223. return nil, false, nil
  224. }
  225. klog.V(3).Infof("Using factory %q for container %q", factory, name)
  226. handle, err := factory.NewContainerHandler(name, metadataEnvAllowList, inHostNamespace)
  227. return handle, canAccept, err
  228. }
  229. klog.V(4).Infof("Factory %q was unable to handle container %q", factory, name)
  230. }
  231. return nil, false, fmt.Errorf("no known factory can handle creation of container")
  232. }
  233. // Clear the known factories.
  234. func ClearContainerHandlerFactories() {
  235. factoriesLock.Lock()
  236. defer factoriesLock.Unlock()
  237. factories = map[watcher.ContainerWatchSource][]ContainerHandlerFactory{}
  238. }
  239. func DebugInfo() map[string][]string {
  240. factoriesLock.RLock()
  241. defer factoriesLock.RUnlock()
  242. // Get debug information for all factories.
  243. out := make(map[string][]string)
  244. for _, factoriesSlice := range factories {
  245. for _, factory := range factoriesSlice {
  246. for k, v := range factory.DebugInfo() {
  247. out[k] = v
  248. }
  249. }
  250. }
  251. return out
  252. }
  253. // GetReorderedFactoryList returns the list of ContainerHandlerFactory where the
  254. // RawContainerHandler is always the last element.
  255. func GetReorderedFactoryList(watchType watcher.ContainerWatchSource) []ContainerHandlerFactory {
  256. ContainerHandlerFactoryList := make([]ContainerHandlerFactory, 0, len(factories))
  257. var rawFactory ContainerHandlerFactory
  258. for _, v := range factories[watchType] {
  259. if v != nil {
  260. if v.String() == "raw" {
  261. rawFactory = v
  262. continue
  263. }
  264. ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, v)
  265. }
  266. }
  267. if rawFactory != nil {
  268. ContainerHandlerFactoryList = append(ContainerHandlerFactoryList, rawFactory)
  269. }
  270. return ContainerHandlerFactoryList
  271. }