Skip to content

Commit f8ac539

Browse files
authored
Merge pull request #8071 from laoj2/add-fallback-metric
Add metric to track calls to get container/pod resources
2 parents 5d95cfd + 27e1d0f commit f8ac539

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

vertical-pod-autoscaler/pkg/recommender/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics"
5353
metrics_quality "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/quality"
5454
metrics_recommender "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/recommender"
55+
metrics_resources "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/resources"
5556
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/server"
5657
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
5758
)
@@ -143,6 +144,7 @@ func main() {
143144
healthCheck := metrics.NewHealthCheck(*metricsFetcherInterval * 5)
144145
metrics_recommender.Register()
145146
metrics_quality.Register()
147+
metrics_resources.Register()
146148
server.Initialize(&commonFlags.EnableProfiling, healthCheck, address)
147149

148150
if !leaderElection.LeaderElect {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package resources contains metrics for the VPA resource helper functions.
18+
package resources
19+
20+
import (
21+
"github.com/prometheus/client_golang/prometheus"
22+
23+
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics"
24+
)
25+
26+
const (
27+
metricsNamespace = metrics.TopMetricsNamespace + "resources"
28+
)
29+
30+
type resourcesSource string
31+
32+
const (
33+
// ContainerStatus indicates that resources were fetched from the
34+
// containerStatuses pod field.
35+
ContainerStatus resourcesSource = "Pod.Status.ContainerStatuses"
36+
// InitContainerStatus indicates that resources were fetched from the
37+
// initContainerStatuses pod field.
38+
InitContainerStatus resourcesSource = "Pod.Status.InitContainerStatuses"
39+
// PodSpecContainer indicates that resources were fetched from the
40+
// containers field in the pod spec.
41+
PodSpecContainer resourcesSource = "Pod.Spec.Containers"
42+
// PodSpecInitContainer indicates that resources were fetched from the
43+
// initContainers field in the pod spec.
44+
PodSpecInitContainer resourcesSource = "Pod.Spec.InitContainers"
45+
)
46+
47+
var (
48+
getResourcesCount = prometheus.NewCounterVec(
49+
prometheus.CounterOpts{
50+
Namespace: metricsNamespace,
51+
Name: "get_resources_count",
52+
Help: "Count of calls to get the resources of a pod or container.",
53+
}, []string{"source"},
54+
)
55+
)
56+
57+
// Register initializes all metrics for VPA resources
58+
func Register() {
59+
prometheus.MustRegister(getResourcesCount)
60+
}
61+
62+
// RecordGetResourcesCount records how many times VPA requested the resources (
63+
// CPU/memory requests and limits) of a pod or container by the data source.
64+
func RecordGetResourcesCount(source resourcesSource) {
65+
getResourcesCount.WithLabelValues(string(source)).Inc()
66+
}

vertical-pod-autoscaler/pkg/utils/resources/resourcehelpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package resourcehelpers
1919
import (
2020
v1 "k8s.io/api/core/v1"
2121
"k8s.io/klog/v2"
22+
23+
metrics_resources "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/resources"
2224
)
2325

2426
// ContainerRequestsAndLimits returns a copy of the actual resource requests and
@@ -32,12 +34,14 @@ import (
3234
func ContainerRequestsAndLimits(containerName string, pod *v1.Pod) (v1.ResourceList, v1.ResourceList) {
3335
cs := containerStatusFor(containerName, pod)
3436
if cs != nil && cs.Resources != nil {
37+
metrics_resources.RecordGetResourcesCount(metrics_resources.ContainerStatus)
3538
return cs.Resources.Requests.DeepCopy(), cs.Resources.Limits.DeepCopy()
3639
}
3740

3841
klog.V(6).InfoS("Container resources not found in containerStatus for container. Falling back to resources defined in the pod spec. This is expected for clusters with in-place pod updates feature disabled.", "container", containerName, "containerStatus", cs)
3942
container := findContainer(containerName, pod)
4043
if container != nil {
44+
metrics_resources.RecordGetResourcesCount(metrics_resources.PodSpecContainer)
4145
return container.Resources.Requests.DeepCopy(), container.Resources.Limits.DeepCopy()
4246
}
4347

@@ -55,12 +59,14 @@ func ContainerRequestsAndLimits(containerName string, pod *v1.Pod) (v1.ResourceL
5559
func InitContainerRequestsAndLimits(initContainerName string, pod *v1.Pod) (v1.ResourceList, v1.ResourceList) {
5660
cs := initContainerStatusFor(initContainerName, pod)
5761
if cs != nil && cs.Resources != nil {
62+
metrics_resources.RecordGetResourcesCount(metrics_resources.InitContainerStatus)
5863
return cs.Resources.Requests.DeepCopy(), cs.Resources.Limits.DeepCopy()
5964
}
6065

6166
klog.V(6).InfoS("initContainer resources not found in initContainerStatus for initContainer. Falling back to resources defined in the pod spec. This is expected for clusters with in-place pod updates feature disabled.", "initContainer", initContainerName, "initContainerStatus", cs)
6267
initContainer := findInitContainer(initContainerName, pod)
6368
if initContainer != nil {
69+
metrics_resources.RecordGetResourcesCount(metrics_resources.PodSpecInitContainer)
6470
return initContainer.Resources.Requests.DeepCopy(), initContainer.Resources.Limits.DeepCopy()
6571
}
6672

0 commit comments

Comments
 (0)