metrics.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2018 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 metrics
  14. import (
  15. "sync"
  16. "k8s.io/apimachinery/pkg/labels"
  17. "k8s.io/apimachinery/pkg/types"
  18. corelisters "k8s.io/client-go/listers/core/v1"
  19. "k8s.io/component-base/metrics"
  20. "k8s.io/component-base/metrics/legacyregistry"
  21. "k8s.io/klog"
  22. "k8s.io/kubernetes/pkg/controller/volume/attachdetach/cache"
  23. "k8s.io/kubernetes/pkg/controller/volume/attachdetach/util"
  24. "k8s.io/kubernetes/pkg/volume"
  25. "k8s.io/kubernetes/pkg/volume/csimigration"
  26. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  27. )
  28. const pluginNameNotAvailable = "N/A"
  29. var (
  30. inUseVolumeMetricDesc = metrics.NewDesc(
  31. metrics.BuildFQName("", "storage_count", "attachable_volumes_in_use"),
  32. "Measure number of volumes in use",
  33. []string{"node", "volume_plugin"}, nil,
  34. metrics.ALPHA, "")
  35. totalVolumesMetricDesc = metrics.NewDesc(
  36. metrics.BuildFQName("", "attachdetach_controller", "total_volumes"),
  37. "Number of volumes in A/D Controller",
  38. []string{"plugin_name", "state"}, nil,
  39. metrics.ALPHA, "")
  40. forcedDetachMetricCounter = metrics.NewCounter(
  41. &metrics.CounterOpts{
  42. Name: "attachdetach_controller_forced_detaches",
  43. Help: "Number of times the A/D Controller performed a forced detach",
  44. StabilityLevel: metrics.ALPHA,
  45. })
  46. )
  47. var registerMetrics sync.Once
  48. // Register registers metrics in A/D Controller.
  49. func Register(pvcLister corelisters.PersistentVolumeClaimLister,
  50. pvLister corelisters.PersistentVolumeLister,
  51. podLister corelisters.PodLister,
  52. asw cache.ActualStateOfWorld,
  53. dsw cache.DesiredStateOfWorld,
  54. pluginMgr *volume.VolumePluginMgr,
  55. csiMigratedPluginManager csimigration.PluginManager,
  56. intreeToCSITranslator csimigration.InTreeToCSITranslator) {
  57. registerMetrics.Do(func() {
  58. legacyregistry.CustomMustRegister(newAttachDetachStateCollector(pvcLister,
  59. podLister,
  60. pvLister,
  61. asw,
  62. dsw,
  63. pluginMgr,
  64. csiMigratedPluginManager,
  65. intreeToCSITranslator))
  66. legacyregistry.MustRegister(forcedDetachMetricCounter)
  67. })
  68. }
  69. type attachDetachStateCollector struct {
  70. metrics.BaseStableCollector
  71. pvcLister corelisters.PersistentVolumeClaimLister
  72. podLister corelisters.PodLister
  73. pvLister corelisters.PersistentVolumeLister
  74. asw cache.ActualStateOfWorld
  75. dsw cache.DesiredStateOfWorld
  76. volumePluginMgr *volume.VolumePluginMgr
  77. csiMigratedPluginManager csimigration.PluginManager
  78. intreeToCSITranslator csimigration.InTreeToCSITranslator
  79. }
  80. // volumeCount is a map of maps used as a counter, e.g.:
  81. // node 172.168.1.100.ec2.internal has 10 EBS and 3 glusterfs PVC in use:
  82. // {"172.168.1.100.ec2.internal": {"aws-ebs": 10, "glusterfs": 3}}
  83. // state actual_state_of_world contains a total of 10 EBS volumes:
  84. // {"actual_state_of_world": {"aws-ebs": 10}}
  85. type volumeCount map[string]map[string]int64
  86. func (v volumeCount) add(typeKey, counterKey string) {
  87. count, ok := v[typeKey]
  88. if !ok {
  89. count = map[string]int64{}
  90. }
  91. count[counterKey]++
  92. v[typeKey] = count
  93. }
  94. func newAttachDetachStateCollector(
  95. pvcLister corelisters.PersistentVolumeClaimLister,
  96. podLister corelisters.PodLister,
  97. pvLister corelisters.PersistentVolumeLister,
  98. asw cache.ActualStateOfWorld,
  99. dsw cache.DesiredStateOfWorld,
  100. pluginMgr *volume.VolumePluginMgr,
  101. csiMigratedPluginManager csimigration.PluginManager,
  102. intreeToCSITranslator csimigration.InTreeToCSITranslator) *attachDetachStateCollector {
  103. return &attachDetachStateCollector{pvcLister: pvcLister, podLister: podLister, pvLister: pvLister, asw: asw, dsw: dsw, volumePluginMgr: pluginMgr, csiMigratedPluginManager: csiMigratedPluginManager, intreeToCSITranslator: intreeToCSITranslator}
  104. }
  105. // Check if our collector implements necessary collector interface
  106. var _ metrics.StableCollector = &attachDetachStateCollector{}
  107. func (collector *attachDetachStateCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
  108. ch <- inUseVolumeMetricDesc
  109. ch <- totalVolumesMetricDesc
  110. }
  111. func (collector *attachDetachStateCollector) CollectWithStability(ch chan<- metrics.Metric) {
  112. nodeVolumeMap := collector.getVolumeInUseCount()
  113. for nodeName, pluginCount := range nodeVolumeMap {
  114. for pluginName, count := range pluginCount {
  115. ch <- metrics.NewLazyConstMetric(inUseVolumeMetricDesc,
  116. metrics.GaugeValue,
  117. float64(count),
  118. string(nodeName),
  119. pluginName)
  120. }
  121. }
  122. stateVolumeMap := collector.getTotalVolumesCount()
  123. for stateName, pluginCount := range stateVolumeMap {
  124. for pluginName, count := range pluginCount {
  125. ch <- metrics.NewLazyConstMetric(totalVolumesMetricDesc,
  126. metrics.GaugeValue,
  127. float64(count),
  128. pluginName,
  129. string(stateName))
  130. }
  131. }
  132. }
  133. func (collector *attachDetachStateCollector) getVolumeInUseCount() volumeCount {
  134. pods, err := collector.podLister.List(labels.Everything())
  135. if err != nil {
  136. klog.Errorf("Error getting pod list")
  137. return nil
  138. }
  139. nodeVolumeMap := make(volumeCount)
  140. for _, pod := range pods {
  141. if len(pod.Spec.Volumes) <= 0 {
  142. continue
  143. }
  144. if pod.Spec.NodeName == "" {
  145. continue
  146. }
  147. for _, podVolume := range pod.Spec.Volumes {
  148. volumeSpec, err := util.CreateVolumeSpec(podVolume, pod.Namespace, types.NodeName(pod.Spec.NodeName), collector.volumePluginMgr, collector.pvcLister, collector.pvLister, collector.csiMigratedPluginManager, collector.intreeToCSITranslator)
  149. if err != nil {
  150. continue
  151. }
  152. volumePlugin, err := collector.volumePluginMgr.FindPluginBySpec(volumeSpec)
  153. if err != nil {
  154. continue
  155. }
  156. pluginName := volumeutil.GetFullQualifiedPluginNameForVolume(volumePlugin.GetPluginName(), volumeSpec)
  157. nodeVolumeMap.add(pod.Spec.NodeName, pluginName)
  158. }
  159. }
  160. return nodeVolumeMap
  161. }
  162. func (collector *attachDetachStateCollector) getTotalVolumesCount() volumeCount {
  163. stateVolumeMap := make(volumeCount)
  164. for _, v := range collector.dsw.GetVolumesToAttach() {
  165. if plugin, err := collector.volumePluginMgr.FindPluginBySpec(v.VolumeSpec); err == nil {
  166. pluginName := pluginNameNotAvailable
  167. if plugin != nil {
  168. pluginName = volumeutil.GetFullQualifiedPluginNameForVolume(plugin.GetPluginName(), v.VolumeSpec)
  169. }
  170. stateVolumeMap.add("desired_state_of_world", pluginName)
  171. }
  172. }
  173. for _, v := range collector.asw.GetAttachedVolumes() {
  174. if plugin, err := collector.volumePluginMgr.FindPluginBySpec(v.VolumeSpec); err == nil {
  175. pluginName := pluginNameNotAvailable
  176. if plugin != nil {
  177. pluginName = volumeutil.GetFullQualifiedPluginNameForVolume(plugin.GetPluginName(), v.VolumeSpec)
  178. }
  179. stateVolumeMap.add("actual_state_of_world", pluginName)
  180. }
  181. }
  182. return stateVolumeMap
  183. }
  184. // RecordForcedDetachMetric register a forced detach metric.
  185. func RecordForcedDetachMetric() {
  186. forcedDetachMetricCounter.Inc()
  187. }