@@ -27,6 +27,8 @@ import (
27
27
"k8s.io/client-go/tools/cache"
28
28
basemetrics "k8s.io/component-base/metrics"
29
29
30
+ "slices"
31
+
30
32
"k8s.io/kube-state-metrics/v2/pkg/metric"
31
33
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
32
34
)
@@ -64,8 +66,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
64
66
createHPASpecTargetContainerMetric (),
65
67
createHPAStatusTargetContainerMetric (),
66
68
createHPAStatusTargetObjectMetric (),
67
- createHPASpecTargetMetric (true ),
68
- createHPASpecTargetMetric ( false ),
69
+ createHPASpecTargetMetric (),
70
+ createHPASpecTargetObjectMetric ( ),
69
71
createHPAStatusTargetMetric (),
70
72
createHPAStatusCurrentReplicas (),
71
73
createHPAStatusDesiredReplicas (),
@@ -236,13 +238,14 @@ func createHPASpecTargetContainerMetric() generator.FamilyGenerator {
236
238
)
237
239
}
238
240
239
- func createHPASpecTargetMetric ( CollectContainerResourceMetricSourceType bool ) generator.FamilyGenerator {
241
+ func createHPASpecTarget ( allowedTypes []autoscaling. MetricSourceType ) generator.FamilyGenerator {
240
242
metricName := "kube_horizontalpodautoscaler_spec_target_metric"
241
243
metricDescription := "The metric specifications used by this autoscaler when calculating the desired replica count."
242
- if CollectContainerResourceMetricSourceType {
244
+ if len ( allowedTypes ) == 1 && allowedTypes [ 0 ] == autoscaling . ObjectMetricSourceType {
243
245
metricName = "kube_horizontalpodautoscaler_spec_target_object_metric"
244
246
metricDescription = "The object metric specifications used by this autoscaler when calculating the desired replica count."
245
247
}
248
+
246
249
return * generator .NewFamilyGeneratorWithStability (
247
250
metricName ,
248
251
metricDescription ,
@@ -252,47 +255,36 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
252
255
wrapHPAFunc (func (a * autoscaling.HorizontalPodAutoscaler ) * metric.Family {
253
256
ms := make ([]* metric.Metric , 0 , len (a .Spec .Metrics ))
254
257
for _ , m := range a .Spec .Metrics {
258
+ // Check whether the metric type is allowed.
259
+ allowed := slices .Contains (allowedTypes , m .Type )
260
+ if ! allowed {
261
+ continue
262
+ }
263
+
255
264
var metricName string
256
265
var metricTarget autoscaling.MetricTarget
257
- // The variable maps the type of metric to the corresponding value
258
- metricMap := make (map [metricTargetType ]float64 )
259
- var fullTargetName string
266
+ var fullTargetName string // only used for ObjectMetricSourceType
260
267
261
268
switch m .Type {
262
269
case autoscaling .PodsMetricSourceType :
263
- if CollectContainerResourceMetricSourceType {
264
- // skip this metric if collecting container resource metric source type
265
- continue
266
- }
267
270
metricName = m .Pods .Metric .Name
268
271
metricTarget = m .Pods .Target
269
272
case autoscaling .ResourceMetricSourceType :
270
- if CollectContainerResourceMetricSourceType {
271
- // skip this metric if collecting container resource metric source type
272
- continue
273
- }
274
273
metricName = string (m .Resource .Name )
275
274
metricTarget = m .Resource .Target
276
275
case autoscaling .ExternalMetricSourceType :
277
- if CollectContainerResourceMetricSourceType {
278
- // skip this metric if collecting container resource metric source type
279
- continue
280
- }
281
276
metricName = m .External .Metric .Name
282
277
metricTarget = m .External .Target
283
278
case autoscaling .ObjectMetricSourceType :
284
- if ! CollectContainerResourceMetricSourceType {
285
- // skip this metric if not collecting container resource metric source type
286
- continue
287
- }
288
279
metricName = m .Object .Metric .Name
289
280
metricTarget = m .Object .Target
290
281
fullTargetName = m .Object .DescribedObject .Name
291
282
default :
292
- // Skip unsupported metric type
283
+ // Skip unsupported metric type.
293
284
continue
294
285
}
295
-
286
+ // The variable maps the type of metric to the corresponding value
287
+ metricMap := make (map [metricTargetType ]float64 )
296
288
if metricTarget .Value != nil {
297
289
metricMap [value ] = convertValueToFloat64 (metricTarget .Value )
298
290
}
@@ -304,12 +296,9 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
304
296
}
305
297
306
298
for metricTypeIndex , metricValue := range metricMap {
307
-
308
299
labelValues := []string {metricName , metricTypeIndex .String ()}
309
300
metricLabels := targetMetricLabels
310
-
311
- // use correct labels and values when collecting container resource metrics
312
- if m .Type == autoscaling .ObjectMetricSourceType && CollectContainerResourceMetricSourceType {
301
+ if m .Type == autoscaling .ObjectMetricSourceType {
313
302
labelValues = append (labelValues , fullTargetName )
314
303
metricLabels = objectMetricLabels
315
304
}
@@ -325,6 +314,20 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
325
314
)
326
315
}
327
316
317
+ func createHPASpecTargetObjectMetric () generator.FamilyGenerator {
318
+ return createHPASpecTarget ([]autoscaling.MetricSourceType {
319
+ autoscaling .ObjectMetricSourceType ,
320
+ })
321
+ }
322
+
323
+ func createHPASpecTargetMetric () generator.FamilyGenerator {
324
+ return createHPASpecTarget ([]autoscaling.MetricSourceType {
325
+ autoscaling .PodsMetricSourceType ,
326
+ autoscaling .ResourceMetricSourceType ,
327
+ autoscaling .ExternalMetricSourceType ,
328
+ })
329
+ }
330
+
328
331
func createHPAStatusTargetContainerMetric () generator.FamilyGenerator {
329
332
return * generator .NewFamilyGeneratorWithStability (
330
333
"kube_horizontalpodautoscaler_status_target_container_metric" ,
0 commit comments