diff --git a/pkg/kubelet/apis/resourcemetrics/v1alpha1/BUILD b/pkg/kubelet/apis/resourcemetrics/v1alpha1/BUILD index fc194ac2ba1..97ae7c573e0 100644 --- a/pkg/kubelet/apis/resourcemetrics/v1alpha1/BUILD +++ b/pkg/kubelet/apis/resourcemetrics/v1alpha1/BUILD @@ -5,11 +5,6 @@ go_library( srcs = ["config.go"], importpath = "k8s.io/kubernetes/pkg/kubelet/apis/resourcemetrics/v1alpha1", visibility = ["//visibility:public"], - deps = [ - "//pkg/kubelet/apis/stats/v1alpha1:go_default_library", - "//pkg/kubelet/server/stats:go_default_library", - "//staging/src/k8s.io/component-base/metrics:go_default_library", - ], ) filegroup( diff --git a/pkg/kubelet/apis/resourcemetrics/v1alpha1/config.go b/pkg/kubelet/apis/resourcemetrics/v1alpha1/config.go index 54d6fc21daa..6f0799cfa28 100644 --- a/pkg/kubelet/apis/resourcemetrics/v1alpha1/config.go +++ b/pkg/kubelet/apis/resourcemetrics/v1alpha1/config.go @@ -16,110 +16,8 @@ limitations under the License. package v1alpha1 -import ( - "time" - - "k8s.io/component-base/metrics" - summary "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" - "k8s.io/kubernetes/pkg/kubelet/server/stats" -) - -// This file contains a series of deprecated metrics which we emit them by endpoint `/metrics/resource/v1alpha1`. -// These metrics have been adapted to new endpoint `/metrics/resource` as well as new `Desc`s. -// In general, we don't need to maintain these deprecated metrics any more. -// TODO(RainbowMango): Remove this file in release 1.20.0+. +// TODO(RainbowMango): We don't need to maintain this package anymore. +// This package will be remove in release 1.20.0+. More details please refer to https://github.com/kubernetes/kubernetes/pull/86282. // Version is the string representation of the version of this configuration const Version = "v1alpha1" - -var ( - nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total", - "Cumulative cpu time consumed by the node in core-seconds", - nil, - nil, - metrics.ALPHA, - "1.18.0") - - nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes", - "Current working set of the node in bytes", - nil, - nil, - metrics.ALPHA, - "1.18.0") - - containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total", - "Cumulative cpu time consumed by the container in core-seconds", - []string{"container", "pod", "namespace"}, - nil, - metrics.ALPHA, - "1.18.0") - - containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes", - "Current working set of the container in bytes", - []string{"container", "pod", "namespace"}, - nil, - metrics.ALPHA, - "1.18.0") -) - -// getNodeCPUMetrics returns CPU utilization of a node. -func getNodeCPUMetrics(s summary.NodeStats) (*float64, time.Time) { - if s.CPU == nil { - return nil, time.Time{} - } - v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) - return &v, s.CPU.Time.Time -} - -// getNodeMemoryMetrics returns memory utilization of a node. -func getNodeMemoryMetrics(s summary.NodeStats) (*float64, time.Time) { - if s.Memory == nil { - return nil, time.Time{} - } - v := float64(*s.Memory.WorkingSetBytes) - return &v, s.Memory.Time.Time -} - -// getContainerCPUMetrics returns CPU utilization of a container. -func getContainerCPUMetrics(s summary.ContainerStats) (*float64, time.Time) { - if s.CPU == nil { - return nil, time.Time{} - } - v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) - return &v, s.CPU.Time.Time -} - -// getContainerMemoryMetrics returns memory utilization of a container. -func getContainerMemoryMetrics(s summary.ContainerStats) (*float64, time.Time) { - if s.Memory == nil { - return nil, time.Time{} - } - v := float64(*s.Memory.WorkingSetBytes) - return &v, s.Memory.Time.Time -} - -// Config is the v1alpha1 resource metrics definition -func Config() stats.ResourceMetricsConfig { - return stats.ResourceMetricsConfig{ - NodeMetrics: []stats.NodeResourceMetric{ - { - Desc: nodeCPUUsageDesc, - ValueFn: getNodeCPUMetrics, - }, - { - Desc: nodeMemoryUsageDesc, - ValueFn: getNodeMemoryMetrics, - }, - }, - ContainerMetrics: []stats.ContainerResourceMetric{ - { - Desc: containerCPUUsageDesc, - ValueFn: getContainerCPUMetrics, - }, - { - Desc: containerMemoryUsageDesc, - ValueFn: getContainerMemoryMetrics, - }, - }, - } -} diff --git a/pkg/kubelet/server/server.go b/pkg/kubelet/server/server.go index d6c05866b4d..3dca19d2c0b 100644 --- a/pkg/kubelet/server/server.go +++ b/pkg/kubelet/server/server.go @@ -350,7 +350,7 @@ func (s *Server) InstallDefaultHandlers(enableCAdvisorJSONEndpoints bool) { // deprecated endpoint which will be removed in release 1.20.0+. s.addMetricsBucketMatcher("metrics/resource/v1alpha1") v1alpha1ResourceRegistry := compbasemetrics.NewKubeRegistry() - v1alpha1ResourceRegistry.CustomMustRegister(stats.NewPrometheusResourceMetricCollector(s.resourceAnalyzer, v1alpha1.Config())) + v1alpha1ResourceRegistry.CustomMustRegister(stats.NewPrometheusResourceMetricCollector(s.resourceAnalyzer, stats.Config())) s.restfulCont.Handle(path.Join(resourceMetricsPath, v1alpha1.Version), compbasemetrics.HandlerFor(v1alpha1ResourceRegistry, compbasemetrics.HandlerOpts{ErrorHandling: compbasemetrics.ContinueOnError}), ) diff --git a/pkg/kubelet/server/stats/BUILD b/pkg/kubelet/server/stats/BUILD index 49851893f53..446da5668b4 100644 --- a/pkg/kubelet/server/stats/BUILD +++ b/pkg/kubelet/server/stats/BUILD @@ -50,7 +50,6 @@ go_test( "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", - "//staging/src/k8s.io/component-base/metrics:go_default_library", "//staging/src/k8s.io/component-base/metrics/testutil:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/github.com/stretchr/testify/mock:go_default_library", diff --git a/pkg/kubelet/server/stats/prometheus_resource_metrics.go b/pkg/kubelet/server/stats/prometheus_resource_metrics.go index 7d0c76fc929..2502f094ee6 100644 --- a/pkg/kubelet/server/stats/prometheus_resource_metrics.go +++ b/pkg/kubelet/server/stats/prometheus_resource_metrics.go @@ -24,6 +24,77 @@ import ( stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" ) +// This file contains a series of deprecated metrics which we emit them by endpoint `/metrics/resource/v1alpha1`. +// These metrics have been adapted to new endpoint `/metrics/resource` as well as new `Desc`s. +// In general, we don't need to maintain these deprecated metrics any more. +// TODO(RainbowMango): Remove this file in release 1.20.0+. + +var ( + nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total", + "Cumulative cpu time consumed by the node in core-seconds", + nil, + nil, + metrics.ALPHA, + "1.18.0") + + nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes", + "Current working set of the node in bytes", + nil, + nil, + metrics.ALPHA, + "1.18.0") + + containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total", + "Cumulative cpu time consumed by the container in core-seconds", + []string{"container", "pod", "namespace"}, + nil, + metrics.ALPHA, + "1.18.0") + + containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes", + "Current working set of the container in bytes", + []string{"container", "pod", "namespace"}, + nil, + metrics.ALPHA, + "1.18.0") +) + +// getNodeCPUMetrics returns CPU utilization of a node. +func getNodeCPUMetrics(s stats.NodeStats) (*float64, time.Time) { + if s.CPU == nil { + return nil, time.Time{} + } + v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) + return &v, s.CPU.Time.Time +} + +// getNodeMemoryMetrics returns memory utilization of a node. +func getNodeMemoryMetrics(s stats.NodeStats) (*float64, time.Time) { + if s.Memory == nil { + return nil, time.Time{} + } + v := float64(*s.Memory.WorkingSetBytes) + return &v, s.Memory.Time.Time +} + +// getContainerCPUMetrics returns CPU utilization of a container. +func getContainerCPUMetrics(s stats.ContainerStats) (*float64, time.Time) { + if s.CPU == nil { + return nil, time.Time{} + } + v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) + return &v, s.CPU.Time.Time +} + +// getContainerMemoryMetrics returns memory utilization of a container. +func getContainerMemoryMetrics(s stats.ContainerStats) (*float64, time.Time) { + if s.Memory == nil { + return nil, time.Time{} + } + v := float64(*s.Memory.WorkingSetBytes) + return &v, s.Memory.Time.Time +} + // NodeResourceMetric describes a metric for the node type NodeResourceMetric struct { Desc *metrics.Desc @@ -50,6 +121,32 @@ type ResourceMetricsConfig struct { ContainerMetrics []ContainerResourceMetric } +// Config is the v1alpha1 resource metrics definition +func Config() ResourceMetricsConfig { + return ResourceMetricsConfig{ + NodeMetrics: []NodeResourceMetric{ + { + Desc: nodeCPUUsageDesc, + ValueFn: getNodeCPUMetrics, + }, + { + Desc: nodeMemoryUsageDesc, + ValueFn: getNodeMemoryMetrics, + }, + }, + ContainerMetrics: []ContainerResourceMetric{ + { + Desc: containerCPUUsageDesc, + ValueFn: getContainerCPUMetrics, + }, + { + Desc: containerMemoryUsageDesc, + ValueFn: getContainerMemoryMetrics, + }, + }, + } +} + // NewPrometheusResourceMetricCollector returns a metrics.StableCollector which exports resource metrics func NewPrometheusResourceMetricCollector(provider SummaryProvider, config ResourceMetricsConfig) metrics.StableCollector { return &resourceMetricCollector{ diff --git a/pkg/kubelet/server/stats/prometheus_resource_metrics_test.go b/pkg/kubelet/server/stats/prometheus_resource_metrics_test.go index ba2b235f7ec..3b88c511a23 100644 --- a/pkg/kubelet/server/stats/prometheus_resource_metrics_test.go +++ b/pkg/kubelet/server/stats/prometheus_resource_metrics_test.go @@ -25,107 +25,10 @@ import ( "github.com/stretchr/testify/mock" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/component-base/metrics" "k8s.io/component-base/metrics/testutil" statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" - summary "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" ) -// TODO(RainbowMango): The Desc variables and value functions should be shared with source code. -// It can not be shared now because there is a import cycle. -// Consider deprecate endpoint `/resource/v1alpha1` as stability framework could offer guarantee now. -var ( - nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total", - "Cumulative cpu time consumed by the node in core-seconds", - nil, - nil, - metrics.ALPHA, - "") - - nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes", - "Current working set of the node in bytes", - nil, - nil, - metrics.ALPHA, - "") - - containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total", - "Cumulative cpu time consumed by the container in core-seconds", - []string{"container", "pod", "namespace"}, - nil, - metrics.ALPHA, - "") - - containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes", - "Current working set of the container in bytes", - []string{"container", "pod", "namespace"}, - nil, - metrics.ALPHA, - "") -) - -// getNodeCPUMetrics returns CPU utilization of a node. -func getNodeCPUMetrics(s summary.NodeStats) (*float64, time.Time) { - if s.CPU == nil { - return nil, time.Time{} - } - v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) - return &v, s.CPU.Time.Time -} - -// getNodeMemoryMetrics returns memory utilization of a node. -func getNodeMemoryMetrics(s summary.NodeStats) (*float64, time.Time) { - if s.Memory == nil { - return nil, time.Time{} - } - v := float64(*s.Memory.WorkingSetBytes) - return &v, s.Memory.Time.Time -} - -// getContainerCPUMetrics returns CPU utilization of a container. -func getContainerCPUMetrics(s summary.ContainerStats) (*float64, time.Time) { - if s.CPU == nil { - return nil, time.Time{} - } - v := float64(*s.CPU.UsageCoreNanoSeconds) / float64(time.Second) - return &v, s.CPU.Time.Time -} - -// getContainerMemoryMetrics returns memory utilization of a container. -func getContainerMemoryMetrics(s summary.ContainerStats) (*float64, time.Time) { - if s.Memory == nil { - return nil, time.Time{} - } - v := float64(*s.Memory.WorkingSetBytes) - return &v, s.Memory.Time.Time -} - -// Config is the v1alpha1 resource metrics definition -func Config() ResourceMetricsConfig { - return ResourceMetricsConfig{ - NodeMetrics: []NodeResourceMetric{ - { - Desc: nodeCPUUsageDesc, - ValueFn: getNodeCPUMetrics, - }, - { - Desc: nodeMemoryUsageDesc, - ValueFn: getNodeMemoryMetrics, - }, - }, - ContainerMetrics: []ContainerResourceMetric{ - { - Desc: containerCPUUsageDesc, - ValueFn: getContainerCPUMetrics, - }, - { - Desc: containerMemoryUsageDesc, - ValueFn: getContainerMemoryMetrics, - }, - }, - } -} - type mockSummaryProvider struct { mock.Mock } @@ -158,7 +61,7 @@ func TestCollectResourceMetrics(t *testing.T) { summaryErr: fmt.Errorf("failed to get summary"), expectedMetricsNames: []string{"scrape_error"}, expectedMetrics: ` - # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise + # HELP scrape_error [ALPHA] (Deprecated since 1.18.0) 1 if there was an error while getting container metrics, 0 otherwise # TYPE scrape_error gauge scrape_error 1 `, @@ -185,13 +88,13 @@ func TestCollectResourceMetrics(t *testing.T) { "scrape_error", }, expectedMetrics: ` - # HELP node_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the node in core-seconds + # HELP node_cpu_usage_seconds_total [ALPHA] (Deprecated since 1.18.0) Cumulative cpu time consumed by the node in core-seconds # TYPE node_cpu_usage_seconds_total gauge node_cpu_usage_seconds_total 10 2000 - # HELP node_memory_working_set_bytes [ALPHA] Current working set of the node in bytes + # HELP node_memory_working_set_bytes [ALPHA] (Deprecated since 1.18.0) Current working set of the node in bytes # TYPE node_memory_working_set_bytes gauge node_memory_working_set_bytes 1000 2000 - # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise + # HELP scrape_error [ALPHA] (Deprecated since 1.18.0) 1 if there was an error while getting container metrics, 0 otherwise # TYPE scrape_error gauge scrape_error 0 `, @@ -259,15 +162,15 @@ func TestCollectResourceMetrics(t *testing.T) { "scrape_error", }, expectedMetrics: ` - # HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise + # HELP scrape_error [ALPHA] (Deprecated since 1.18.0) 1 if there was an error while getting container metrics, 0 otherwise # TYPE scrape_error gauge scrape_error 0 - # HELP container_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the container in core-seconds + # HELP container_cpu_usage_seconds_total [ALPHA] (Deprecated since 1.18.0) Cumulative cpu time consumed by the container in core-seconds # TYPE container_cpu_usage_seconds_total gauge container_cpu_usage_seconds_total{container="container_a",namespace="namespace_a",pod="pod_a"} 10 2000 container_cpu_usage_seconds_total{container="container_a",namespace="namespace_b",pod="pod_b"} 10 2000 container_cpu_usage_seconds_total{container="container_b",namespace="namespace_a",pod="pod_a"} 10 2000 - # HELP container_memory_working_set_bytes [ALPHA] Current working set of the container in bytes + # HELP container_memory_working_set_bytes [ALPHA] (Deprecated since 1.18.0) Current working set of the container in bytes # TYPE container_memory_working_set_bytes gauge container_memory_working_set_bytes{container="container_a",namespace="namespace_a",pod="pod_a"} 1000 2000 container_memory_working_set_bytes{container="container_a",namespace="namespace_b",pod="pod_b"} 1000 2000 @@ -283,7 +186,9 @@ func TestCollectResourceMetrics(t *testing.T) { provider.On("GetCPUAndMemoryStats").Return(tc.summary, tc.summaryErr) collector := NewPrometheusResourceMetricCollector(provider, tc.config) - if err := testutil.CustomCollectAndCompare(collector, strings.NewReader(tc.expectedMetrics), tc.expectedMetricsNames...); err != nil { + registry := testutil.NewFakeKubeRegistry("1.18.0") + registry.CustomMustRegister(collector) + if err := testutil.GatherAndCompare(registry, strings.NewReader(tc.expectedMetrics), tc.expectedMetricsNames...); err != nil { t.Fatal(err) } })