forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpa.go
154 lines (138 loc) · 5.31 KB
/
hpa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2016 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 collectors
import (
"k8s.io/kube-state-metrics/pkg/metrics"
autoscaling "k8s.io/api/autoscaling/v2beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
var (
descHorizontalPodAutoscalerLabelsName = "kube_hpa_labels"
descHorizontalPodAutoscalerLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descHorizontalPodAutoscalerLabelsDefaultLabels = []string{"namespace", "hpa"}
hpaMetricFamilies = []metrics.FamilyGenerator{
metrics.FamilyGenerator{
Name: "kube_hpa_metadata_generation",
Type: metrics.MetricTypeGauge,
Help: "The generation observed by the HorizontalPodAutoscaler controller.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
return metrics.Family{&metrics.Metric{
Name: "kube_hpa_metadata_generation",
Value: float64(a.ObjectMeta.Generation),
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_hpa_spec_max_replicas",
Type: metrics.MetricTypeGauge,
Help: "Upper limit for the number of pods that can be set by the autoscaler; cannot be smaller than MinReplicas.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
return metrics.Family{&metrics.Metric{
Name: "kube_hpa_spec_max_replicas",
Value: float64(a.Spec.MaxReplicas),
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_hpa_spec_min_replicas",
Type: metrics.MetricTypeGauge,
Help: "Lower limit for the number of pods that can be set by the autoscaler, default 1.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
return metrics.Family{&metrics.Metric{
Name: "kube_hpa_spec_min_replicas",
Value: float64(*a.Spec.MinReplicas),
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_hpa_status_current_replicas",
Type: metrics.MetricTypeGauge,
Help: "Current number of replicas of pods managed by this autoscaler.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
return metrics.Family{&metrics.Metric{
Name: "kube_hpa_status_current_replicas",
Value: float64(a.Status.CurrentReplicas),
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_hpa_status_desired_replicas",
Type: metrics.MetricTypeGauge,
Help: "Desired number of replicas of pods managed by this autoscaler.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
return metrics.Family{&metrics.Metric{
Name: "kube_hpa_status_desired_replicas",
Value: float64(a.Status.DesiredReplicas),
}}
}),
},
metrics.FamilyGenerator{
Name: descHorizontalPodAutoscalerLabelsName,
Type: metrics.MetricTypeGauge,
Help: descHorizontalPodAutoscalerLabelsHelp,
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
labelKeys, labelValues := kubeLabelsToPrometheusLabels(a.Labels)
return metrics.Family{&metrics.Metric{
Name: descHorizontalPodAutoscalerLabelsName,
LabelKeys: labelKeys,
LabelValues: labelValues,
Value: 1,
}}
}),
},
metrics.FamilyGenerator{
Name: "kube_hpa_status_condition",
Type: metrics.MetricTypeGauge,
Help: "The condition of this autoscaler.",
GenerateFunc: wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) metrics.Family {
f := metrics.Family{}
for _, c := range a.Status.Conditions {
metrics := addConditionMetrics(c.Status)
for _, m := range metrics {
metric := m
metric.Name = "kube_hpa_status_condition"
metric.LabelKeys = []string{"condition", "status"}
metric.LabelValues = append(metric.LabelValues, string(c.Type))
f = append(f, metric)
}
}
return f
}),
},
}
)
func wrapHPAFunc(f func(*autoscaling.HorizontalPodAutoscaler) metrics.Family) func(interface{}) metrics.Family {
return func(obj interface{}) metrics.Family {
hpa := obj.(*autoscaling.HorizontalPodAutoscaler)
metricFamily := f(hpa)
for _, m := range metricFamily {
m.LabelKeys = append(descHorizontalPodAutoscalerLabelsDefaultLabels, m.LabelKeys...)
m.LabelValues = append([]string{hpa.Namespace, hpa.Name}, m.LabelValues...)
}
return metricFamily
}
}
func createHPAListWatch(kubeClient clientset.Interface, ns string) cache.ListWatch {
return cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
return kubeClient.AutoscalingV2beta1().HorizontalPodAutoscalers(ns).List(opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
return kubeClient.AutoscalingV2beta1().HorizontalPodAutoscalers(ns).Watch(opts)
},
}
}