pkg/custom-provider: populate metric selector

When querying custom-metrics, the metric label selectors weren't
populated in the resulting values.

Signed-off-by: Damien Grisonnet <dgrisonn@redhat.com>
This commit is contained in:
Damien Grisonnet 2020-12-10 16:14:22 +01:00
parent e0ddb886a7
commit 76020f6618

View file

@ -78,7 +78,7 @@ func NewPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interfa
}, lister }, lister
} }
func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.NamespacedName, info provider.CustomMetricInfo) (*custom_metrics.MetricValue, error) { func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.NamespacedName, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) {
ref, err := helpers.ReferenceFor(p.mapper, name, info) ref, err := helpers.ReferenceFor(p.mapper, name, info)
if err != nil { if err != nil {
return nil, err return nil, err
@ -90,7 +90,8 @@ func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.Name
} else { } else {
q = resource.NewMilliQuantity(int64(value*1000.0), resource.DecimalSI) q = resource.NewMilliQuantity(int64(value*1000.0), resource.DecimalSI)
} }
return &custom_metrics.MetricValue{
metric := &custom_metrics.MetricValue{
DescribedObject: ref, DescribedObject: ref,
Metric: custom_metrics.MetricIdentifier{ Metric: custom_metrics.MetricIdentifier{
Name: info.Metric, Name: info.Metric,
@ -98,10 +99,20 @@ func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.Name
// TODO(directxman12): use the right timestamp // TODO(directxman12): use the right timestamp
Timestamp: metav1.Time{time.Now()}, Timestamp: metav1.Time{time.Now()},
Value: *q, Value: *q,
}, nil }
if !metricSelector.Empty() {
sel, err := metav1.ParseToLabelSelector(metricSelector.String())
if err != nil {
return nil, err
}
metric.Metric.Selector = sel
}
return metric, nil
} }
func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.CustomMetricInfo, namespace string, names []string) (*custom_metrics.MetricValueList, error) { func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, namespace string, names []string, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
values, found := p.MatchValuesToNames(info, valueSet) values, found := p.MatchValuesToNames(info, valueSet)
if !found { if !found {
return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric) return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric)
@ -113,7 +124,7 @@ func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.Cu
continue continue
} }
value, err := p.metricFor(values[name], types.NamespacedName{Namespace: namespace, Name: name}, info) value, err := p.metricFor(values[name], types.NamespacedName{Namespace: namespace, Name: name}, info, metricSelector)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -175,7 +186,7 @@ func (p *prometheusProvider) GetMetricByName(name types.NamespacedName, info pro
} }
// return the resulting metric // return the resulting metric
return p.metricFor(resultValue, name, info) return p.metricFor(resultValue, name, info, metricSelector)
} }
func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) { func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
@ -194,7 +205,7 @@ func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labe
} }
// return the resulting metrics // return the resulting metrics
return p.metricsFor(queryResults, info, namespace, resourceNames) return p.metricsFor(queryResults, namespace, resourceNames, info, metricSelector)
} }
type cachingMetricsLister struct { type cachingMetricsLister struct {