config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2019 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 v1alpha1
  14. import (
  15. "time"
  16. "k8s.io/component-base/metrics"
  17. summary "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  18. "k8s.io/kubernetes/pkg/kubelet/server/stats"
  19. )
  20. // This file contains a series of deprecated metrics which we emit them by endpoint `/metrics/resource/v1alpha1`.
  21. // These metrics have been adapted to new endpoint `/metrics/resource` as well as new `Desc`s.
  22. // In general, we don't need to maintain these deprecated metrics any more.
  23. // TODO(RainbowMango): Remove this file in release 1.20.0+.
  24. // Version is the string representation of the version of this configuration
  25. const Version = "v1alpha1"
  26. var (
  27. nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total",
  28. "Cumulative cpu time consumed by the node in core-seconds",
  29. nil,
  30. nil,
  31. metrics.ALPHA,
  32. "1.18.0")
  33. nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes",
  34. "Current working set of the node in bytes",
  35. nil,
  36. nil,
  37. metrics.ALPHA,
  38. "1.18.0")
  39. containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total",
  40. "Cumulative cpu time consumed by the container in core-seconds",
  41. []string{"container", "pod", "namespace"},
  42. nil,
  43. metrics.ALPHA,
  44. "1.18.0")
  45. containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes",
  46. "Current working set of the container in bytes",
  47. []string{"container", "pod", "namespace"},
  48. nil,
  49. metrics.ALPHA,
  50. "1.18.0")
  51. )
  52. // getNodeCPUMetrics returns CPU utilization of a node.
  53. func getNodeCPUMetrics(s summary.NodeStats) (*float64, time.Time) {
  54. if s.CPU == nil {
  55. return nil, time.Time{}
  56. }
  57. v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
  58. return &v, s.CPU.Time.Time
  59. }
  60. // getNodeMemoryMetrics returns memory utilization of a node.
  61. func getNodeMemoryMetrics(s summary.NodeStats) (*float64, time.Time) {
  62. if s.Memory == nil {
  63. return nil, time.Time{}
  64. }
  65. v := float64(*s.Memory.WorkingSetBytes)
  66. return &v, s.Memory.Time.Time
  67. }
  68. // getContainerCPUMetrics returns CPU utilization of a container.
  69. func getContainerCPUMetrics(s summary.ContainerStats) (*float64, time.Time) {
  70. if s.CPU == nil {
  71. return nil, time.Time{}
  72. }
  73. v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
  74. return &v, s.CPU.Time.Time
  75. }
  76. // getContainerMemoryMetrics returns memory utilization of a container.
  77. func getContainerMemoryMetrics(s summary.ContainerStats) (*float64, time.Time) {
  78. if s.Memory == nil {
  79. return nil, time.Time{}
  80. }
  81. v := float64(*s.Memory.WorkingSetBytes)
  82. return &v, s.Memory.Time.Time
  83. }
  84. // Config is the v1alpha1 resource metrics definition
  85. func Config() stats.ResourceMetricsConfig {
  86. return stats.ResourceMetricsConfig{
  87. NodeMetrics: []stats.NodeResourceMetric{
  88. {
  89. Desc: nodeCPUUsageDesc,
  90. ValueFn: getNodeCPUMetrics,
  91. },
  92. {
  93. Desc: nodeMemoryUsageDesc,
  94. ValueFn: getNodeMemoryMetrics,
  95. },
  96. },
  97. ContainerMetrics: []stats.ContainerResourceMetric{
  98. {
  99. Desc: containerCPUUsageDesc,
  100. ValueFn: getContainerCPUMetrics,
  101. },
  102. {
  103. Desc: containerMemoryUsageDesc,
  104. ValueFn: getContainerMemoryMetrics,
  105. },
  106. },
  107. }
  108. }