Skip to content

Commit 42fa1c0

Browse files
authored
Merge pull request kubernetes#88568 from RainbowMango/pr_cleanup_resource_metrics_ut
Clean up duplicate code and remove import cycle.
2 parents 5777b91 + 7b7c73b commit 42fa1c0

File tree

6 files changed

+110
-216
lines changed

6 files changed

+110
-216
lines changed

pkg/kubelet/apis/resourcemetrics/v1alpha1/BUILD

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ go_library(
55
srcs = ["config.go"],
66
importpath = "k8s.io/kubernetes/pkg/kubelet/apis/resourcemetrics/v1alpha1",
77
visibility = ["//visibility:public"],
8-
deps = [
9-
"//pkg/kubelet/apis/stats/v1alpha1:go_default_library",
10-
"//pkg/kubelet/server/stats:go_default_library",
11-
"//staging/src/k8s.io/component-base/metrics:go_default_library",
12-
],
138
)
149

1510
filegroup(

pkg/kubelet/apis/resourcemetrics/v1alpha1/config.go

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,110 +16,8 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
import (
20-
"time"
21-
22-
"k8s.io/component-base/metrics"
23-
summary "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
24-
"k8s.io/kubernetes/pkg/kubelet/server/stats"
25-
)
26-
27-
// This file contains a series of deprecated metrics which we emit them by endpoint `/metrics/resource/v1alpha1`.
28-
// These metrics have been adapted to new endpoint `/metrics/resource` as well as new `Desc`s.
29-
// In general, we don't need to maintain these deprecated metrics any more.
30-
// TODO(RainbowMango): Remove this file in release 1.20.0+.
19+
// TODO(RainbowMango): We don't need to maintain this package anymore.
20+
// This package will be remove in release 1.20.0+. More details please refer to https://github.com/kubernetes/kubernetes/pull/86282.
3121

3222
// Version is the string representation of the version of this configuration
3323
const Version = "v1alpha1"
34-
35-
var (
36-
nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total",
37-
"Cumulative cpu time consumed by the node in core-seconds",
38-
nil,
39-
nil,
40-
metrics.ALPHA,
41-
"1.18.0")
42-
43-
nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes",
44-
"Current working set of the node in bytes",
45-
nil,
46-
nil,
47-
metrics.ALPHA,
48-
"1.18.0")
49-
50-
containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total",
51-
"Cumulative cpu time consumed by the container in core-seconds",
52-
[]string{"container", "pod", "namespace"},
53-
nil,
54-
metrics.ALPHA,
55-
"1.18.0")
56-
57-
containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes",
58-
"Current working set of the container in bytes",
59-
[]string{"container", "pod", "namespace"},
60-
nil,
61-
metrics.ALPHA,
62-
"1.18.0")
63-
)
64-
65-
// getNodeCPUMetrics returns CPU utilization of a node.
66-
func getNodeCPUMetrics(s summary.NodeStats) (*float64, time.Time) {
67-
if s.CPU == nil {
68-
return nil, time.Time{}
69-
}
70-
v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
71-
return &v, s.CPU.Time.Time
72-
}
73-
74-
// getNodeMemoryMetrics returns memory utilization of a node.
75-
func getNodeMemoryMetrics(s summary.NodeStats) (*float64, time.Time) {
76-
if s.Memory == nil {
77-
return nil, time.Time{}
78-
}
79-
v := float64(*s.Memory.WorkingSetBytes)
80-
return &v, s.Memory.Time.Time
81-
}
82-
83-
// getContainerCPUMetrics returns CPU utilization of a container.
84-
func getContainerCPUMetrics(s summary.ContainerStats) (*float64, time.Time) {
85-
if s.CPU == nil {
86-
return nil, time.Time{}
87-
}
88-
v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
89-
return &v, s.CPU.Time.Time
90-
}
91-
92-
// getContainerMemoryMetrics returns memory utilization of a container.
93-
func getContainerMemoryMetrics(s summary.ContainerStats) (*float64, time.Time) {
94-
if s.Memory == nil {
95-
return nil, time.Time{}
96-
}
97-
v := float64(*s.Memory.WorkingSetBytes)
98-
return &v, s.Memory.Time.Time
99-
}
100-
101-
// Config is the v1alpha1 resource metrics definition
102-
func Config() stats.ResourceMetricsConfig {
103-
return stats.ResourceMetricsConfig{
104-
NodeMetrics: []stats.NodeResourceMetric{
105-
{
106-
Desc: nodeCPUUsageDesc,
107-
ValueFn: getNodeCPUMetrics,
108-
},
109-
{
110-
Desc: nodeMemoryUsageDesc,
111-
ValueFn: getNodeMemoryMetrics,
112-
},
113-
},
114-
ContainerMetrics: []stats.ContainerResourceMetric{
115-
{
116-
Desc: containerCPUUsageDesc,
117-
ValueFn: getContainerCPUMetrics,
118-
},
119-
{
120-
Desc: containerMemoryUsageDesc,
121-
ValueFn: getContainerMemoryMetrics,
122-
},
123-
},
124-
}
125-
}

