kubemetrics.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package metrics
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. "github.com/kubecost/cost-model/pkg/clustercache"
  7. "github.com/kubecost/cost-model/pkg/prom"
  8. "github.com/prometheus/client_golang/prometheus"
  9. batchv1 "k8s.io/api/batch/v1"
  10. v1 "k8s.io/api/core/v1"
  11. "k8s.io/apimachinery/pkg/api/resource"
  12. "k8s.io/apimachinery/pkg/util/validation"
  13. )
  14. //--------------------------------------------------------------------------
  15. // Kube Metric Registration
  16. //--------------------------------------------------------------------------
  17. // initializer
  18. var kubeMetricInit sync.Once
  19. // KubeMetricsOpts represents our Kubernetes metrics emission options.
  20. type KubeMetricsOpts struct {
  21. EmitKubecostControllerMetrics bool
  22. EmitNamespaceAnnotations bool
  23. EmitPodAnnotations bool
  24. EmitKubeStateMetrics bool
  25. EmitKubeStateMetricsV1Only bool
  26. }
  27. // DefaultKubeMetricsOpts returns KubeMetricsOpts with default values set
  28. func DefaultKubeMetricsOpts() *KubeMetricsOpts {
  29. return &KubeMetricsOpts{
  30. EmitKubecostControllerMetrics: true,
  31. EmitNamespaceAnnotations: false,
  32. EmitPodAnnotations: false,
  33. EmitKubeStateMetrics: true,
  34. EmitKubeStateMetricsV1Only: false,
  35. }
  36. }
  37. // InitKubeMetrics initializes kubernetes metric emission using the provided options.
  38. func InitKubeMetrics(clusterCache clustercache.ClusterCache, opts *KubeMetricsOpts) {
  39. if opts == nil {
  40. opts = DefaultKubeMetricsOpts()
  41. }
  42. kubeMetricInit.Do(func() {
  43. if opts.EmitKubecostControllerMetrics {
  44. prometheus.MustRegister(KubecostServiceCollector{
  45. KubeClusterCache: clusterCache,
  46. })
  47. prometheus.MustRegister(KubecostDeploymentCollector{
  48. KubeClusterCache: clusterCache,
  49. })
  50. prometheus.MustRegister(KubecostStatefulsetCollector{
  51. KubeClusterCache: clusterCache,
  52. })
  53. }
  54. if opts.EmitPodAnnotations {
  55. prometheus.MustRegister(KubecostPodCollector{
  56. KubeClusterCache: clusterCache,
  57. })
  58. }
  59. if opts.EmitNamespaceAnnotations {
  60. prometheus.MustRegister(KubecostNamespaceCollector{
  61. KubeClusterCache: clusterCache,
  62. })
  63. }
  64. if opts.EmitKubeStateMetrics {
  65. prometheus.MustRegister(KubeNodeCollector{
  66. KubeClusterCache: clusterCache,
  67. })
  68. prometheus.MustRegister(KubeNamespaceCollector{
  69. KubeClusterCache: clusterCache,
  70. })
  71. prometheus.MustRegister(KubeDeploymentCollector{
  72. KubeClusterCache: clusterCache,
  73. })
  74. prometheus.MustRegister(KubePodCollector{
  75. KubeClusterCache: clusterCache,
  76. })
  77. prometheus.MustRegister(KubePVCollector{
  78. KubeClusterCache: clusterCache,
  79. })
  80. prometheus.MustRegister(KubePVCCollector{
  81. KubeClusterCache: clusterCache,
  82. })
  83. prometheus.MustRegister(KubeJobCollector{
  84. KubeClusterCache: clusterCache,
  85. })
  86. } else if opts.EmitKubeStateMetricsV1Only {
  87. prometheus.MustRegister(KubeNodeCollector{
  88. KubeClusterCache: clusterCache,
  89. })
  90. prometheus.MustRegister(KubeNamespaceCollector{
  91. KubeClusterCache: clusterCache,
  92. })
  93. prometheus.MustRegister(KubePodLabelsCollector{
  94. KubeClusterCache: clusterCache,
  95. })
  96. }
  97. })
  98. }
  99. //--------------------------------------------------------------------------
  100. // Kube Metric Helpers
  101. //--------------------------------------------------------------------------
  102. // getPersistentVolumeClaimClass returns StorageClassName. If no storage class was
  103. // requested, it returns "".
  104. func getPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
  105. // Use beta annotation first
  106. if class, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found {
  107. return class
  108. }
  109. if claim.Spec.StorageClassName != nil {
  110. return *claim.Spec.StorageClassName
  111. }
  112. // Special non-empty string to indicate absence of storage class.
  113. return "<none>"
  114. }
  115. // toResourceUnitValue accepts a resource name and quantity and returns the sanitized resource, the unit, and the value in the units.
  116. // Returns an empty string for resource and unit if there was a failure.
  117. func toResourceUnitValue(resourceName v1.ResourceName, quantity resource.Quantity) (resource string, unit string, value float64) {
  118. resource = prom.SanitizeLabelName(string(resourceName))
  119. switch resourceName {
  120. case v1.ResourceCPU:
  121. unit = "core"
  122. value = float64(quantity.MilliValue()) / 1000
  123. return
  124. case v1.ResourceStorage:
  125. fallthrough
  126. case v1.ResourceEphemeralStorage:
  127. fallthrough
  128. case v1.ResourceMemory:
  129. unit = "byte"
  130. value = float64(quantity.Value())
  131. return
  132. case v1.ResourcePods:
  133. unit = "integer"
  134. value = float64(quantity.Value())
  135. return
  136. default:
  137. if isHugePageResourceName(resourceName) || isAttachableVolumeResourceName(resourceName) {
  138. unit = "byte"
  139. value = float64(quantity.Value())
  140. return
  141. }
  142. if isExtendedResourceName(resourceName) {
  143. unit = "integer"
  144. value = float64(quantity.Value())
  145. return
  146. }
  147. }
  148. resource = ""
  149. unit = ""
  150. value = 0.0
  151. return
  152. }
  153. // isHugePageResourceName checks for a huge page container resource name
  154. func isHugePageResourceName(name v1.ResourceName) bool {
  155. return strings.HasPrefix(string(name), v1.ResourceHugePagesPrefix)
  156. }
  157. // isAttachableVolumeResourceName checks for attached volume container resource name
  158. func isAttachableVolumeResourceName(name v1.ResourceName) bool {
  159. return strings.HasPrefix(string(name), v1.ResourceAttachableVolumesPrefix)
  160. }
  161. // isExtendedResourceName checks for extended container resource name
  162. func isExtendedResourceName(name v1.ResourceName) bool {
  163. if isNativeResource(name) || strings.HasPrefix(string(name), v1.DefaultResourceRequestsPrefix) {
  164. return false
  165. }
  166. // Ensure it satisfies the rules in IsQualifiedName() after converted into quota resource name
  167. nameForQuota := fmt.Sprintf("%s%s", v1.DefaultResourceRequestsPrefix, string(name))
  168. if errs := validation.IsQualifiedName(nameForQuota); len(errs) != 0 {
  169. return false
  170. }
  171. return true
  172. }
  173. // isNativeResource checks for a kubernetes.io/ prefixed resource name
  174. func isNativeResource(name v1.ResourceName) bool {
  175. return !strings.Contains(string(name), "/") || isPrefixedNativeResource(name)
  176. }
  177. func isPrefixedNativeResource(name v1.ResourceName) bool {
  178. return strings.Contains(string(name), v1.ResourceDefaultNamespacePrefix)
  179. }
  180. func failureReason(jc *batchv1.JobCondition, reason string) bool {
  181. if jc == nil {
  182. return false
  183. }
  184. return jc.Reason == reason
  185. }
  186. // boolFloat64 converts a boolean input into a 1 or 0
  187. func boolFloat64(b bool) float64 {
  188. if b {
  189. return 1
  190. }
  191. return 0
  192. }
  193. // toStringPtr is used to create a new string pointer from iteration vars
  194. func toStringPtr(s string) *string { return &s }