@@ -36,8 +36,6 @@ const (
36
36
value metricTargetType = iota
37
37
utilization
38
38
average
39
-
40
- metricTargetTypeCount // Used as a length argument to arrays
41
39
)
42
40
43
41
func (m metricTargetType ) String () string {
@@ -134,53 +132,53 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
134
132
for _ , m := range a .Spec .Metrics {
135
133
var metricName string
136
134
137
- var v [ metricTargetTypeCount ] int64
138
- var ok [ metricTargetTypeCount ] bool
135
+ // The variable maps the type of metric to the corresponding value
136
+ metricMap := make ( map [ metricTargetType ] float64 )
139
137
140
138
switch m .Type {
141
139
case autoscaling .ObjectMetricSourceType :
142
140
metricName = m .Object .Metric .Name
143
141
144
- v [value ], ok [value ] = m .Object .Target .Value .AsInt64 ()
142
+ if m .Object .Target .Value != nil {
143
+ metricMap [value ] = float64 (m .Object .Target .Value .MilliValue ()) / 1000
144
+ }
145
145
if m .Object .Target .AverageValue != nil {
146
- v [average ], ok [ average ] = m .Object .Target .AverageValue .AsInt64 ()
146
+ metricMap [average ] = float64 ( m .Object .Target .AverageValue .MilliValue ()) / 1000
147
147
}
148
148
case autoscaling .PodsMetricSourceType :
149
149
metricName = m .Pods .Metric .Name
150
150
151
- v [average ], ok [ average ] = m .Pods .Target .AverageValue .AsInt64 ()
151
+ metricMap [average ] = float64 ( m .Pods .Target .AverageValue .MilliValue ()) / 1000
152
152
case autoscaling .ResourceMetricSourceType :
153
153
metricName = string (m .Resource .Name )
154
154
155
- if ok [ utilization ] = ( m .Resource .Target .AverageUtilization != nil ); ok [ utilization ] {
156
- v [utilization ] = int64 (* m .Resource .Target .AverageUtilization )
155
+ if m .Resource .Target .AverageUtilization != nil {
156
+ metricMap [utilization ] = float64 (* m .Resource .Target .AverageUtilization )
157
157
}
158
158
159
159
if m .Resource .Target .AverageValue != nil {
160
- v [average ], ok [ average ] = m .Resource .Target .AverageValue .AsInt64 ()
160
+ metricMap [average ] = float64 ( m .Resource .Target .AverageValue .MilliValue ()) / 1000
161
161
}
162
162
case autoscaling .ExternalMetricSourceType :
163
163
metricName = m .External .Metric .Name
164
164
165
165
if m .External .Target .Value != nil {
166
- v [value ], ok [ value ] = m .External .Target .Value .AsInt64 ()
166
+ metricMap [value ] = float64 ( m .External .Target .Value .MilliValue ()) / 1000
167
167
}
168
168
if m .External .Target .AverageValue != nil {
169
- v [average ], ok [ average ] = m .External .Target .AverageValue .AsInt64 ()
169
+ metricMap [average ] = float64 ( m .External .Target .AverageValue .MilliValue ()) / 1000
170
170
}
171
171
default :
172
172
// Skip unsupported metric type
173
173
continue
174
174
}
175
175
176
- for i := range ok {
177
- if ok [i ] {
178
- ms = append (ms , & metric.Metric {
179
- LabelKeys : targetMetricLabels ,
180
- LabelValues : []string {metricName , metricTargetType (i ).String ()},
181
- Value : float64 (v [i ]),
182
- })
183
- }
176
+ for metricTypeIndex , metricValue := range metricMap {
177
+ ms = append (ms , & metric.Metric {
178
+ LabelKeys : targetMetricLabels ,
179
+ LabelValues : []string {metricName , metricTypeIndex .String ()},
180
+ Value : metricValue ,
181
+ })
184
182
}
185
183
}
186
184
return & metric.Family {Metrics : ms }
0 commit comments