Skip to content

Commit 1fe588b

Browse files
committed
Refactor functions further to create one more generic function
1 parent 2466943 commit 1fe588b

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"k8s.io/client-go/tools/cache"
2828
basemetrics "k8s.io/component-base/metrics"
2929

30+
"slices"
31+
3032
"k8s.io/kube-state-metrics/v2/pkg/metric"
3133
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
3234
)
@@ -64,8 +66,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
6466
createHPASpecTargetContainerMetric(),
6567
createHPAStatusTargetContainerMetric(),
6668
createHPAStatusTargetObjectMetric(),
67-
createHPASpecTargetMetric(true),
68-
createHPASpecTargetMetric(false),
69+
createHPASpecTargetMetric(),
70+
createHPASpecTargetObjectMetric(),
6971
createHPAStatusTargetMetric(),
7072
createHPAStatusCurrentReplicas(),
7173
createHPAStatusDesiredReplicas(),
@@ -236,13 +238,14 @@ func createHPASpecTargetContainerMetric() generator.FamilyGenerator {
236238
)
237239
}
238240

239-
func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) generator.FamilyGenerator {
241+
func createHPASpecTarget(allowedTypes []autoscaling.MetricSourceType) generator.FamilyGenerator {
240242
metricName := "kube_horizontalpodautoscaler_spec_target_metric"
241243
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 {
243245
metricName = "kube_horizontalpodautoscaler_spec_target_object_metric"
244246
metricDescription = "The object metric specifications used by this autoscaler when calculating the desired replica count."
245247
}
248+
246249
return *generator.NewFamilyGeneratorWithStability(
247250
metricName,
248251
metricDescription,
@@ -252,47 +255,36 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
252255
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
253256
ms := make([]*metric.Metric, 0, len(a.Spec.Metrics))
254257
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+
255264
var metricName string
256265
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
260267

261268
switch m.Type {
262269
case autoscaling.PodsMetricSourceType:
263-
if CollectContainerResourceMetricSourceType {
264-
// skip this metric if collecting container resource metric source type
265-
continue
266-
}
267270
metricName = m.Pods.Metric.Name
268271
metricTarget = m.Pods.Target
269272
case autoscaling.ResourceMetricSourceType:
270-
if CollectContainerResourceMetricSourceType {
271-
// skip this metric if collecting container resource metric source type
272-
continue
273-
}
274273
metricName = string(m.Resource.Name)
275274
metricTarget = m.Resource.Target
276275
case autoscaling.ExternalMetricSourceType:
277-
if CollectContainerResourceMetricSourceType {
278-
// skip this metric if collecting container resource metric source type
279-
continue
280-
}
281276
metricName = m.External.Metric.Name
282277
metricTarget = m.External.Target
283278
case autoscaling.ObjectMetricSourceType:
284-
if !CollectContainerResourceMetricSourceType {
285-
// skip this metric if not collecting container resource metric source type
286-
continue
287-
}
288279
metricName = m.Object.Metric.Name
289280
metricTarget = m.Object.Target
290281
fullTargetName = m.Object.DescribedObject.Name
291282
default:
292-
// Skip unsupported metric type
283+
// Skip unsupported metric type.
293284
continue
294285
}
295-
286+
// The variable maps the type of metric to the corresponding value
287+
metricMap := make(map[metricTargetType]float64)
296288
if metricTarget.Value != nil {
297289
metricMap[value] = convertValueToFloat64(metricTarget.Value)
298290
}
@@ -304,12 +296,9 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
304296
}
305297

306298
for metricTypeIndex, metricValue := range metricMap {
307-
308299
labelValues := []string{metricName, metricTypeIndex.String()}
309300
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 {
313302
labelValues = append(labelValues, fullTargetName)
314303
metricLabels = objectMetricLabels
315304
}
@@ -325,6 +314,20 @@ func createHPASpecTargetMetric(CollectContainerResourceMetricSourceType bool) ge
325314
)
326315
}
327316

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+
328331
func createHPAStatusTargetContainerMetric() generator.FamilyGenerator {
329332
return *generator.NewFamilyGeneratorWithStability(
330333
"kube_horizontalpodautoscaler_status_target_container_metric",

0 commit comments

Comments
 (0)