pkg/kubelet/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (s *Server) InstallDefaultHandlers(enableCAdvisorJSONEndpoints bool) {
350350
// deprecated endpoint which will be removed in release 1.20.0+.
351351
s.addMetricsBucketMatcher("metrics/resource/v1alpha1")
352352
v1alpha1ResourceRegistry := compbasemetrics.NewKubeRegistry()
353-
v1alpha1ResourceRegistry.CustomMustRegister(stats.NewPrometheusResourceMetricCollector(s.resourceAnalyzer, v1alpha1.Config()))
353+
v1alpha1ResourceRegistry.CustomMustRegister(stats.NewPrometheusResourceMetricCollector(s.resourceAnalyzer, stats.Config()))
354354
s.restfulCont.Handle(path.Join(resourceMetricsPath, v1alpha1.Version),
355355
compbasemetrics.HandlerFor(v1alpha1ResourceRegistry, compbasemetrics.HandlerOpts{ErrorHandling: compbasemetrics.ContinueOnError}),
356356
)

pkg/kubelet/server/stats/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ go_test(
5050
"//staging/src/k8s.io/api/core/v1:go_default_library",
5151
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
5252
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
53-
"//staging/src/k8s.io/component-base/metrics:go_default_library",
5453
"//staging/src/k8s.io/component-base/metrics/testutil:go_default_library",
5554
"//vendor/github.com/stretchr/testify/assert:go_default_library",
5655
"//vendor/github.com/stretchr/testify/mock:go_default_library",

pkg/kubelet/server/stats/prometheus_resource_metrics.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,77 @@ import (
2424
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
2525
)
2626

27+
// This file contains a series of deprecated metrics which we emit them by endpoint `/metrics/resource/v1alpha1`.
28+
// These metrics have been adapted to new endpoint `/metrics/resource` as well as new `Desc`s.
29+
// In general, we don't need to maintain these deprecated metrics any more.
30+
// TODO(RainbowMango): Remove this file in release 1.20.0+.
31+
32+
var (
33+
nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total",
34+
"Cumulative cpu time consumed by the node in core-seconds",
35+
nil,
36+
nil,
37+
metrics.ALPHA,
38+
"1.18.0")
39+
40+
nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes",
41+
"Current working set of the node in bytes",
42+
nil,
43+
nil,
44+
metrics.ALPHA,
45+
"1.18.0")
46+
47+
containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total",
48+
"Cumulative cpu time consumed by the container in core-seconds",
49+
[]string{"container", "pod", "namespace"},
50+
nil,
51+
metrics.ALPHA,
52+
"1.18.0")
53+
54+
containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes",
55+
"Current working set of the container in bytes",
56+
[]string{"container", "pod", "namespace"},
57+
nil,
58+
metrics.ALPHA,
59+
"1.18.0")
60+
)
61+
62+
// getNodeCPUMetrics returns CPU utilization of a node.
63+
func getNodeCPUMetrics(s stats.NodeStats) (*float64, time.Time) {
64+
if s.CPU == nil {
65+
return nil, time.Time{}
66+
}
67+
v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
68+
return &v, s.CPU.Time.Time
69+
}
70+
71+
// getNodeMemoryMetrics returns memory utilization of a node.
72+
func getNodeMemoryMetrics(s stats.NodeStats) (*float64, time.Time) {
73+
if s.Memory == nil {
74+
return nil, time.Time{}
75+
}
76+
v := float64(*s.Memory.WorkingSetBytes)
77+
return &v, s.Memory.Time.Time
78+
}
79+
80+
// getContainerCPUMetrics returns CPU utilization of a container.
81+
func getContainerCPUMetrics(s stats.ContainerStats) (*float64, time.Time) {
82+
if s.CPU == nil {
83+
return nil, time.Time{}
84+
}
85+
v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second)
86+
return &v, s.CPU.Time.Time
87+
}
88+
89+
// getContainerMemoryMetrics returns memory utilization of a container.
90+
func getContainerMemoryMetrics(s stats.ContainerStats) (*float64, time.Time) {
91+
if s.Memory == nil {
92+
return nil, time.Time{}
93+
}
94+
v := float64(*s.Memory.WorkingSetBytes)
95+
return &v, s.Memory.Time.Time
96+
}
97+
2798
// NodeResourceMetric describes a metric for the node
2899
type NodeResourceMetric struct {
29100
Desc *metrics.Desc
@@ -50,6 +121,32 @@ type ResourceMetricsConfig struct {
50121
ContainerMetrics []ContainerResourceMetric
51122
}
52123

124+
// Config is the v1alpha1 resource metrics definition
125+
func Config() ResourceMetricsConfig {
126+
return ResourceMetricsConfig{
127+
NodeMetrics: []NodeResourceMetric{
128+
{
129+
Desc: nodeCPUUsageDesc,
130+
ValueFn: getNodeCPUMetrics,
131+
},
132+
{
133+
Desc: nodeMemoryUsageDesc,
134+
ValueFn: getNodeMemoryMetrics,
135+
},
136+
},
137+
ContainerMetrics: []ContainerResourceMetric{
138+
{
139+
Desc: containerCPUUsageDesc,
140+
ValueFn: getContainerCPUMetrics,
141+
},
142+
{
143+
Desc: containerMemoryUsageDesc,
144+
ValueFn: getContainerMemoryMetrics,
145+
},
146+
},
147+
}
148+
}
149+
53150
// NewPrometheusResourceMetricCollector returns a metrics.StableCollector which exports resource metrics
54151
func NewPrometheusResourceMetricCollector(provider SummaryProvider, config ResourceMetricsConfig) metrics.StableCollector {
55152
return &resourceMetricCollector{

0 commit comments

Comments
 (0)