Merge pull request #380 from zalando-incubator/fix-metrics-map

Fix panic when trying to add key to nil map
This commit is contained in:
Katyanna Moura 2022-01-12 11:36:53 +01:00 committed by GitHub
commit 9d8359b580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions

View File

@ -185,6 +185,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue) {
object2label[object] = labelsHashToCustomMetricStore{
labelsKey: customMetric,
}
return
}
labels2metric[labelsKey] = customMetric

View File

@ -894,6 +894,69 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
},
},
},
// Regression test covering https://github.com/zalando-incubator/kube-metrics-adapter/issues/379
{
test: "test multiple metrics with label added to metric name",
insert: []collector.CollectedMetric{
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
Namespace: "default",
Kind: "Deployment",
APIVersion: "apps/v1",
},
},
},
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject-000",
Namespace: "default",
Kind: "Deployment",
APIVersion: "apps/v1",
},
},
},
},
list: []provider.CustomMetricInfo{
{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
byName: struct {
name types.NamespacedName
info provider.CustomMetricInfo
}{
name: types.NamespacedName{Name: "metricObject-000", Namespace: "default"},
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
byLabel: struct {
namespace string
selector labels.Selector
info provider.CustomMetricInfo
}{
namespace: "default",
selector: labels.Everything(),
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
},
}
for _, tc := range multiValueTests {