Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Per group of metrics there is one file for each metrics. See each file for speci
- [ConfigMap Metrics](configmap-metrics.md)
- [Ingress Metrics](ingress-metrics.md)
- [CertificateSigningRequest Metrics](certificatessigningrequest-metrics.md)
- [VerticalPodAutoscaler Metrics](verticalpodautoscaler-metrics.md)

## Join Metrics

Expand Down
12 changes: 12 additions & 0 deletions docs/verticalpodautoscaler-metrics.md
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`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_spec_resourcepolicy_container_policies_maxallowed | Gauge | `container`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_lowerbound | Gauge | `container`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_target | Gauge | `container`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_uncappedtarget | Gauge | `container`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_status_recommendation_containerrecommendations_upperbound | Gauge | `container`=&lt;container name&gt; <br> `namespace`=&lt;namespace&gt; <br> `resource`=&lt;cpu | memory&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `unit`=&lt;core | byte&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_labels | Gauge | `label_app`=&lt;foo&gt; <br> `namespace`=&lt;namespace&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
| kube_verticalpodautoscaler_spec_updatepolicy_updatemode | Gauge | `namespace`=&lt;namespace&gt; <br> `target_api_version`=&lt;api version&gt; <br> `target_kind`=&lt;target kind&gt; <br> `target_name`=&lt;target name&gt; <br> `update_mode`=&lt;foo&gt; <br> `verticalpodautoscaler`=&lt;vertical pod autoscaler name&gt; | EXPERIMENTAL |
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
gopkg.in/inf.v0 v0.9.0 // indirect
k8s.io/api v0.0.0-20190313235455-40a48860b5ab
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
k8s.io/autoscaler v0.0.0-20190612121530-8bb2354d9af9
k8s.io/client-go v2.0.0-alpha.0.0.20190313235726-6ee68ca5fd83+incompatible
k8s.io/klog v0.3.0
k8s.io/kube-openapi v0.0.0-20190320154901-5e45bb682580 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ k8s.io/api v0.0.0-20190313235455-40a48860b5ab h1:DG9A67baNpoeweOy2spF1OWHhnVY5KR
k8s.io/api v0.0.0-20190313235455-40a48860b5ab/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1 h1:IS7K02iBkQXpCeieSiyJjGoLSdVOv2DbPaWHJ+ZtgKg=
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/autoscaler v0.0.0-20190612121530-8bb2354d9af9 h1:+GB3VH3tw7T8PZw4RPa9XLmYdqTnSB3qa/Zern1bhOk=
k8s.io/autoscaler v0.0.0-20190612121530-8bb2354d9af9/go.mod h1:QEXezc9uKPT91dwqhSJq3GNI3B1HxFRQHiku9kmrsSA=
k8s.io/client-go v2.0.0-alpha.0.0.20190313235726-6ee68ca5fd83+incompatible h1:+SiikveGdttGGsPhWZsGg+RD1ziNvr+PL8zKqLDIrbE=
k8s.io/client-go v2.0.0-alpha.0.0.20190313235726-6ee68ca5fd83+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/klog v0.3.0 h1:0VPpR+sizsiivjIfIAQH/rl8tan6jvWkS7lU+0di3lE=
Expand Down
13 changes: 13 additions & 0 deletions internal/store/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
extensions "k8s.io/api/extensions/v1beta1"
policy "k8s.io/api/policy/v1beta1"
storagev1 "k8s.io/api/storage/v1"
vpaautoscaling "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"
Expand All @@ -49,6 +51,7 @@ type whiteBlackLister interface {
// (https://en.wikipedia.org/wiki/Builder_pattern).
type Builder struct {
kubeClient clientset.Interface
vpaClient vpaclientset.Interface
namespaces options.NamespaceList
ctx context.Context
enabledResources []string
Expand Down Expand Up @@ -91,6 +94,11 @@ func (b *Builder) WithKubeClient(c clientset.Interface) {
b.kubeClient = c
}

// WithVPAClient sets the vpaClient property of a Builder so that the verticalpodautoscaler collector can query VPA objects.
func (b *Builder) WithVPAClient(c vpaclientset.Interface) {
b.vpaClient = c
}

// WithWhiteBlackList configures the white or blacklisted metric to be exposed
// by the store build by the Builder.
func (b *Builder) WithWhiteBlackList(l whiteBlackLister) {
Expand Down Expand Up @@ -144,6 +152,7 @@ var availableStores = map[string]func(f *Builder) *metricsstore.MetricsStore{
"services": func(b *Builder) *metricsstore.MetricsStore { return b.buildServiceStore() },
"statefulsets": func(b *Builder) *metricsstore.MetricsStore { return b.buildStatefulSetStore() },
"storageclasses": func(b *Builder) *metricsstore.MetricsStore { return b.buildStorageClassStore() },
"verticalpodautoscalers": func(b *Builder) *metricsstore.MetricsStore { return b.buildVPAStore() },
}

func collectorExists(name string) bool {
Expand Down Expand Up @@ -251,6 +260,10 @@ func (b *Builder) buildCsrStore() *metricsstore.MetricsStore {
return b.buildStore(csrMetricFamilies, &certv1beta1.CertificateSigningRequest{}, createCSRListWatch)
}

func (b *Builder) buildVPAStore() *metricsstore.MetricsStore {
return b.buildStore(vpaMetricFamilies, &vpaautoscaling.VerticalPodAutoscaler{}, createVPAListWatchFunc(b.vpaClient))
}

func (b *Builder) buildStore(
metricFamilies []metric.FamilyGenerator,
expectedType interface{},
Expand Down
256 changes: 256 additions & 0 deletions internal/store/verticalpodautoscaler.go
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:
Copy link
Member

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?

Copy link
Contributor Author

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 the kube_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 the resource.Quantity only provides an int 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 this

Copy link
Member

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.

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)
},
}
}
}
Loading