-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
store: add metrics for VerticalPodAutoscaler objects #791
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
monzo:vertical-pod-autoscaler-metrics
Jun 20, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Vertical Pod Autoscaler Metrics | ||
|
||
| Metric name | Metric type | Labels/tags | Status | | ||
| -------------------------------- | ----------- | ------------------------------------------------------------- | ------ | | ||
| kube_verticalpodautoscaler_spec_resourcepolicy_container_policies_minallowed | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_spec_resourcepolicy_container_policies_maxallowed | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_lowerbound | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_target | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_uncappedtarget | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_upperbound | Gauge | `container`=<container name> <br> `namespace`=<namespace> <br> `resource`=<cpu | memory> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `unit`=<core | byte> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_labels | Gauge | `label_app`=<foo> <br> `namespace`=<namespace> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | | ||
| kube_verticalpodautoscaler_spec_updatepolicy_updatemode | Gauge | `namespace`=<namespace> <br> `target_api_version`=<api version> <br> `target_kind`=<target kind> <br> `target_name`=<target name> <br> `update_mode`=<foo> <br> `verticalpodautoscaler`=<vertical pod autoscaler name> | EXPERIMENTAL | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/kube-state-metrics/pkg/constant" | ||
|
||
autoscaling "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1beta2" | ||
vpaclientset "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/clientset/versioned" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/kube-state-metrics/pkg/metric" | ||
) | ||
|
||
var ( | ||
descVerticalPodAutoscalerLabelsName = "kube_verticalpodautoscaler_labels" | ||
descVerticalPodAutoscalerLabelsHelp = "Kubernetes labels converted to Prometheus labels." | ||
descVerticalPodAutoscalerLabelsDefaultLabels = []string{"namespace", "verticalpodautoscaler", "target_api_version", "target_kind", "target_name"} | ||
|
||
vpaMetricFamilies = []metric.FamilyGenerator{ | ||
{ | ||
Name: descVerticalPodAutoscalerLabelsName, | ||
Type: metric.Gauge, | ||
Help: descVerticalPodAutoscalerLabelsHelp, | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
labelKeys, labelValues := kubeLabelsToPrometheusLabels(a.Labels) | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: labelKeys, | ||
LabelValues: labelValues, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_spec_updatepolicy_updatemode", | ||
Type: metric.Gauge, | ||
Help: "Update mode of the VerticalPodAutoscaler.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
|
||
if a.Spec.UpdatePolicy.UpdateMode == nil { | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
} | ||
|
||
for _, mode := range []autoscaling.UpdateMode{ | ||
autoscaling.UpdateModeOff, | ||
autoscaling.UpdateModeInitial, | ||
autoscaling.UpdateModeRecreate, | ||
autoscaling.UpdateModeAuto, | ||
} { | ||
var v float64 | ||
if *a.Spec.UpdatePolicy.UpdateMode == mode { | ||
v = 1 | ||
} else { | ||
v = 0 | ||
} | ||
ms = append(ms, &metric.Metric{ | ||
LabelKeys: []string{"update_mode"}, | ||
LabelValues: []string{string(mode)}, | ||
Value: v, | ||
}) | ||
} | ||
|
||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_spec_resourcepolicy_container_policies_minallowed", | ||
Type: metric.Gauge, | ||
Help: "Minimum resources the VerticalPodAutoscaler can set for containers matching the name.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
for _, c := range a.Spec.ResourcePolicy.ContainerPolicies { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.MinAllowed)...) | ||
|
||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_spec_resourcepolicy_container_policies_maxallowed", | ||
Type: metric.Gauge, | ||
Help: "Maximum resources the VerticalPodAutoscaler can set for containers matching the name.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
for _, c := range a.Spec.ResourcePolicy.ContainerPolicies { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.MaxAllowed)...) | ||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_status_recommendation_containerrecommendations_lowerbound", | ||
Type: metric.Gauge, | ||
Help: "Minimum resources the container can use before the VerticalPodAutoscaler updater evicts it.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
if a.Status.Recommendation == nil || a.Status.Recommendation.ContainerRecommendations == nil { | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
} | ||
|
||
for _, c := range a.Status.Recommendation.ContainerRecommendations { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.LowerBound)...) | ||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_status_recommendation_containerrecommendations_upperbound", | ||
Type: metric.Gauge, | ||
Help: "Maximum resources the container can use before the VerticalPodAutoscaler updater evicts it.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
if a.Status.Recommendation == nil || a.Status.Recommendation.ContainerRecommendations == nil { | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
} | ||
|
||
for _, c := range a.Status.Recommendation.ContainerRecommendations { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.UpperBound)...) | ||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_status_recommendation_containerrecommendations_target", | ||
Type: metric.Gauge, | ||
Help: "Target resources the VerticalPodAutoscaler recommends for the container.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
if a.Status.Recommendation == nil || a.Status.Recommendation.ContainerRecommendations == nil { | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
} | ||
for _, c := range a.Status.Recommendation.ContainerRecommendations { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.Target)...) | ||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_verticalpodautoscaler_status_recommendation_containerrecommendations_uncappedtarget", | ||
Type: metric.Gauge, | ||
Help: "Target resources the VerticalPodAutoscaler recommends for the container ignoring bounds.", | ||
GenerateFunc: wrapVPAFunc(func(a *autoscaling.VerticalPodAutoscaler) *metric.Family { | ||
ms := []*metric.Metric{} | ||
if a.Status.Recommendation == nil || a.Status.Recommendation.ContainerRecommendations == nil { | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
} | ||
for _, c := range a.Status.Recommendation.ContainerRecommendations { | ||
ms = append(ms, vpaResourcesToMetrics(c.ContainerName, c.UncappedTarget)...) | ||
} | ||
return &metric.Family{ | ||
Metrics: ms, | ||
} | ||
}), | ||
}, | ||
} | ||
) | ||
|
||
func vpaResourcesToMetrics(containerName string, resources v1.ResourceList) []*metric.Metric { | ||
ms := []*metric.Metric{} | ||
for resourceName, val := range resources { | ||
switch resourceName { | ||
case v1.ResourceCPU: | ||
ms = append(ms, &metric.Metric{ | ||
LabelValues: []string{containerName, sanitizeLabelName(string(resourceName)), string(constant.UnitCore)}, | ||
Value: float64(val.MilliValue()) / 1000, | ||
}) | ||
case v1.ResourceStorage: | ||
fallthrough | ||
case v1.ResourceEphemeralStorage: | ||
fallthrough | ||
case v1.ResourceMemory: | ||
ms = append(ms, &metric.Metric{ | ||
LabelValues: []string{containerName, sanitizeLabelName(string(resourceName)), string(constant.UnitByte)}, | ||
Value: float64(val.Value()), | ||
}) | ||
} | ||
} | ||
for _, metric := range ms { | ||
metric.LabelKeys = []string{"container", "resource", "unit"} | ||
} | ||
return ms | ||
} | ||
|
||
func wrapVPAFunc(f func(*autoscaling.VerticalPodAutoscaler) *metric.Family) func(interface{}) *metric.Family { | ||
return func(obj interface{}) *metric.Family { | ||
vpa := obj.(*autoscaling.VerticalPodAutoscaler) | ||
|
||
metricFamily := f(vpa) | ||
targetRef := vpa.Spec.TargetRef | ||
|
||
for _, m := range metricFamily.Metrics { | ||
m.LabelKeys = append(descVerticalPodAutoscalerLabelsDefaultLabels, m.LabelKeys...) | ||
m.LabelValues = append([]string{vpa.Namespace, vpa.Name, targetRef.APIVersion, targetRef.Kind, targetRef.Name}, m.LabelValues...) | ||
} | ||
|
||
return metricFamily | ||
} | ||
} | ||
|
||
func createVPAListWatchFunc(vpaClient vpaclientset.Interface) func(kubeClient clientset.Interface, ns string) cache.ListerWatcher { | ||
return func(kubeClient clientset.Interface, ns string) cache.ListerWatcher { | ||
return &cache.ListWatch{ | ||
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { | ||
return vpaClient.AutoscalingV1beta2().VerticalPodAutoscalers(ns).List(opts) | ||
}, | ||
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { | ||
return vpaClient.AutoscalingV1beta2().VerticalPodAutoscalers(ns).Watch(opts) | ||
}, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it really necessary to whitelist each type here? can't we just normalize to base units in any case and put the resource name in the
unit
label?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brancz I'm a little conflicted about this - I'm actually tempted to remove the
Storage
cases (if I'm honest, I copied and pasted this from thekube_pod_container_resource_requests
).At the moment the VPA only operates on CPU and memory, though there's a discussion about adding ephemeral storage (kubernetes/autoscaler#1751). Those are the only metrics exposed by the Metrics Server AFAIK, so it's unlikely we'd need to whitelist every type of resource.
I'm also not sure how we'd generically normalize to base units - I'd consider the base unit of CPU to be a
core
, and theresource.Quantity
only provides anint
Value for that 😢Personally I'm in favour of keeping this consistent with how
kube_pod_container_resource_requests
works, though I don't feel strongly about thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, I was just thinking if we can find a default case similar to
kube_pod_container_resource_requests
. But I'm also happy to just add it when we're actually at a point where this is a reality.